I am writing a script that downloads a file from the web, performs some processing and stores the data into a mysql db.
I am using argparse for accepting arguments. Essentially, the script will do 1 of 4 things:
1) download a user supplied file name from the web and perform processing/db-insert.
2) download the current most filename based on yesterday's date. I have a cron job that runs this part each night after 2am.
3) do the same as #2 but for an additional file.
4) process a user defined file in the current folder and save it to an output file in the same folder.
The script can only do 1 of the above 4 things. As such, I was thinking I can use mutually exclusive optional arguments as follows:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-f','--filename',action='store',nargs=1,help='Filename to download')
group.add_argument('-b','--bulkfile',action='store',nargs=2,help='Bulk filename to process and save')
group.add_argument('-l', '--load', action='store_true', help='Download current load data')
group.add_argument('-s', '--supply', action='store_true', help='Download current supply data')
args = parser.parse_args()
if args.filename:
do something
elif args.bulkfile:
do something
elif args.load:
do something
elif args.supply:
do something
else:
print "Improper usage. Can only use [-f | -b | -l| -s]"
return
I know this isn't ideal. I would rather let argparse deal with the usage part of it. I am looking for an optimal way of achieving my objective. I appreciate the help.