I want to validate a argument based on the value of another 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 from argument arg1", action="store_true")
args = parser.parse_args()
return args
def main():
args = parse_args()
if __name__ == "__main__":
main()
Value for arg1 can be in format YYYY or YYYY-MM. However, when the arg2 is set, I want to make sure that the value for arg1 passed is in format 'YYYY-MM' and not 'YYYY'.
i.e when I run python SampleScript.py -a 2011, the program should work fine.
when I run python SampleScript.py -a 2011 -b, the program should throw an error since -a is not in YYYY-MM format
Is it possible to do such validation using argparse ?