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

Uploaded Source

Built Distribution

handofcats-0.4.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for handofcats-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4d9f79f36bf2031f75e4b4833e91c661c8a3b15e8bf2e81e54271956f5f88ecb
MD5 034a5a69a2f606618233a2c5ee102939
BLAKE2b-256 896c1fdf92222b988c7e0a55e1d0520393f06bc15f28a618a8ff921dd88a28a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for handofcats-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c38b291c11e53d24b9c6b34b9356a5ca0292bda259e2ff7a7558bdb4f5ee2b0
MD5 6bcb3db6cb04201743afb3c5fef84a5d
BLAKE2b-256 3361e2cf0ff2c1a0a29f76cfcb8f3fc4700c87bcf13b240fabf5417d7794d620

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