Package: main-dec
main_decA small library for painless commandline argument parsing in python.Install> pip install main-decQuickstart# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(required_str:str,optional_int=1,optional_tuple:Tuple[int,...]=()):"""A small example cli:param required_str: a required str:param optional_int: an optional int:param optional_tuple: an optional tuple"""print('required_str',required_str)print('optional_int',optional_int)print('optional_tuple',optional_tuple)> python -m my_cli -husage: my_cli [-h] [--optional-tuple OPTIONAL_TUPLE [OPTIONAL_TUPLE ...]] [--optional-int OPTIONAL_INT] required_strA small example clipositional arguments:required_str          a required stroptional arguments:-h, --help            show this help message and exit--optional-tuple OPTIONAL_TUPLE [OPTIONAL_TUPLE ...]an optional tuple--optional-int OPTIONAL_INTan optional int> python -m my_cli arg --optional-int 2 --optional-tuple 1 2 3required_str argoptional_int 2optional_tuple (1, 2, 3)Required and optional argumentsPositional arguments to your function will be parsed as required arguments to your cli.
Optional arguments to your function will be parsed as optional arguments.# my_cli.pyfrommain_decimportmain@maindefrun(required_arg:str,optional_arg=''):pass> python -m my_cli -husage: my_cli [-h] [--optional-arg OPTIONAL_ARG] required_argpositional arguments:required_argoptional arguments:-h, --help            show this help message and exit--optional-argFlagsOptionalboolarguments are parsed as flags, such that passing them
on the commandline "flips" their
truthiness.# my_cli.pyfrommain_decimportmain@maindefrun(positive_flag=False,negative_flag=True):print('positive_flag',positive_flag)print('negative_flag',negative_flag)> python -m my_cli --postive-flag --negative-flagpositive_flag Truenegative_flag FalseType conversionsPEP484 annotated arguments and arguments with default values will have their
types converted before they are passed to your function.# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(required_float:float,optional_tuple:Tuple[float,...]=()):print('required_float',required_float)print('optional_tuple',optional_tuple)> python -m my_cli 1 --optional-tuple 2 3 4required_float 1.0optional_tuple (2.0, 3.0, 4.0)Currently supported types arestr,bytes,int,float,list(includingtyping.List),tuple(includingtyping.Tuple) andEnum.stris the default type
for arguments that are not annotated and do not have a default value.Tuple argumentsTuple arguments can either be parsed as varied length tuples, or fixed length tuples.Fixed length tuples are arguments that are annotated without...as a type variable,
or arguments with default values with mixed types# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(fixed_length_tuple1:Tuple[int,int],fixed_length_tuple2=(1,'arg')):pass> python -m my_cli -husage: my_cli [-h] [--fixed-length-tuple2 FIXED_LENGTH_TUPLE2 FIXED_LENGTH_TUPLE2] fixed_length_tuple1 fixed_length_tuple1positional arguments:fixed_length_tuple1optional arguments:-h, --help            show this help message and exit--fixed-length-tuple2 FIXED_LENGTH_TUPLE2 FIXED_LENGTH_TUPLE2Varied length tuples are arguments thatAre annotated with...as a type variable (e.gTuple[int, ...])Are annotated simply withTupleortupleHave a tuple as a default value with homogeneous types (e.g(1, 2))Enum argumentsArguments annotated withEnum, or with a defaultEnumtype, can be used
to enforce that an argument must have certain values.# my_cli.pyfromenumimportEnumfrommain_decimportmainclassChoice(Enum):first=1second=2@maindefrun(argument_with_choices:Choice):print('argument_with_choices',argument_with_choices)> python -b my_cli -husage: my_cli [-h] {first,second}positional arguments:{first,second}optional arguments:-h, --help      show this help message and exit> python -m my_cli secondargument_with_choices Choice.secondThis can be combined with generic types such astyping.Tupleandtyping.List, as well
as arguments with default arguments that aretupleorlisttypes withEnumelements.CLI DocumentationDoc strings in ReST or Google style are parsed and used to create usage
and help messages# my_cli.pyfrommain_decimportmain@maindefrun(arg:str):"""An example cli:param arg: A required argument"""> python -m my_cli -husage: my_cli [-h] argAn example clipositional arguments:arg         A required argumentoptional arguments:-h, --help  show this help message and exit