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
experimental
sequences
from typing import List, Optional
def psum(xs: List[int], *, ys: Optional[List[int]] = None):
# treated as
# parser.add_argument('xs', nargs='*', type=int)
# parser.add_argument('--ys', action='append', required=False, type=int)
..
choices
from typing import NewType
DumpFormat = NewType("DumpFormat", str)
DumpFormat.choices = ["json", "csv"] # this: (experimental)
def run(*, format: DumpFormat = "json"):
# treated as
# parser.add_argument("--format", defaul="json", choices=("json", "csv"), required=False)
...
2.1.0
choices function
fix bug that it is not working, importing with physical filepath
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.1.0.tar.gz
(7.2 kB
view details)
Built Distribution
File details
Details for the file handofcats-2.1.0.tar.gz
.
File metadata
- Download URL: handofcats-2.1.0.tar.gz
- Upload date:
- Size: 7.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 | 597c0f109505a63026bc83a493465fddf71dfeb204ea16e9397af91b3ca6491b |
|
MD5 | 0e733a53f9506b6bc54d163ace930b13 |
|
BLAKE2b-256 | 993361da88b508af3986de1683408ea30502bef672e30d5bd6fc624f56f816dd |
File details
Details for the file handofcats-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: handofcats-2.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 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 | 2d8f7b232ce2480c189268615798f3fed1c1f79936b64d87730802b6ebdfb6fb |
|
MD5 | 5ee95307100e0720aeb4e6bf8f5cfba8 |
|
BLAKE2b-256 | 55250fc12bde2c24bb6b449731c5fec10f631a53e5ca43968012b15e49d0577f |