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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

func2argparse-0.1.8.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

func2argparse-0.1.8-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file func2argparse-0.1.8.tar.gz.

File metadata

  • Download URL: func2argparse-0.1.8.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for func2argparse-0.1.8.tar.gz
Algorithm Hash digest
SHA256 d975da3c56981bc1375226a2883022ea6c9978b82bd015543494f2416db5e17c
MD5 6f4f8972c6100a963156c281e0378c15
BLAKE2b-256 ea3e7acbc45f9f919c34ab2b6e796504276adf422bc6cf478a5d307dfafb0667

See more details on using hashes here.

File details

Details for the file func2argparse-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: func2argparse-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for func2argparse-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4fe98911f8e2dec4d28c28a1782f1da6a6027d92b2046810b353fc0df2398d45
MD5 f0e14e7f06606e74865a73229eb7f08e
BLAKE2b-256 92bf9e85a96af0a805577e5b1fe1741291d13ac24cd89d377aaed8ad60afd71f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 files

0.1.7

1 file

0.1.6

1 file

0.1.5

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

0.0.9

1 file

0.0.8

1 file

0.0.7

1 file

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page