Skip to main content

Clippy generates command-line interfaces for Python modules.

Project description

👀📎 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Clippy-0.6.4.tar.gz (12.3 kB view hashes)

Uploaded Source

Built Distribution

Clippy-0.6.4-py3-none-any.whl (16.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page