From a54cd777f0b33dc894656ba7bab6850922fea631 Mon Sep 17 00:00:00 2001 From: Joris van Vugt Date: Thu, 19 Sep 2019 21:21:07 +0200 Subject: [PATCH] Improve documentation --- README.md | 30 +++++++++++++++++++++++++++++- auto_cli.py | 1 + auto_cli/cli.py | 7 +++++++ auto_cli/configuration.py | 9 +++++++++ setup.py | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db30d6f..48b760b 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,14 @@ `auto_cli` is a tool for calling Python functions directly from the command-line, without the need for writing argument parsers. Instead, the argument parser is automatically generated from the annotation of the function, including default arguments and types. When you use `auto_cli`, you can still use your Python functions from code without any changes. In fact, you can use `auto_cli` to generate a CLI for functions in a stand-alone script, or for an external library, as long as the functions have type annotations. + +## Installation +auto_cli requires Python 3.6+ and can be installed as follows +``` +$ git clone https://github.com/jvanvugt/auto_cli +$ pip install ./auto_cli +``` + ## Getting Started Add a file called `auto_cli.py` to any directory. This file registers all the functions that are available from the command-line. Imagine you wrote a package called `weather`, containing just a single function with the signature ```python @@ -23,6 +31,9 @@ Register your command-line app with `auto_cli`, by running the following command $ ac cli register_app --name weather ``` +All `auto_cli` commands start with `ac`. When you install `auto_cli`, the `cli` app will automatically be registered. The `cli` app is used for interacting with `auto_cli` itself. After running the command above, the commands that are registered in `auto_cli.py` are available via `ac weather `. + + Now, you can call your function from the command-line: ```bash $ ac weather get_weather --location Amsterdam @@ -42,7 +53,24 @@ auto_cli.register_command(get_weather) Alternatively, you could manipulate the `PYTHONPATH` environment variable to make sure Python can find your function. -## Benefits +## ac cli +The following commands are available with `ac cli`: +``` +apps Get all registered apps +register_app Register an app with auto_cli +delete_app Delete the app +``` +In general, you can figure out which commands are available for an app by running +``` +$ ac +``` + +If you want to know how to use a command, you can run it with `--help`: +``` +$ ac cli register_app --help +``` + +## Benefits over other CLI packages - Write your function once, call it from Python code _and_ the command-line - Automatically generate argument parsers, no need to duplicate argument names, default values, documentation and types. - Automatically print the result of the function to the console, no need to clutter your code with `print` or `log`. diff --git a/auto_cli.py b/auto_cli.py index 8d91fd1..7389a6a 100644 --- a/auto_cli.py +++ b/auto_cli.py @@ -2,3 +2,4 @@ auto_cli.register_command("auto_cli.cli.apps") auto_cli.register_command("auto_cli.cli.register_app") +auto_cli.register_command("auto_cli.cli.delete_app") diff --git a/auto_cli/cli.py b/auto_cli/cli.py index 82f1dde..6a371bf 100644 --- a/auto_cli/cli.py +++ b/auto_cli/cli.py @@ -53,6 +53,13 @@ def register_app(name: str, location: Optional[Path] = None) -> None: config.register_app(name, location) +def delete_app(name: str) -> str: + """Delete the app""" + with Configuration() as config: + config.delete_app(name) + return f"Deleted {name}" + + def run_command(app: str, argv: List[str]) -> None: """Run a command in app""" commands = _load_app(app) diff --git a/auto_cli/configuration.py b/auto_cli/configuration.py index c6c1680..fb61fc6 100644 --- a/auto_cli/configuration.py +++ b/auto_cli/configuration.py @@ -34,6 +34,15 @@ def register_app(self, name: str, location: Optional[Path]) -> None: self.config["apps"][name] = {"location": str(app_location)} self._dirty = True + def delete_app(self, name: str) -> None: + apps = self.config["apps"] + if name not in apps: + _print_and_quit( + f"Unknown app '{name}'. Run `ac cli apps` to see which apps are registered." + ) + del apps[name] + self._dirty = True + def get_app_location(self, name: str) -> Path: """Get the location of the app called `name`""" location = self.config["apps"].get(name) diff --git a/setup.py b/setup.py index 75f280f..82da659 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ name="auto_cli", packages=[], version="0.1.0", + python_requires=">=3.6", description="Automatically generate a command-line interface for any Python function", author="Joris van Vugt", license="MIT",