Skip to main content

argparse with type annotations

Project description

TypedArgs

Build Status Version

Strong type args.

This project is inspired by TeXitoi/structopt.

Install

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

Usage

import argparse
from dataclasses import dataclass

from typed_args import TypedArgs, add_argument

"""
argparse
"""
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument(
    'data', metavar='DIR', type=str,
    help='path to dataset'
)
parser.add_argument(
    '-a', '--arch', metavar='ARCH', default='resnet18', type=str,
    help='model architecture (default: resnet18)'
)
parser.add_argument(
    '-j', '--workers', default=4, metavar='N', type=int, dest='num_workers',
    help='number of data loading workers (default: 4)'
)

"""
TypedArgs
"""
@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 parser_factory(self):
        return argparse.ArgumentParser('PROG')


def test_args():
    data = '/path/to/dataset'
    arch = 'resnet50'
    num_workers = 8

    argv = f'{data} -a {arch} --workers {num_workers}'.split()

    """
    from_args = parse_args, from_known_args = parse_known_args
    """
    typed_args = Args.from_args(argv)
    args = parser.parse_args(argv)

    assert args.arch == typed_args.arch
    assert args.data == typed_args.data
    assert args.num_workers == typed_args.num_workers


if __name__ == "__main__":
    test_args()

Limitation

Currently, we don't support add_group and sub parser.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for typed-args, version 0.4.2
Filename, size File type Python version Upload date Hashes
Filename, size typed_args-0.4.2-py3-none-any.whl (5.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size typed-args-0.4.2.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page