john doe
john doe

Reputation: 2253

How to add a new functionality to a command line arparse menu?

I have a scholar project which is about a command line menu script. It expects a directory with a large text file, for example this is how I execute the program:

 project/script.py /large_text_directory

So, lets assume I add a new sub directory and a .txt file to /data, let's say new_directory/list.txt:

Now I create this new functionality:

What would be an easy aproach to add the previous functionality to this command line menu.

For example I would like to run the script like this:

python project/txt

I know this could be difficult but this is my first dive in argparse. How can I aproach this?. Thanks in advance

Upvotes: 0

Views: 32

Answers (1)

msvalkon
msvalkon

Reputation: 12077

This will store the filename given from commandline to the opts.sw.

p.add_argument("-sw", action="store", help="Enter a specific file to process")

When using this commandline switch, after opts = p.parse_args() the contents of opts.sw will be path/of/the/file_1.txt

After this you'll pass that filename to the function that does your text processing.

Upvotes: 2

Related Questions