1

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.

2
  • It seems like since the two responses answer two completely different questions your question is not clear... Commented Apr 13, 2013 at 20:28
  • I don't really know what subcommands are but think they could be handy at some point. Your response answered my question best as per my requirements. Commented Apr 16, 2013 at 23:02

2 Answers 2

2

argparse will handle usage for you. Running your script with no arguments I get this error message:

usage: test.py [-h] (-f FILENAME | -b BULKFILE BULKFILE | -l | -s)
test.py: error: one of the arguments -f/--filename -b/--bulkfile -l/--load -s/--supply is required

Running with both -l and -s I get

usage: test.py [-h] (-f FILENAME | -b BULKFILE BULKFILE | -l | -s)
test.py: error: argument -s/--supply: not allowed with argument -l/--load

The parser automatically handles the error messages for the mutually exclusive arguments for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you SethMMortn! So I can remove the else clause from my if block and proceed with what I already have?
Yes. You don't have to do anything else.
1

In the spirit of svn or git you could use sub-commands.

1 Comment

Thanks ExP! I will investigate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.