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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With