Clippy generates command-line interfaces for Python modules.
Project description
👀📎 Clippy
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
Built Distribution
File details
Details for the file Clippy-0.6.4.tar.gz
.
File metadata
- Download URL: Clippy-0.6.4.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2491cb34cd3fd394f1d7664a6db3efb4f48297cf0201afc84319b915746d3e09 |
|
MD5 | 69f27da6531d4c208cbbbef895e68977 |
|
BLAKE2b-256 | 3b649d16ffef72e9617e1922dc39b5332f97fc57231072f8d9cea762b2245890 |
File details
Details for the file Clippy-0.6.4-py3-none-any.whl
.
File metadata
- Download URL: Clippy-0.6.4-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f26028c837a9347e8f8512ce9636a4a2ba8d993b1b36cfdb3582fee8ee680753 |
|
MD5 | b637d24abb86b4ef7adccbb8efe718b4 |
|
BLAKE2b-256 | 91cf8c2a482bc1b66d342f1603c0a1340e47ad56b8e90fed2a61b04d52cbd427 |