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] message
greeting.py: error: too few arguments
$ python greeting.py -h
usage: greeting.py [-h] [--is_surprised] [--name NAME] message

positional arguments:
  message

optional arguments:
  -h, --help      show this help message and exit
  --is_surprised
  --name NAME
$ 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] 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

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

Uploaded Source

Built Distribution

handofcats-0.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for handofcats-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f8d4b585f89dbe027795f5f634e591e0b6dbb4dfa7379f9fc4e002974ed4ac81
MD5 c16597d49609bd45b695966da9f479dd
BLAKE2b-256 2a801bfefea2988ada4b4544b7d5036da11301060ab112731f9f8f53cf809fb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for handofcats-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b70703245f179c9a01dacd82c47f4587f2482beb00ec160e28df3b2acf71251
MD5 9c4d79747c0949933db47d07ba55e2b5
BLAKE2b-256 c10b994b6770ac835d84d3f442f56f62b6017ed06c3a08dc84209ddee617bbb5

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