# handofcats
[![Build Status](https://travis-ci.org/podhmo/handofcats.svg?branch=master)](https://travis-ci.org/podhmo/handofcats)
A tiny magically Converter that making executable command from
plain python function. If the function is type annotated, it is used.
## Feature
- â¨Using plain python function as Console application
- If you want to treat python function as single-command, `as_command()` is helpful
- If you want to treat python function as sub-commands, `as_subcommand()` is helpful
- âï¸Escape from dependencies, if dislike this library
- If you want something like [create-react-app's eject](https://github.com/facebook/create-react-app#philosophy), use [`--expose` option](https://github.com/podhmo/handofcats#--expose)
## Installing
You can install via pip command, as you know.
```console
$ pip install handofcats
```
## Using plain python function as Console application
### `as_command()`
If you want plain python function to treat as single command, you can attach with `as_command` decorator. Then it acts as executable command.
greeting.py
```python
from handofcats import as_command
@as_command
def greeting(message: str, is_surprised: bool = False, name: str = "foo") -> None:
"""greeting message"""
suffix = "!" if is_surprised else ""
print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))
```
ð It acts as single-command.
``` console
$ python greeting.py hello
foo: hello
$ python greeting.py --is-surprised hello
foo: hello!
$ python greeting.py --is-surprised --name=bar bye
bar: bye!
```
Then, help message is here.
``` console
$ python greeting.py -h
usage: greeting [-h] [--is-surprised] [--name NAME] [--expose] [--inplace]
[--simple]
[--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}]
message
greeting message
positional arguments:
message -
optional arguments:
-h, --help show this help message and exit
--is-surprised - (default: False)
--name NAME - (default: foo)
--expose dump generated code. with --inplace, eject from handofcats dependency (default: False)
--inplace overwrite file (default: False)
--simple use minimum expression (default: False)
--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}positional arguments:
message -
optional arguments:
-h, --help show this help message and exit
--is-surprised - (default: False)
--name NAME - (default: foo)
--expose dump generated code. with --inplace, eject from handofcats dependency (default: False)
--inplace overwrite file (default: False)
--simple use minimum expression (default: False)
--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
```
( :warning: TODO: detail description )
### `as_subcommand()` and `as_subcommand.run()`
Sub-command support is also included, so handofcats can be useful, when using plain python functions as sub-commands.
Using `as_subcommand` decorator, and calling `as_subcommand.run()`. There is no need to write `if __name__ == "__main__"`.
cli.py
``` python
from handofcats import as_subcommand
@as_subcommand
def hello(*, name: str = "world") -> None:
print(f"hello {name}")
@as_subcommand
def byebye(name: str) -> None:
print(f"byebye {name}")
# :warning: don't forget this
as_subcommand.run()
```
ð It acts as sub-commands.
``` cosole
$ python cli.py hello
hello world
$ python cli.py hello --name foo
hello foo
$ python cli.py byebye foo
byebye foo
```
Then, help message is here.
```cosole
$ python cli.py -h
usage: cli.py [-h] [--expose] [--inplace] [--simple]
[--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}]
{hello,byebye} ...
optional arguments:
-h, --help show this help message and exit
--expose dump generated code. with --inplace, eject from handofcats dependency (default: False)
--inplace overwrite file (default: False)
--simple use minimum expression (default: False)
--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
subcommands:
{hello,byebye}
hello
byebye
$ python cli.py hello -h
usage: cli.py hello [-h] [--name NAME]
optional arguments:
-h, --help show this help message and exit
--name NAME (default: 'world')
```
## Dropping dependencies
If you dislike handofcats, you can drop it.
### `--expose`
> No Lock-In: You can âejectâ to a custom setup at any time. Run a single-command, and all the configuration and build dependencies will be moved directly into your project, so you can pick up right where you left off.
Something like [create-react-app'seject](https://github.com/facebook/create-react-app#philosophy) , runing with `--expose` option, generationg the code that dropping dependencies of handofcats module.
If you want to eject from [the code described above](https://github.com/podhmo/handofcats#as_command), `--expose` is helpful, maybe.
``` console
$ python greeting.py --expose
import typing as t
def greeting(message: str, is_surprised: bool = False, name: str = "foo") -> None:
"""greeting message"""
suffix = "!" if is_surprised else ""
print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))
def main(argv: t.Optional[t.List[str]] = None) -> t.Any:
import argparse
parser = argparse.ArgumentParser(prog=greeting.__name__, description=greeting.__doc__, formatter_class=type('_HelpFormatter', (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}))
parser.print_usage = parser.print_help # type: ignore
parser.add_argument('message', help='-')
parser.add_argument('--is-surprised', action='store_true', help='-')
parser.add_argument('--name', required=False, default='foo', help='-')
args = parser.parse_args(argv)
params = vars(args).copy()
return greeting(**params)
if __name__ == '__main__':
main()
```
### `--expose` with `--inplace`
In addition, running with `inplace` option, when `--expose`, overwrite
target source code.
For handofcats, eject action is `--inplace --exepose`.
## If you're lazy, you can even skip using decorators
If you're lazy, passing file to `handofcats` command. After installing this package, you can use the `handofcats` command.
For example, pass the following file to `handofcats` command:
sum.py
``` python
def sum(x: int, y: int) -> None:
print(f"{x} + {y} = {x + y}")
```
It acts as single-command, even not decorated by the decorators introduced earlier.
``` console
$ handofcats sum.py:sum 10 20
10 + 20 = 30
$ handofcats sum.py:sum -h
handofcats sum.py:sum -h
usage: sum [-h] [--expose] [--inplace] [--simple]
[--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}]
x y
positional arguments:
x
y
optional arguments:
-h, --help show this help message and exit
--expose dump generated code. with --inplace, eject from handofcats dependency (default: False)
--inplace overwrite file (default: False)
--simple use minimum expression (default: False)
--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
```
### `--expose` with handofcats command
Passed in the form `<filename>.py`, it will be interpreted as a
sub-commands. Of course, the `--expose` option also works.
And passed in the form `<filename>.py:<function name>`, it will be
interpreted as a single-command.
So, plain python function only needed.
cli.py
``` python
def hello(*, name: str = "world"):
print(f"hello {name}")
# FIXME: default arguments (positional arguments)
def byebye(name: str):
print(f"byebye {name}")
# ignored
def _ignore(name: str):
print("ignored")
```
``` console
# treated as sub-commands
$ handofcats cli.py --
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共26个文件
py:16个
txt:5个
pkg-info:2个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
资源分类:Python库 所属语言:Python 资源全名:handofcats-3.1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 26 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)