Typed Argument Parser (Tap)

1.12.0 · active · verified Wed Apr 15

Typed Argument Parser (Tap) is a Python library that modernizes `argparse` by leveraging type hints for command-line argument parsing. It offers benefits like static type checking, code completion, and improved source code navigation. Additionally, it provides `tapify`, a function inspired by Google's Python Fire, to effortlessly run functions or initialize classes directly from command-line arguments while automatically handling type conversions. Currently at version 1.12.0, Tap is actively developed with a regular release cadence, supporting Python 3.10 and newer.

Warnings

Install

Imports

Quickstart

This example demonstrates defining a `Tap` class with typed arguments, including required and optional arguments with default values. The arguments are then parsed from the command line and used in the program.

from tap import Tap

class SimpleArgumentParser(Tap):
    name: str # Your name
    language: str = 'Python' # Programming language
    package: str = 'Tap' # Package name
    stars: int # Number of stars
    max_stars: int = 5 # Maximum stars

if __name__ == '__main__':
    args = SimpleArgumentParser().parse_args()
    print(f'My name is {args.name} and I give the {args.language} package '
          f'{args.package} {args.stars}/{args.max_stars} stars!')

view raw JSON →