Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse arguments nesting

Tags:

I have a following code in python:

parser = argparse.ArgumentParser(description='Deployment tool') group = parser.add_mutually_exclusive_group() group.add_argument('-a', '--add', dest='name_to_add', help='Add a new group or a role to existing group') group.add_argument('-u', '--upgrade', dest='name_to_upgrade', help='Upgrade a group with the new version') parser.add_argument('--web_port', help='Port of the WEB instance that is being added to the group') 

My problem is with "--web_port" option. I want to be able to add this option only with "-a" option but not with "-u".

I want to be able to run: "python my_script.py -a name --web_port=XXXX".

I don't want to be able to run: "python my_script.py -u name --web_port=XXXX"

How should I change my code in order to be able to run it this way?

Thanks, Arshavski Alexander.

like image 983
alexarsh Avatar asked Aug 01 '12 13:08

alexarsh


People also ask

What does argparse do in Python?

The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

What is the difference between parse_ARGs and argumentparser?

In a script, parse_args () will typically be called with no arguments, and the ArgumentParser will automatically determine the command-line arguments from sys.argv. class argparse.

Do you have to declare positional arguments in argparse?

With python argparse, you must declare your positional arguments explicitly. If you do not, the parser expects to have no arguments left over after it completes parsing, and it raises an error if arguments still remain. How do we define optional and positional arguments?

How does the add_argument_group () method work in argumentparser?

The add_argument_group() method returns an argument group object which has an add_argument() method just like a regular ArgumentParser. When an argument is added to the group, the parser treats it just like a normal


1 Answers

Instead of having -a and -u be options, you may want to make them subcommands. Then, make --web-port an option of the add subcommand:

python my_script.py add name --web_port=XXXX python my_script.py upgrade name 

Something like:

parser = argparse.ArgumentParser(description='Deployment tool') subparsers = parser.add_subparsers()  add_p = subparsers.add_parser('add') add_p.add_argument("name") add_p.add_argument("--web_port") ...  upg_p = subparsers.add_parser('upgrade') upg_p.add_argument("name") ... 

If you try run

my_script.py upgrade name --web_port=1234 

you'll get an error for unrecognized argument "--web_port".

Likewise, if you try

my_script.py add name upgrade 

you'll get an error for unrecognized argument "upgrade", since you only defined a single positional argument for the 'add' subcommand.

In other words, subcommands are implicitly mutually exclusive. The only tiny wart is that you need to add the "name" positional parameter to each subparser.

like image 148
chepner Avatar answered Sep 24 '22 06:09

chepner