Skip to content

Instantly share code, notes, and snippets.

@wolkenarchitekt
Last active February 22, 2016 09:45
Show Gist options
  • Save wolkenarchitekt/3a98dc70f0a4f01b7ca7 to your computer and use it in GitHub Desktop.
Save wolkenarchitekt/3a98dc70f0a4f01b7ca7 to your computer and use it in GitHub Desktop.
Command line argument parsing libraries

argcomplete

https://github.com/kislyuk/argcomplete

argcomplete - Bash tab completion for argparse Tab complete all the things!

Argcomplete provides easy, extensible command line tab completion of arguments for your Python script.

It makes two assumptions:

You're using bash or zsh as your shell You're using argparse to manage your command line arguments/options Argcomplete is particularly useful if your program has lots of options or subparsers, and if your program can dynamically suggest completions for your argument/option values (for example, if the user is browsing resources over the network).

argh - The Natural CLI

http://pythonhosted.org/argh/

Building a command-line interface? Found yourself uttering “argh!” while struggling with the API of argparse? Don’t want to lose its power but don’t need the complexity?

Argh provides a wrapper for argparse. Argparse is a very powerful tool; Argh just makes it easy to use.

click

http://click.pocoo.org/5/

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box.

It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.

Click in three points:

  • arbitrary nesting of commands
  • automatic help page generation
  • supports lazy loading of subcommands at runtime

cliff

http://docs.openstack.org/developer/cliff/

cliff is a framework for building command line programs. It uses plugins to define sub-commands, output formatters, and other extensions.

  • Used and developed by OpenStack

clint

http://clint-notes.readthedocs.org/en/latest/howto.html

clint (Command Line INterface Tools) is a Python package providing a set of awesome tools for developing command line applications. It has support for printing in color, indented printing, column printer and a number of other features. Let’s get started.

plac

https://plac.googlecode.com/hg/doc/plac.html

Technically plac is just a simple wrapper over argparse which hides most of its complexity by using a declarative interface: the argument parser is inferred rather than written down by imperatively. Still, plac is surprisingly scalable upwards, even without using the underlying argparse. I have been using Python for 9 years and in my experience it is extremely unlikely that you will ever need to go beyond the features provided by the declarative interface of plac: they should be more than enough for 99.9% of the use cases.

plac is targetting especially unsophisticated users, programmers, sys-admins, scientists and in general people writing throw-away scripts for themselves, choosing the command-line interface because it is quick and simple. Such users are not interested in features, they are interested in a small learning curve: they just want to be able to write a simple command line tool from a simple specification, not to build a command-line parser by hand. Unfortunately, the modules in the standard library forces them to go the hard way. They are designed to implement power user tools and they have a non-trivial learning curve. On the contrary, plac is designed to be simple to use and extremely concise, as the examples below will show.

docopt

http://docopt.org/

docopt helps you:

define interface for your command-line app, and automatically generate parser for it. docopt is based on conventions that are used for decades in help messages and man pages for program interface description. Interface description in docopt is such a help message, but formalized.

defopt

https://github.com/evanunderscore/defopt

defopt is a lightweight, no-effort argument parser.

defopt will:

Allow functions to be run from code and the command line without modification Reward you for documenting your functions Save you from writing, testing and maintaining argument parsing code defopt will not:

Modify your functions in any way Allow you to build highly complex or customized command line tools If you want total control over how your command line looks or behaves, try docopt, click or plac. If you just want to write Python code and leave the command line interface up to someone else, defopt is for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment