Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanvugt committed Sep 19, 2019
1 parent 4d9dce9 commit a54cd77
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <command>`.


Now, you can call your function from the command-line:
```bash
$ ac weather get_weather --location Amsterdam
Expand All @@ -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 <app>
```

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`.
Expand Down
1 change: 1 addition & 0 deletions auto_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
7 changes: 7 additions & 0 deletions auto_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions auto_cli/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a54cd77

Please sign in to comment.