Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Python's argparse accept single-character abbreviations for subparsers?

I'm using argparse's excellent subparser system to make a command line program that accepts many different commands. From what I read, argparse automatically accepts single-character abbreviations for arguments when they are unambiguous. I want to make it do the same thing for subparsers. If I set up my subparsers like this:

    foo_parser = subparsers.add_parser('foo')
    # ... set up arguments and handler ...

    bar_parser = subparsers.add_parser('bar')
    # ... set up arguments and handler ...

Then I'd like the parser to accept myprogram f arg0 as an unambiguous abbreviation for myprogram foo arg0. But it doesn't.

Any ideas?

like image 864
japreiss Avatar asked Nov 28 '12 15:11

japreiss


1 Answers

From argparse documentation it is not clear that it supports such automatic abbreviation for subparser but you can explicitly set alias like this:

foo_parser = subparsers.add_parser('foo', aliases=['f'])

On the other hand, arguments abbreviation is supported.

like image 90
Bula Avatar answered Nov 10 '22 05:11

Bula