Monday, 9 September 2013

Add arguments to argparse depending on value of initial arguments

Add arguments to argparse depending on value of initial arguments

I know similar questions have been asked but I could not really find an
exact one (also, I could not understand the 'nargs' concept)
I want something like:
parser = argparse.ArgumentParser()
parser.add_argument('-mode', choices=['download', 'upload'], required=True)
parser.add_argument('-d', required=True)
args = parser.parse_args()
if args.mode == 'download':
parser.add_argument('-f', required=True)
args = parser.parse_args()
so the -f argument is required only when the -mode is download otherwise
it should not be parsed

No comments:

Post a Comment