Skip to main content

func2argparse

Converts annotated python functions to argparsers.

func2argparse uses type hints and default values in function signatures and combines them together with the docstring of the function to create an argument parser out of it.

Choices for arguments are taken from the docstrings as demonstrated in the example below. Boolean arguments must always be default False.

Documentation & Installation

pip install func2argparse

Examples

from pathlib import Path

def foo(
    x: int,
    y: Path,
    z: int = 54,
    w: list[str] = ("hey", "ho"),
    k: str = "choice1",
    ll: list[int] = None,
    flg: bool = False,
    lb: list[bool] = True,
):
    """This is a test function

    Parameters
    ----------
    x : int
        First arg
    y : Path
        Second arg
    z : int
        Third arg
    w : list[str]
        Fourth arg.
        Multiline documentation
    k : str, choices=("choice1", "choice2")
        Fifth arg
    ll : list[int]
        This is an empty list
    flg : bool
        Set to True to do something
    lb : list[bool]
        A list of boolean values

    Examples
    --------
    >>> foo()
    """
    print(locals())

from func2argparse import func_to_manifest, manifest_to_argparser

manifest = func_to_manifest(foo)
parser = manifest_to_argparser(manifest, exit_on_error=False)
parser.print_help()
usage: This [-h] --x X --y Y [--z Z] [--w W [W ...]] [--k {choice1,choice2}] [--ll LL [LL ...]] [--flg] [--lb LB [LB ...]]

options:
  -h, --help            show this help message and exit
  --x X, -x X           First arg (default: None)
  --y Y, -y Y           Second arg (default: None)
  --z Z, -z Z           Third arg (default: 54)
  --w W [W ...], -w W [W ...]
                        Fourth arg. Multiline documentation (default: ('hey', 'ho'))
  --k {choice1,choice2}, -k {choice1,choice2}
                        Fifth arg (default: choice1)
  --ll LL [LL ...], -l LL [LL ...]
                        This is an empty list (default: None)
  --flg, -f             Set to True to do something (default: False)
  --lb LB [LB ...], -lb LB [LB ...]
                        A list of boolean values (default: True)

Metadata

Release files for func2argparse 0.1.8

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

Source distribution (sdist)

Source distribution for func2argparse 0.1.8
File Size Uploaded
func2argparse-0.1.8.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for func2argparse 0.1.8
File Interpreter ABI Platform
func2argparse-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / func2argparse-0.1.8.tar.gz

Download URL func2argparse-0.1.8.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d975da3c56981bc1375226a2883022ea6c9978b82bd015543494f2416db5e17c
BLAKE2b-256 checksum
How to use checksums
ea3e7acbc45f9f919c34ab2b6e796504276adf422bc6cf478a5d307dfafb0667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / func2argparse-0.1.8-py3-none-any.whl

Download URL func2argparse-0.1.8-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fe98911f8e2dec4d28c28a1782f1da6a6027d92b2046810b353fc0df2398d45
BLAKE2b-256 checksum
How to use checksums
92bf9e85a96af0a805577e5b1fe1741291d13ac24cd89d377aaed8ad60afd71f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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