captain


Namecaptain JSON
Version 5.2.0 PyPI version JSON
download
home_pageNone
SummaryPython cli scripts for humans
upload_time2024-09-24 22:16:55
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseThe MIT License (MIT) Copyright (c) 2014+ Jay Marcyes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords cli console
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Captain

Easy python cli scripts for people that just want get things done.


## Usage

A valid `captain` cli script needs just two things:

1. A `Default` class that extends `captain.Command` and has a `handle()` method (can be async):

    ```python
    from captain import Command
    
    class Default(Command):
        async def handle(self, foo, bar):
            return 0
    ```

2. Calling `captain.application()` at the end of your script:

    ```python
    from captain import Command, application
    
    class Default(Command):
        async def handle(self, foo, bar):
            return 0

    if __name__ == "__main__":
        application()
    ```

That's it! Whatever arguments you define in your class's `Default.handle()` method will be options on the command line. A captain script is called just like any other python command line script, so to run the above example you could do:

    $ python path/to/script.py --foo=1 --bar=2


## Argument Decorator

The `captain.arg()` decorator provides a nice passthrough api to the full [argparse.ArgumentParser.add_argument() method](https://docs.python.org/3/library/argparse.html#the-add-argument-method) if you want to fine tune how arguments are passed into your script:

```python
from captain import Command, application, arg

class Default(Command):
    @arg('--foo', '-f', action="store_true")
    @arg('arg', metavar='ARG')
    async def handle(self, *args, **kwargs):
        '''this is the help description'''
        self.output.out(args)
        self.output.out(kwargs)

if __name__ == "__main__":
    application()
```

Would print a help string like this:

    usage: script.py [-h] [--foo FOO] ARG

    this is the help description

    positional arguments:
      ARG

    optional arguments:
      -h, --help         show this help message and exit
      --foo FOO, -f FOO


## Command Output

The `captain.io.Output` class makes it easy to print stuff in your script while still giving you full control by being able to configure the logger if you need to. It also will obey the global `--quiet` flag that Captain adds to every script. 

It's available in the `handle()` method by using `self.output`:

```python
from captain import Command

class Default(Command):
    async def handle(self, *args, **kwargs):
        var1 = "print"

        var2 = "stdout"
        self.output.out("this will {} to {}", var1, var2)

        var2 = "stderr"
        self.output.err("this will {} to {}", var1, var2)

        e = ValueError("this will print with stacktrace and everything")
        self.output.exception(e)
```

The `captain.io.Output` class has a lot of nice little helper methods but Captain can also work with modules like [clint](https://github.com/kennethreitz/clint) if you need to do more advanced cli output.


## Examples

A typical python cli script

```python
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='fancy script description')
    parser.add_argument("--foo", action='store_true')
    parser.add_argument("--bar", default=0, type=int)
    parser.add_argument("args", nargs='*')
    args = parser.parse_args()
    sys.exit(0)
```

would become:

```python
import captain

class Default(captain.Command):
    async def handle(foo=False, bar=0, *args):
        '''fancy script description'''
        return 0

if __name__ == '__main__':
    captain.application()
```


### Subcommands

Captain supports multiple subcommands defined in the script by naming your `captain.Command` child classes something other than `Default`:

```python
# cli.py

import captain

class Foo(captain.Command):
    async def handle(self):
        pass

class Bar(captain.Command):
    async def handle(self):
        pass

if __name__ == '__main__':
    captain.application()
```

So `foo` could be called using:

    $ python cli.py foo

And `bar` could be called using:

    $ python cli.py bar


### Embedding captain in another package

If you want a script from you package to be usable using both `python -m example` and maybe a `console_scripts` entry point defined in `setup.py`, you can set up your package's `__main__.py` module like this:


```python
# example/__main__.py

from captain import Command, application

class Default(captain.Command):
    async def handle(self):
        pass
        
if __name__ == "__main__":
    application()
```

And then in your `setup.py` script you can add:


```python
entry_points = {
    'console_scripts': [
        'example = example.__main__:application'
    ],
}
```

That's all there is to it.


## Install

Use pip:

    $ pip install captain

For latest and greatest:

    $ pip install -U "git+https://github.com/Jaymon/captain#egg=captain"


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "captain",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "cli console",
    "author": null,
    "author_email": "Jay Marcyes <jay@marcyes.com>",
    "download_url": "https://files.pythonhosted.org/packages/80/77/402a4190fa06a418a884cd0ac5841997c439e3371ff5da43c0ea336f0b65/captain-5.2.0.tar.gz",
    "platform": null,
    "description": "# Captain\n\nEasy python cli scripts for people that just want get things done.\n\n\n## Usage\n\nA valid `captain` cli script needs just two things:\n\n1. A `Default` class that extends `captain.Command` and has a `handle()` method (can be async):\n\n    ```python\n    from captain import Command\n    \n    class Default(Command):\n        async def handle(self, foo, bar):\n            return 0\n    ```\n\n2. Calling `captain.application()` at the end of your script:\n\n    ```python\n    from captain import Command, application\n    \n    class Default(Command):\n        async def handle(self, foo, bar):\n            return 0\n\n    if __name__ == \"__main__\":\n        application()\n    ```\n\nThat's it! Whatever arguments you define in your class's `Default.handle()` method will be options on the command line. A captain script is called just like any other python command line script, so to run the above example you could do:\n\n    $ python path/to/script.py --foo=1 --bar=2\n\n\n## Argument Decorator\n\nThe `captain.arg()` decorator provides a nice passthrough api to the full [argparse.ArgumentParser.add_argument() method](https://docs.python.org/3/library/argparse.html#the-add-argument-method) if you want to fine tune how arguments are passed into your script:\n\n```python\nfrom captain import Command, application, arg\n\nclass Default(Command):\n    @arg('--foo', '-f', action=\"store_true\")\n    @arg('arg', metavar='ARG')\n    async def handle(self, *args, **kwargs):\n        '''this is the help description'''\n        self.output.out(args)\n        self.output.out(kwargs)\n\nif __name__ == \"__main__\":\n    application()\n```\n\nWould print a help string like this:\n\n    usage: script.py [-h] [--foo FOO] ARG\n\n    this is the help description\n\n    positional arguments:\n      ARG\n\n    optional arguments:\n      -h, --help         show this help message and exit\n      --foo FOO, -f FOO\n\n\n## Command Output\n\nThe `captain.io.Output` class makes it easy to print stuff in your script while still giving you full control by being able to configure the logger if you need to. It also will obey the global `--quiet` flag that Captain adds to every script. \n\nIt's available in the `handle()` method by using `self.output`:\n\n```python\nfrom captain import Command\n\nclass Default(Command):\n    async def handle(self, *args, **kwargs):\n        var1 = \"print\"\n\n        var2 = \"stdout\"\n        self.output.out(\"this will {} to {}\", var1, var2)\n\n        var2 = \"stderr\"\n        self.output.err(\"this will {} to {}\", var1, var2)\n\n        e = ValueError(\"this will print with stacktrace and everything\")\n        self.output.exception(e)\n```\n\nThe `captain.io.Output` class has a lot of nice little helper methods but Captain can also work with modules like [clint](https://github.com/kennethreitz/clint) if you need to do more advanced cli output.\n\n\n## Examples\n\nA typical python cli script\n\n```python\nimport argparse\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(description='fancy script description')\n    parser.add_argument(\"--foo\", action='store_true')\n    parser.add_argument(\"--bar\", default=0, type=int)\n    parser.add_argument(\"args\", nargs='*')\n    args = parser.parse_args()\n    sys.exit(0)\n```\n\nwould become:\n\n```python\nimport captain\n\nclass Default(captain.Command):\n    async def handle(foo=False, bar=0, *args):\n        '''fancy script description'''\n        return 0\n\nif __name__ == '__main__':\n    captain.application()\n```\n\n\n### Subcommands\n\nCaptain supports multiple subcommands defined in the script by naming your `captain.Command` child classes something other than `Default`:\n\n```python\n# cli.py\n\nimport captain\n\nclass Foo(captain.Command):\n    async def handle(self):\n        pass\n\nclass Bar(captain.Command):\n    async def handle(self):\n        pass\n\nif __name__ == '__main__':\n    captain.application()\n```\n\nSo `foo` could be called using:\n\n    $ python cli.py foo\n\nAnd `bar` could be called using:\n\n    $ python cli.py bar\n\n\n### Embedding captain in another package\n\nIf you want a script from you package to be usable using both `python -m example` and maybe a `console_scripts` entry point defined in `setup.py`, you can set up your package's `__main__.py` module like this:\n\n\n```python\n# example/__main__.py\n\nfrom captain import Command, application\n\nclass Default(captain.Command):\n    async def handle(self):\n        pass\n        \nif __name__ == \"__main__\":\n    application()\n```\n\nAnd then in your `setup.py` script you can add:\n\n\n```python\nentry_points = {\n    'console_scripts': [\n        'example = example.__main__:application'\n    ],\n}\n```\n\nThat's all there is to it.\n\n\n## Install\n\nUse pip:\n\n    $ pip install captain\n\nFor latest and greatest:\n\n    $ pip install -U \"git+https://github.com/Jaymon/captain#egg=captain\"\n\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT)  Copyright (c) 2014+ Jay Marcyes  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Python cli scripts for humans",
    "version": "5.2.0",
    "project_urls": {
        "Homepage": "http://github.com/jaymon/captain",
        "Repository": "https://github.com/Jaymon/captain"
    },
    "split_keywords": [
        "cli",
        "console"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b895922c9a9d54681a89847ff990a8913e8bc32888c175ea9d78e88a152243fe",
                "md5": "4868867d0aea56cb1eebfe3a90d2feda",
                "sha256": "f9b4a32a2c9be084acb62f6e5f08590228ec594f12e32382e0b2ceceaa006610"
            },
            "downloads": -1,
            "filename": "captain-5.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4868867d0aea56cb1eebfe3a90d2feda",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 31247,
            "upload_time": "2024-09-24T22:16:57",
            "upload_time_iso_8601": "2024-09-24T22:16:57.284545Z",
            "url": "https://files.pythonhosted.org/packages/b8/95/922c9a9d54681a89847ff990a8913e8bc32888c175ea9d78e88a152243fe/captain-5.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8077402a4190fa06a418a884cd0ac5841997c439e3371ff5da43c0ea336f0b65",
                "md5": "63ab9683e0330f9f186785dde94fa2ba",
                "sha256": "568f304c222b41000ba382f6d46889d2c28e6b340bc16b588f385a5e4b06196c"
            },
            "downloads": -1,
            "filename": "captain-5.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "63ab9683e0330f9f186785dde94fa2ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 29106,
            "upload_time": "2024-09-24T22:16:55",
            "upload_time_iso_8601": "2024-09-24T22:16:55.541371Z",
            "url": "https://files.pythonhosted.org/packages/80/77/402a4190fa06a418a884cd0ac5841997c439e3371ff5da43c0ea336f0b65/captain-5.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-24 22:16:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jaymon",
    "github_project": "captain",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "captain"
}
        
Elapsed time: 0.29550s