Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gem/git-style command line arguments in Python

Is there a Python module for doing gem/git-style command line arguments? What I mean by gem/git style is:

$ ./MyApp.py The most commonly used MyApp commands are:   add        Add file contents to the index   bisect     Find by binary search the change that introduced a bug   branch     List, create, or delete branches   checkout   Checkout a branch or paths to the working tree   ...  $ ./MyApp.py branch   * current-branch     master 

With no arguments, the output tells you how you can proceed. And there is a special "help" command:

$ ./MyApp.py help branch 

Which gets you deeper tips about the "branch" command.

Edit: And by doing I mean it does the usage printing for you, exits with invalid input, runs your functions according to your CLI specification. Sort of a "URL mapper" for the command line.

like image 582
David Stolarsky Avatar asked Mar 15 '12 23:03

David Stolarsky


People also ask

How do you use command line in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

What is the type specification of the command line arguments by default in Python?

Python sys module stores the command line arguments into a list, we can access it using sys. argv . This is very useful and simple way to read command line arguments as String. Let's look at a simple example to read and print command line arguments using python sys module.


2 Answers

Yes, argparse with add_subparsers().

It's all well explained in the Sub-commands section.

Copying one of the examples from there:

>>> parser = argparse.ArgumentParser() >>> subparsers = parser.add_subparsers() >>> checkout = subparsers.add_parser('checkout', aliases=['co']) >>> checkout.add_argument('foo') >>> parser.parse_args(['checkout', 'bar']) Namespace(foo='bar') 

Edit: Unfortunately there's no self generated special help command, but you can get the verbose help message (that you seem to want) with -h or --help like one normally would after the command:

$ ./MyApp.py branch --help 

By verbose I don't mean that is like a man page, it's like every other --help kind of help: listing all the arguments, etc...

Example:

>>> parser = argparse.ArgumentParser() >>> subparsers = parser.add_subparsers(description='Sub description') >>> checkout = subparsers.add_parser('checkout', description='Checkout description') >>> checkout.add_argument('foo', help='This is the foo help') >>> parser.parse_args(['checkout', '--help']) usage:  checkout [-h] foo  Checkout description  positional arguments:   foo         This is the foo help  optional arguments:   -h, --help  show this help message and exit 

If you need to, it should be easy to implement an help command that redirects to --help.

like image 88
Rik Poggi Avatar answered Oct 02 '22 15:10

Rik Poggi


A reasonable hack to get the gem/git style "help" behavior (I just wrote this for what I'm working on anyway):

parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='sub_commands') parser_branch = subparsers.add_parser('branch', description='list of branches') parser_help = subparsers.add_parser('help') parser_help.add_argument('command', nargs="?", default=None)  # I can't find a legitimate way to set a default subparser in the docs #   If you know of one, please let me know! if len(sys.argv) < 2:     sys.argv.append('--help')  parsed = parser.parse_args()  if parsed.sub_commands == "help":     if not parsed.command:         parser.parse_args(['--help'])     else:         parser.parse_args([parsed.command, '--help']) 

argparse is definitely a step up from optparse and other python solutions I've come across. But IMO the gem/git style of handling args is just a more logical and safer way to do things so it's annoying that it's not supported.

like image 35
danny Avatar answered Oct 02 '22 14:10

danny