Skip to main content

Convert python functions to argparse objects

Project description

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)

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

func2argparse-0.1.7.tar.gz (9.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: func2argparse-0.1.7.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for func2argparse-0.1.7.tar.gz
Algorithm Hash digest
SHA256 bc01ff7162cd9f791c455238aba59452850b48b782b92fee78c7ad4a6e0c8db9
MD5 6a178dc786b57e9415e6a1a112d30a7b
BLAKE2b-256 2a2f555729d29950baa4329c9157639495c3971a3768f1232811f7c3f61c1d8f

See more details on using hashes here.

Supported by

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