python - Validate a argument value based on another argument -
i want validate argument based on value of argument.
samplescript.py def parse_args(): parser = argparse.argumentparser() parser.add_argument('-a', '--arg1', help="year-month or year") parser.add_argument('-b', '--arg2', help="needs month argument arg1", action="store_true") args = parser.parse_args() return args def main(): args = parse_args() if __name__ == "__main__": main()
value arg1
can in format yyyy
or yyyy-mm
. however, when arg2
set, want make sure value arg1
passed in format 'yyyy-mm' , not 'yyyy'.
i.e when run python samplescript.py -a 2011
, program should work fine. when run python samplescript.py -a 2011 -b
, program should throw error since -a
not in yyyy-mm
format
is possible such validation using argparse
?
Comments
Post a Comment