Parse command line arguments by defining dataclasses
Project description
typed-args
This project is inspired by TeXitoi/structopt.
Introduction
typed-args
is a Python package for creating command line interfaces with type annotations.
The program defines what arguments it requires, and typed-args
will figure out how to parse them out of sys.argv
.
typed-args
use standard python library argparse
and dataclasses
so no need to install any dependencies after Python 3.6.
Its API is very similar to argparse
.
What does it look like? Here is an example from argparse
docs and is rewritten with typed-args
:
import typed_args as ta
from typing import List, Callable
@ta.argument_parser(
description='Process some integers.'
)
class Args(ta.TypedArgs):
integers: List[int] = ta.add_argument(
metavar='N', type=int, nargs='+',
help='an integer for the accumulator'
)
accumulate: Callable[[List[int]], int] = ta.add_argument(
'--sum',
action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)'
)
args = Args.parse_args()
print(args.accumulate(args.integers))
Assuming the above Python code is saved into a file called prog.py
, it can be run at the command line and it provides useful help messages:
$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]
Process some integers.
positional arguments:
N an integer for the accumulator
optional arguments:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:
$ python prog.py 1 2 3 4
4
$ python prog.py 1 2 3 4 --sum
10
If invalid arguments are passed in, an error will be displayed:
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
Installation
From pypi
pip install typed-args
If you want to use it on python 3.5 and 3.6 please install dataclasses
:
pip install dataclasses
Core Functionality
Check _test_v0_6.py for add_argument_group
and add_subparsers
.
Create a parser
argparse
import argparse
parser = argparse.ArgumentParser(prog='ProgramName')
typed-args
import typed_args as ta
@ta.argument_parser(prog='ProgramName')
class Args(ta.TypedArgs):
pass
Add arguments
argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename') # positional argument
parser.add_argument('-c', '--count') # option that takes a value
parser.add_argument('-v', '--verbose',
action='store_true') # on/off flag
typed-args
import typed_args as ta
@ta.argument_parser()
class Args(ta.TypedArgs):
filename: str = ta.add_argument() # positional argument, use the attribute name automatically
count: str = ta.add_argument('-c', '--count') # option that takes a value, also can be annotated as Optional[str]
verbose: bool = ta.add_argument('-v', '--verbose',
action='store_true') # on/off flag
Parse args
argparse
args = parser.parse_args()
print(args.filename, args.count, args.verbose)
typed-args
args = Args.parse_args()
print(args.filename, args.count, args.verbose)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file typed_args-0.6.5.tar.gz
.
File metadata
- Download URL: typed_args-0.6.5.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f7194c7c98c16b78a52b1a467caae2cfffcff402e2ad4b90f57f77a92f018fe |
|
MD5 | 01e2eece94983af0c9a1e91af8720696 |
|
BLAKE2b-256 | 9e3e9200f084be31d5068de9002aa456526ec4a2a9bf83d40da4e82439bc240c |
File details
Details for the file typed_args-0.6.5-py3-none-any.whl
.
File metadata
- Download URL: typed_args-0.6.5-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee7329e6dccd97140205e0ae1119b268f213045655f42d5b5b03c4645e66aa68 |
|
MD5 | 40fddae88fa6fad64590c1855d025da8 |
|
BLAKE2b-256 | 3c7a1a55d964cb1b43e48d3cfce278efe3e55ea793c5e9d4e92a5ed5feca143c |