python function to command translator
Project description
A tiny Converter that making executable command script from python function. If the function is type annotated, it is also used.
Please using as_command() decorator.
as_command()
greeting.py
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))
$ python greeting.py -h
usage: greeting.py [-h] [--expose] [--is-surprised] [--name NAME] message
greeting message
positional arguments:
message
optional arguments:
-h, --help show this help message and exit
--expose
--is-surprised
--name NAME (default: 'foo')
$ python greeting.py hello
foo: hello
$ python greeting.py --is-surprised hello
foo: hello!
$ python greeting.py --is-surprised --name=bar bye
bar: bye!
(TODO: detail description)
–expose
calling with –expose option, generationg the code that dropping dependencies of handofcats module.
$ python greeting.py --expose
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=None):
import argparse
parser = argparse.ArgumentParser(description='greeting message')
parser.print_usage = parser.print_help
parser.add_argument('message')
parser.add_argument('--is-surprised', action='store_true')
parser.add_argument('--name', default='foo', required=False)
args = parser.parse_args(argv)
greeting(**vars(args))
if __name__ == '__main__':
main()
handofcats command
sum.py
def sum(x: int, y: int) -> None:
print(f"{x} + {y} = {x + y}")
It is also ok, calling the function that not decorated via handofcats command.
$ handofcats sum.py:sum 10 20
10 + 20 = 30
$ handofcats sum.py:sum -h
handofcats sum.py:sum -h
usage: handofcats [-h] [--expose] x y
positional arguments:
x
y
optional arguments:
-h, --help show this help message and exit
--expose
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
handofcats-2.0.0.tar.gz
(6.2 kB
view details)
Built Distribution
File details
Details for the file handofcats-2.0.0.tar.gz
.
File metadata
- Download URL: handofcats-2.0.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | deaaa275456713dad71a6047752364656194cb1eb016f8731cefc53306304ca5 |
|
MD5 | 6c917aeb0dc5fe0500e80e350a2108dd |
|
BLAKE2b-256 | f25f959ad0563a48476deeccaacec04c91556a27b6d51c6afb4ea92bc6d571ac |
File details
Details for the file handofcats-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: handofcats-2.0.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42c2f8fa79040569c56f04b22cd4f39c85b2c753a6f945a4f4822b5cd5875baa |
|
MD5 | ae0e5aa0c473315c5956cbd8bbccd15f |
|
BLAKE2b-256 | cb27bbdc772167a04f79a35354eda3b67bc994c3db155864c153315b5322cfdd |