Do Python argparse accept single-character abbreviations for subparents?

I use the excellent subparser argparse 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 him to do the same for steamers. If I configured my subparameters as follows:

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

Then I would like the analyzer to accept myprogram f arg0 as the unique abbreviation for myprogram foo arg0 . But this is not so.

Any ideas?

+6
source share
1 answer

It is not clear from the argparse documentation whether it supports such automatic reduction for subparser, but you can explicitly set an alias, like so:

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

On the other hand, argument reduction is supported .

+11
source

All Articles