/clippy

Clippy turns a Python module into a simple command-line interface.

Primary LanguagePythonMIT LicenseMIT

👀📎 Clippy

PyPI Version Python Versions Travis Build Status Codacy Badge MIT codecov

Clippy (Command Line Interface Parser for Python) crawls the abstract syntax tree (AST) of a Python file and generates a simple command-line interface.

Installation

Clippy is available in PyPI and can be installed via pip.

pip install Clippy

Clippy requires Python 3.6. There is no plan to support Python 2 at this time, but earlier versions of Python 3 may be included in the future.

Usage

All functions that you would like to be available as commands should be annotated with @clippy. You can then call begin_clippy(), in, for example, the main block of your module.

"""
This is a Python module.
"""

from clippy import clippy, begin_clippy


@clippy
def some_function(foo: int, bar: str = "optional") -> str:
    """
    This is some function.

    :param foo: The first parameter.
    :param bar: The second parameter.
    :returns: The result of the function.
    """
    return f"some_value {foo} {bar}"


if __name__ == "__main__":
    begin_clippy()

When you call this file from the command line as follows:

python -m examples.readme

You will now receive information about the annotated method. This information is all based on the docstrings and annotations you've already provided in your code.

This is a Python module.

Usage:
	python -m examples.readme some_function <foo> [--bar=<str>] 
	python -m examples.readme --help

Options:
	--help   Show this screen.
	--bar    The second parameter.

Each command can then provide additional help information as needed.

python -m examples.readme some_function --help

Which results in the following output:

This is some function.

Usage:
	python -m examples.readme some_function <foo> [--bar=<str>] 

Positional arguments:
	foo      The first parameter.

Options:
	--help   Show this screen.
	--bar    The second parameter.

Note that any parameter that has a default value is treated as an option requiring a label with the -- prefix. Required parameters are treated as positional arguments. The goal is to closely match the docopt specification.

Functions that are missing documentation or type annotations will use default or placeholder values. Essentially, any valid Python function will be parsed and available on the command line.

Why Clippy

There are a number of comparable Python packages available. Clippy is designed specifically to make your existing module functions available on the command line with little effort, without modifying the way these functions behave currently.

If you'd like to create more comprehensive tools specifically for the command line, check out Click. If you'd like to make scripts with extensive customization of command-line flags, try argparse. If you'd like to parse or verify existing documentation, try docopt.

Todo

  • Use hypothesis to improve tests
  • Send output of examples to docopt to verify formatting
  • Support different parameter formatting, e.g. @param vs. :param
  • Generate, validate, and upload docs
  • Support nested commands, e.g. python -m my_module parent_method child_method
  • Support Python 3.5
  • Improved handling for list type command-line arguments
  • Lazily evaluate methods to improve performance (often only one method needs parsed)

License

All content in this repository is shared under an MIT license. See license.md for details.