Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse: command-line argument that can be either named or positional

I am trying to make a Python program that uses the argparse module to parse command-line options.

I want to make an optional argument that can either be named or positional. For example, I want myScript --username=batman to do the same thing as myScript batman. I also want myScript without a username to be valid. Is this possible? If so, how can it be done?

I tried various things similar to the code below without any success.

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-u", "--user-name", default="admin")
group.add_argument("user-name", default="admin")
args = parser.parse_args()

EDIT: The above code throws an exception saying ValueError: mutually exclusive arguments must be optional.

I am using Python 2.7.2 on OS X 10.8.4.

EDIT: I tried Gabriel Jacobsohn's suggestion but I couldn't get it working correctly in all cases.

I tried this:

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-u", "--user-name", default="admin", nargs="?")
group.add_argument("user_name", default="admin", nargs="?")
args = parser.parse_args()
print(args)

and running myScript batman would print Namespace(user_name='batman'), but myScript -u batman and myScript --user-name=batman would print Namespace(user_name='admin').

I tried changing the name user-name to user_name in the 1st add_argument line and that sometimes resulted in both batman and admin in the namespace or an error, depending on how I ran the program.

I tried changing the name user_name to user-name in the 2nd add_argument line but that would print either Namespace(user-name='batman', user_name='admin') or Namespace(user-name='admin', user_name='batman'), depending on how I ran the program.

like image 733
Elias Zamaria Avatar asked Jul 11 '13 15:07

Elias Zamaria


1 Answers

The way the ArgumentParser works, it always checks for any trailing positional arguments after it has parsed the optional arguments. So if you have a positional argument with the same name as an optional argument, and it doesn't appear anywhere on the command line, it's guaranteed to override the optional argument (either with its default value or None).

Frankly this seems like a bug to me, at least when used in a mutually exclusive group, since if you had specified the parameter explicitly it would have been an error.

That said, my suggested solution, is to give the positional argument a different name.

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-u','--username')
group.add_argument('static_username',nargs='?',default='admin')

Then when parsing, you use the optional username if present, otherwise fallback to the positional static_username.

results = parser.parse_args()
username = results.username or results.static_username

I realise this isn't a particularly neat solution, but I don't think any of the answers will be.

like image 122
James Holderness Avatar answered Oct 17 '22 02:10

James Holderness