Skip to main content

func_argparse

Generate a nice command line interface for a list of functions or a module, leveraging your doc-strings and type annotations. Never worry about your Argparser being out of sync with your code. Try it with pip install func_argparse.

Example

In a 'hello.py' file:

"""Say hello or goodbye to the user."""

import func_argparse


def hello(user: str, times: int = None):
    """Say hello.

    Arguments:
        user: name of the user
    """
    print(f"Hello {user}" * (1 if times is None else times))


def bye(user: str, see_you: float = 1.0):
    """Say goodbye."""
    print(f"Goodbye {user}, see you in {see_you:.1f} days")


if __name__ == "__main__":
    func_argparse.main()

From CLI:

$ python hello.py hello --user gwenzek
Hello gwenzek

$ python hello.py hello --user gwenzek --times 2
Hello gwenzekHello gwenzek

$ python hello.py bye --user gwenzek --see_you 12.345
Goodbye gwenzek, see you in 12.3 days

$ python hello.py hello -u gwenzek -t 1
Hello gwenzek

$ python hello.py --help
usage: hello.py [-h] {hello,bye} ...

Say hello or goodbye to the user.

positional arguments:
  {hello,bye}
    hello      Say hello.
    bye        Say goodbye.

optional arguments:
  -h, --help   show this help message and exit

$ python hello.py bye --help
usage: hello.py bye [-h] -u USER [-s SEE_YOU]

optional arguments:
  -h, --help            show this help message and exit
  -u USER, --user USER
  -s SEE_YOU, --see_you SEE_YOU
                        (default=1.0)

Gotchas

  • func_argparse generate classics argparse.Argparser you can mix and match them with hand-written parsers.
  • func_argparse.main() create one CLI command by "public" function from a file / module.
  • Use func_argparse.single_main(my_main) if you only have one entry point in your file.
  • All functions arguments need a type hint.
  • Arguments without default value will be marked as required.
  • A boolean argument a will generate two flags: --a and --no-a.
  • A boolean argument with no default value will be assumed to default to False.
  • The first argument starting with letter a will also be available with the flag -a.
  • The function docstring will be parsed to extract the help messages.
    • First line is used as help message for the function
    • First line starting with a will be used to extract the documentation for argument a. Spaces, dashes and columns will be stripped before displaying.
  • Some kind of functions (notably builtin and C-function) can't be inspected and we can't generate Argparser for them.
  • You can't have a function with an argument named __command when using main or multi_parser.
  • If you don't like the generated parser, you can modify it using override function.

Alternatives

Here are other alternatives you might be interested in.

  • argparse: the builtin library upon which func_argparse is built. Grants a very precise control on the CLI but is a bit verbose and prone to go out-of-sync with the code.
  • fire: also generates parser with introspection but doesn't leverage types. So the types of arguments is determined at parse time. Can generate completion files.
  • click: uses function annotations to generate the CLI.

Contibuting

All contributions are welcome. Code formatting is enforced with isort, black. Types annotations are required for the main module and checked with mypy. Tests are run with pytest.

Run pip install ".[dev]" to install the required modules Run ./tools.sh all to format your code and run mypy and pytest.

TODOs

  • Add all parameters from add_argument to override
  • Chose one from Circle CI and Github Workflows
  • Make it easy to parse a comma separated list of argument

Metadata

Release files for func-argparse 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for func-argparse 1.1.1
File Size Uploaded
func_argparse-1.1.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for func-argparse 1.1.1
File Interpreter ABI Platform
func_argparse-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / func_argparse-1.1.1.tar.gz

Download URL func_argparse-1.1.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a61bafc642c0ba711c51567fcd860526ec40de72bfafccad9c2a9b49c166166b
BLAKE2b-256 checksum
How to use checksums
98a479adbdacbfbaea11efb7dc2cf91e5db2e3fb56b17e5eb27bb0c82a9f0111
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release files / func_argparse-1.1.1-py3-none-any.whl

Download URL func_argparse-1.1.1-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a530c93ff18e838e486ced9e6b8b606a1cb184627f78d88e978c2e099426bf5
BLAKE2b-256 checksum
How to use checksums
2e37e0a393069282714a357558734d6a11f989ef8a19a9939926ff35d75e22d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page