I am working on Python 2.7.9 and using argparse module for command line arguments. I want to extend my code such that it should be able to take the command line arguments depending on the arguments already given by the user in the same command line. Lets say, the arguments are -a,-b,-c,-d If the user gives -a <value>
then only he should be able to enter -x <value>
and same applies to the other case. If the user enters -b <value>
then only he should be able to enter -y <value>
. Can anyone please help me with this. Thank you!
One way would be to parse the args in two steps using parse_known_args
, for example:
ap = argparse.ArgumentParse()
ap.add_argument('-a')
args, unknown = ap.parse_known_args()
if args.a and '-x' in unknown:
ap.add_argument('-x')
args = ap.parse_args()
When designing command line arguments, follow the best practices/standards used in *nix systems.
If you are looking for example to use argparse
refer this link
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.