python - Argparse: Required argument 'y' if 'x' is present -
i have requirement follows:
./xyifier --prox --lport lport --rport rport
for argument prox , use action='store_true' check if present or not. not require of arguments. but, if --prox set require rport , lport well. there easy way of doing argparse without writing custom conditional coding.
more code:
non_int.add_argument('--prox', action='store_true', help='flag turn on proxy') non_int.add_argument('--lport', type=int, help='listen port.') non_int.add_argument('--rport', type=int, help='proxy port.')
no, there isn't option in argparse make mutually inclusive sets of options.
the simplest way deal be:
if args.prox , args.lport none , args.rport none: parser.error("--prox requires --lport , --rport.")
Comments
Post a Comment