Skip to main content

func-to-script` is a lightweight and convenient tool which can be used to turn a Python function into a command line script, with minimal boilerplate! As `func-to-script` is only a thin wrapper around `argparse`, it is incredibly lightweight there are no additional dependencies required! `func-to-script` is designed to be used in simple cases, so offers a streamlined feature set. For more complex scenarios, it is recommended to use `argparse` directly.

Project description

func-to-script: Quickly turn a function into a command-line script

func-to-script is a lightweight and convenient tool which can be used to turn a Python function into a command line script, with minimal boilerplate!

As func-to-script is only a thin wrapper around argparse, it is incredibly lightweight there are no additional dependencies required!

func-to-script is designed to be used in simple cases, so offers a streamlined feature set. For more complex scenarios, it is recommended to use argparse directly.

Installation

func-to-script can be installed from pip using the following command:

pip install func-to-script

Usage

To get started, all that you need to do is add the script decorator to the function that you wish to convert, as demonstrated below:

# say_hello_script.py

from func_to_script import script

@script
def say_hello(
    greeting: str = "Hello", name: str = "World", print_message: bool = True
):
    """
    A simple function to say hello

    :param greeting: the greeting to use
    :param name: the person to greet
    :param print_message: flag to indicate whether to print to the command line
    """
    hello_str = f"{greeting}, {name}"

    if print_message:
        print(hello_str)

    return hello_str


if __name__ == "__main__":
    say_hello()

We can now call this like so:

python say_hello_script.py --greeting hi --print_message False

As func-to-script uses regular Python type hints and default value syntax, no additional changes are required! If a docstring is provided, func-to-script will also attempt to parse this to provide descriptions of the required parameters to display when using:

python say_hello_script.py -h

The script decorator does not affect normal function usage, so decorated functions can still be imported and called elsewhere:

from say_hello_script import say_hello


def main():
    say_hello(print_message=True)


if __name__ == "__main__":
    main()

Constraints

  • Only four types are supported for script level arguments: str, int, float, bool
  • When calling a function as a script, arguments must be given as keyword arguments; positional arguments are not supported by design, to promote clarity.
  • To be able to parse documentation, docstrings must be written in sphinx format. If this is not the case, the docstring will be ignored.

Aren't there a bunch of libraries that do this already

There are, and most of those contain more functionality than is contained here! However, most of these tend to require interacting with custom objects or adding additional boilerplate; func-to-script is able to avoid this for simple use cases.

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

func-to-script-0.1.0.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

func_to_script-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file func-to-script-0.1.0.tar.gz.

File metadata

  • Download URL: func-to-script-0.1.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for func-to-script-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ef112ae88f25c510f9760cde2d5b797d56590748403114a9af27e70a3f529af
MD5 582d7d64216192d068584d1a80ee06b6
BLAKE2b-256 076e2c905f818f047063c44bf7068aca17a21904a2cbdf3eefdcf07f387e7837

See more details on using hashes here.

File details

Details for the file func_to_script-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: func_to_script-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for func_to_script-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d6d9cfe906db82861e1d6d851b25b8be18fec3b273b1f5c03067eec62bd557a
MD5 71d3b9bf4cf1dbf1f88db9ee4f56cd45
BLAKE2b-256 71e1a086061f7a1fdfd837d4e41ab1e27911e1e0078ddef28501b45c9bf07f01

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