cli-buddy


Namecli-buddy JSON
Version 0.2.6 PyPI version JSON
download
home_pagehttps://github.com/jicruz96/cli-buddy
SummaryThe fastest, simplest way to create CLIs in Python
upload_time2024-07-19 22:11:35
maintainerNone
docs_urlNone
authorJ.I. Cruz
requires_python<4.0,>=3.11
licenseNone
keywords data validation dataclasses validation models meta-programming
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# The fastest, simplest way to create a CLI in Python

`cli-buddy` provides an ergonomic syntax for creating command-line interfaces in Python.

### With `cli-buddy`, 👇 this code snippet is all it takes to build a CLI.

```python
from cli_buddy import CLI

class MyCLI(CLI):
    arg: str
```

Under the hood, `cli-buddy` is a pydantic-flavored wrapper around the built-in `argparse` module.


## Requirements
* Python 3.11+

## Install

```bash
pip install cli-buddy
```

## Usage

### Use the `CLI` class to define your CLI's arguments and options.


This code snippet...

```python
# main.py
from cli_buddy import CLI

class TerminalTimer(CLI):
    seconds: int
    # boolean fields are automatically converted to flags
    show_time: bool
```

... will produce the following CLI:

```bash
$ python main.py --help
usage: main.py [-h] seconds

positional arguments:
  seconds

options:
  -h, --help  show this help message and exit
  --show_time
```

A CLI class alone won't run anything, obviously. We need to define our behavior first.

### Define your CLI's behavior

#### Either use your CLI instance however you like...


```python

# main.py
...

cli = TerminalTimer()

for _ in range(cli.seconds):
    print(_)
    time.sleep(1)
```

#### ... or just put your logic within the `__call__()` method of your CLI class...


```python
class TerminalTimer(CLI):
    seconds: int
    show_time: bool

    def __call__(self):
        for _ in range(self.seconds):
            print(_)
            time.sleep(1)

TerminalTimer()  # That's it!
```

### Give your arguments default values to convert arguments to flags.

The `CLI` class will automatically convert arguments to flags if:
* they are assigned a default value,
* *or if the field is a boolean*.

For example, this...
```python
class TerminalTimer(CLI):
    seconds: int = 1
    show_time: bool
```
... will produce the following CLI:
```bash
$ python main.py --help
usage: main.py [-h] [--seconds SECONDS] [--show_time]

options:
  -h, --help         show this help message and exit
  --seconds SECONDS  (default: 1)
  --show_time
```

### Use the `Argument` class to further configure your CLI's arguments.

The `Argument` function takes the same arguments as the built-in `argparse.ArgumentParser.add_argument` function.


For example, this...

```python
from cli_buddy import CLI, Argument


class TerminalTimer(CLI):
    seconds: int = Argument(help="Number of seconds to wait")
    show_time: bool = Argument("-t", help="Show seconds in terminal", action="store_true")
```

... will produce the following CLI:
```bash
$ python main.py --help
usage: main.py [-h] -t seconds

positional arguments:
  seconds          Number of seconds to wait

options:
  -h, --help       show this help message and exit
  -t, --show_time  Show seconds in terminal
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jicruz96/cli-buddy",
    "name": "cli-buddy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": "data validation, dataclasses, validation, models, meta-programming",
    "author": "J.I. Cruz",
    "author_email": "hello@jicruz.com",
    "download_url": "https://files.pythonhosted.org/packages/d4/57/42adad3dedc1393f1a21c590d45570009ff6207f338e014183f4090b6d32/cli_buddy-0.2.6.tar.gz",
    "platform": null,
    "description": "\n# The fastest, simplest way to create a CLI in Python\n\n`cli-buddy` provides an ergonomic syntax for creating command-line interfaces in Python.\n\n### With `cli-buddy`, \ud83d\udc47 this code snippet is all it takes to build a CLI.\n\n```python\nfrom cli_buddy import CLI\n\nclass MyCLI(CLI):\n    arg: str\n```\n\nUnder the hood, `cli-buddy` is a pydantic-flavored wrapper around the built-in `argparse` module.\n\n\n## Requirements\n* Python 3.11+\n\n## Install\n\n```bash\npip install cli-buddy\n```\n\n## Usage\n\n### Use the `CLI` class to define your CLI's arguments and options.\n\n\nThis code snippet...\n\n```python\n# main.py\nfrom cli_buddy import CLI\n\nclass TerminalTimer(CLI):\n    seconds: int\n    # boolean fields are automatically converted to flags\n    show_time: bool\n```\n\n... will produce the following CLI:\n\n```bash\n$ python main.py --help\nusage: main.py [-h] seconds\n\npositional arguments:\n  seconds\n\noptions:\n  -h, --help  show this help message and exit\n  --show_time\n```\n\nA CLI class alone won't run anything, obviously. We need to define our behavior first.\n\n### Define your CLI's behavior\n\n#### Either use your CLI instance however you like...\n\n\n```python\n\n# main.py\n...\n\ncli = TerminalTimer()\n\nfor _ in range(cli.seconds):\n    print(_)\n    time.sleep(1)\n```\n\n#### ... or just put your logic within the `__call__()` method of your CLI class...\n\n\n```python\nclass TerminalTimer(CLI):\n    seconds: int\n    show_time: bool\n\n    def __call__(self):\n        for _ in range(self.seconds):\n            print(_)\n            time.sleep(1)\n\nTerminalTimer()  # That's it!\n```\n\n### Give your arguments default values to convert arguments to flags.\n\nThe `CLI` class will automatically convert arguments to flags if:\n* they are assigned a default value,\n* *or if the field is a boolean*.\n\nFor example, this...\n```python\nclass TerminalTimer(CLI):\n    seconds: int = 1\n    show_time: bool\n```\n... will produce the following CLI:\n```bash\n$ python main.py --help\nusage: main.py [-h] [--seconds SECONDS] [--show_time]\n\noptions:\n  -h, --help         show this help message and exit\n  --seconds SECONDS  (default: 1)\n  --show_time\n```\n\n### Use the `Argument` class to further configure your CLI's arguments.\n\nThe `Argument` function takes the same arguments as the built-in `argparse.ArgumentParser.add_argument` function.\n\n\nFor example, this...\n\n```python\nfrom cli_buddy import CLI, Argument\n\n\nclass TerminalTimer(CLI):\n    seconds: int = Argument(help=\"Number of seconds to wait\")\n    show_time: bool = Argument(\"-t\", help=\"Show seconds in terminal\", action=\"store_true\")\n```\n\n... will produce the following CLI:\n```bash\n$ python main.py --help\nusage: main.py [-h] -t seconds\n\npositional arguments:\n  seconds          Number of seconds to wait\n\noptions:\n  -h, --help       show this help message and exit\n  -t, --show_time  Show seconds in terminal\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "The fastest, simplest way to create CLIs in Python",
    "version": "0.2.6",
    "project_urls": {
        "Homepage": "https://github.com/jicruz96/cli-buddy",
        "Repository": "https://github.com/jicruz96/cli-buddy"
    },
    "split_keywords": [
        "data validation",
        " dataclasses",
        " validation",
        " models",
        " meta-programming"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fd22b24f698cc9b952695509484062f986b87cec77c4a9292e2c8dda37480ea4",
                "md5": "ac8022c1a80478e8fb51cf0c8d41939a",
                "sha256": "461fcf9b51b038fa84839588a7b54bc6bd782e66a31a8ebe8854b4495c7d6286"
            },
            "downloads": -1,
            "filename": "cli_buddy-0.2.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ac8022c1a80478e8fb51cf0c8d41939a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 7310,
            "upload_time": "2024-07-19T22:11:34",
            "upload_time_iso_8601": "2024-07-19T22:11:34.330377Z",
            "url": "https://files.pythonhosted.org/packages/fd/22/b24f698cc9b952695509484062f986b87cec77c4a9292e2c8dda37480ea4/cli_buddy-0.2.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d45742adad3dedc1393f1a21c590d45570009ff6207f338e014183f4090b6d32",
                "md5": "a3e49f3a5bc41f3d1decf6b0586ee5e3",
                "sha256": "a61882b15862a1783120bcd2d97444dec95408c7b63010e6b8d4efcae6f48979"
            },
            "downloads": -1,
            "filename": "cli_buddy-0.2.6.tar.gz",
            "has_sig": false,
            "md5_digest": "a3e49f3a5bc41f3d1decf6b0586ee5e3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 5493,
            "upload_time": "2024-07-19T22:11:35",
            "upload_time_iso_8601": "2024-07-19T22:11:35.639466Z",
            "url": "https://files.pythonhosted.org/packages/d4/57/42adad3dedc1393f1a21c590d45570009ff6207f338e014183f4090b6d32/cli_buddy-0.2.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-19 22:11:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jicruz96",
    "github_project": "cli-buddy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "cli-buddy"
}
        
Elapsed time: 3.70360s