Skip to main content

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


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.3.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

handofcats-0.3.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file handofcats-0.3.1.tar.gz.

File metadata

  • Download URL: handofcats-0.3.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for handofcats-0.3.1.tar.gz
Algorithm Hash digest
SHA256 91523bce67cd43947b0828d3943afdadb821d2a699b17b9483e15f15676b60b2
MD5 f72240fc7db2db2ca95dd3c52850c900
BLAKE2b-256 036a284ea8dede1368e619559bdbf45d83d4214f3ed7946d6e90f565ae86b4c5

See more details on using hashes here.

File details

Details for the file handofcats-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for handofcats-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a91f9d152d9a43df8dbd52a9a87e6eccd2bddd89e0b16a1ee3633099e441418
MD5 66901b5a66dd20ff7c3e627fb3a40f86
BLAKE2b-256 80e1874258b3441478c1fe2e9b8976745c78cf067004a416537110957e1da127

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page