Make Python's argparse accept single-character abbreviations for subparsers?
argparse, command-line, python
Solution
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.
Problem
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?