Reputation: 81
hi i want to add the following arguments:
parser = argparse.ArgumentParser()
parser.add_argument('-n','--name', required=True)
parser.add_argument("-sd", "--start_date", dest="start_date",
type=valid_date,
help="Date in the format yyyy-mm-dd")
parser.add_argument("-ed", "--end_date", dest="end_date",
type=valid_date,
help="Date in the format yyyy-mm-dd")
i want that in case the name='test1' then start_date and end_date will be mandatory. can it be done with arparse? or do i need some validation method to enforce it to be mandatory?
thanks
Upvotes: 0
Views: 5328
Reputation: 1874
You can check the condition and then check if the other arguments are both provided.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-n','--name', required=True)
parser.add_argument("-sd", "--start_date", dest="start_date",
help="Date in the format yyyy-mm-dd")
parser.add_argument("-ed", "--end_date", dest="end_date",
help="Date in the format yyyy-mm-dd")
args = parser.parse_args()
if args.name == "test1":
if args.start_date is None or args.end_date is None:
parser.error('Requiring start and end date if test1 is provided')
Upvotes: 3