Skip to content

SunDoge/typed-args

Repository files navigation

typed-args

Github Actions Pypi

This project is inspired byTeXitoi/structopt.

Introduction

typed-argsis a Python package for creating command line interfaces with type annotations. The program defines what arguments it requires, andtyped-argswill figure out how to parse them out ofsys.argv. typed-argsuse standard Python libraryargparseanddataclassesso no need to install any dependencies after Python 3.6. Its API is very similar toargparse.

What does it look like? Here is anexamplefromargparsedocs and is rewritten withtyped-args:

importtyped_argsasta
fromtypingimportList,Callable


@ta.argument_parser(
description='Process some integers.'
)
classArgs(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 calledprog.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 installdataclasses:

pip install dataclasses

Core Functionality

Check_test_v0_6.pyforadd_argument_groupandadd_subparsers.

Create a parser

argparse

importargparse
parser=argparse.ArgumentParser(prog='ProgramName')

typed-args

importtyped_argsasta

@ta.argument_parser(prog='ProgramName')
classArgs(ta.TypedArgs):
pass

Add arguments

argparse

importargparse
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

importtyped_argsasta

@ta.argument_parser()
classArgs(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)