python function to command translator
Project description
A tiny Converter that making executable command script from python function. If the function has sphinx autodoc style docstring, it is also used.
this module has two functions.
as_command()
describe()
If you just convert python function to executable command, then use as_command(). And, you want to show a list of managemented commands, describe() is helpful.
as_command()
# greeting.py
from handofcats import as_command
@as_command
def greeting(message, is_surprised=False, name="foo"):
suffix = "!" if is_surprised else ""
print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))
$ python greeting.py
usage: greeting.py [-h] [--is-surprised] [--name NAME] [-v] [-q] message
greeting.py: error: too few arguments
$ python greeting.py -h
usage: greeting.py [-h] [--is-surprised] [--name NAME] [-v] [-q] message
positional arguments:
message
optional arguments:
-h, --help show this help message and exit
--is-surprised
--name NAME
-v, --verbose (default option: increment logging level(default is WARNING))
-q, --quiet (default option: decrement logging level(default is WARNING))
$ python greeting.py hello
foo: hello
$ python greeting.py --is-surprised hello
foo: hello!
$ python greeting.py --is-surprised --name=bar bye
bar: bye!
with docstring (additional feature)
from handofcats import as_command
@as_command
def greeting(message, is_surprised=False, name="foo"):
""" greeting message
:param message: message of greeting
:param is_surprised: surprised or not (default=False)
:param name: name of actor
"""
suffix = "!" if is_surprised else ""
print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))
$ python greeting.py -h
usage: greeting.py [-h] [--is-surprised] [--name NAME] [-v] [-q] message
greeting message
positional arguments:
message message of greeting
optional arguments:
-h, --help show this help message and exit
--is-surprised surprised or not (default=False)
--name NAME name of actor
-v, --verbose (default option: increment logging level(default is
WARNING))
-q, --quiet (default option: decrement logging level(default is
WARNING))
describe()
$ tree foo/
foo/
├── __init__.py
├── __main__.py
├── bye.py
└── hello.py
$ cat foo/__main__.py
from handofcats import describe
describe()
$ python -m foo
avaiable commands are here. (with --full option, showing full text)
- foo.bye
- foo.hello -- hello message
$ cat foo/hello.py
from handofcats import as_command
@as_command
def hello():
"""
hello message
"""
print("hello")
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-0.4.2.tar.gz
(7.8 kB
view details)
Built Distribution
File details
Details for the file handofcats-0.4.2.tar.gz
.
File metadata
- Download URL: handofcats-0.4.2.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ed60947e7984a046fb792c20b4349a14a4f42a49d194fe4463b52ba4ac71908 |
|
MD5 | d78dd808af4243e2773f6702b0a27f63 |
|
BLAKE2b-256 | 6f98742efa7558df757d116abd622faf3e8c6e7daa2f79c0945302d97ef362c5 |
File details
Details for the file handofcats-0.4.2-py3-none-any.whl
.
File metadata
- Download URL: handofcats-0.4.2-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c45aa3e065b665b6d050cff06432c92aee02094449af776acb389f21f3438a3 |
|
MD5 | 33936cd2d3b4fa43b0cf310e1b0f8f66 |
|
BLAKE2b-256 | b70554819e6c9d2b55eb119c639f62d31174e9465256eb653068965fa1e34de5 |