argparse with type annotations
Project description
TypedArgs
Strong type args.
This project is inspired by TeXitoi/structopt.
Install
From pypi
pip install typed-args
From github
pip install git+https://github.com/SunDoge/typed-args.git@v0.3
If you want to use it on python 3.5 and 3.6 please install dataclasses
:
pip install dataclasses
Usage
from dataclasses import dataclass
from typed_args import TypedArgs, add_argument
@dataclass
class Args(TypedArgs):
data: str = add_argument(metavar='DIR', help='path to dataset')
arch: str = add_argument('-a', '--arch', metavar='ARCH', default='resnet18',
help='model architecture (default: resnet18)')
num_workers: int = add_argument('-j', '--workers', default=4, metavar='N',
help='number of data loading workers (default: 4)')
def __post_init__(self):
"""
anothor way to init
"""
self._parse_args()
def test_args():
data = '/path/to/dataset'
arch = 'resnet50'
num_workers = 8
argv = f'{data} -a {arch} --workers {num_workers}'.split()
args = Args.from_args(argv)
args1 = Args() # if __post_init__ is defined
assert args.arch == arch
assert args.data == data
assert args.num_workers == num_workers
if __name__ == "__main__":
test_args()
Limitation
Currently, we don't support add_group and sub parser.
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
typed-args-0.3.7.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for typed_args-0.3.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3f6080fffdb6dfb231a02a5720f0b082225239175e6ce6a56a4e6a36b2ed71c |
|
MD5 | 5a657d899652cd19f35c4d9ff07c50b1 |
|
BLAKE2b-256 | 53f23c5cb0cbd345b1def4ad63c6db38888a0cb564a50c23c3964891368c3803 |