argparse-ext

1.4.2 · active · verified Fri Apr 17

argparse-ext is a Python library that extends the standard `argparse` module, providing additional argument types and features for command-line interface parsing. It offers types like path, URL, JSON, email, and utilities for subcommands and config files. The current version is 1.4.2, and it maintains a steady, though not rapid, release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `argparse_ext.ArgumentParser` with some of its extended types like 'path', 'url', and 'json'. It also shows how to access the methods and attributes of the resulting parsed objects.

from argparse_ext import ArgumentParser

parser = ArgumentParser(prog='my_cli_tool')
parser.add_argument('--path', type='path', help='A path argument which becomes a pathlib.Path object')
parser.add_argument('--url', type='url', help='A URL argument which becomes a urllib.parse.ParseResult object')
parser.add_argument('--config', type='json', help='A JSON string which is parsed into a Python object')

args = parser.parse_args(['--path', '/tmp/foo.txt', '--url', 'http://example.com/foo', '--config', '{"key": "value"}']) # Simulate args for quickstart

print(f"Path object type: {type(args.path)}, absolute: {args.path.absolute()}")
print(f"URL object type: {type(args.url)}, scheme: {args.url.scheme}")
print(f"Config object type: {type(args.config)}, value: {args.config['key']}")

view raw JSON →