Package: argser
argserGitHub|PyPI|Docs|Examples|Installation|ChangelogArguments parsing without boilerplate.Features:arguments and type hints in IDEeasy nested sub-commandssane defaults for arguments' params (ie if default of arg is 3 then type should be int, or when annotation/type/default isboolthen generate 2 arguments: for true value--argand for false--no-arg, ...)𝕡𝕣𝕖𝕥𝕥𝕪 𝕡𝕣𝕚𝕟𝕥𝕚𝕟𝕘support for argparse actionscommon options/arguments reusabilityauto shortcuts generation:--verbose -> -v, --foo_bar -> --fbauto completionin shell (tnx toargcomplete)Installationpip install argser
pip install argser[tabulate]  # for fancy tables support
pip install argser[argcomplete]  # for shell auto completion
pip install argser[all]Notes for examplesIf second parameter ofparse_argsis string (as in almost all examples) then it will be parsed,
otherwise arguments to parse will be taken from command line.Simple examplefromargserimportparse_argsclassArgs:a='a'foo=1bar:boolbar_baz=42,"bar_baz help"args=parse_args(Args,show=True)argparse alternativefromargparseimportArgumentParserparser=ArgumentParser()parser.add_argument('-a',type=str,default='a',help="str, default: 'a'")parser.add_argument('--foo','-f',dest='foo',type=int,default=1,help="int, default: 1")parser.add_argument('--bar','-b',dest='bar',action='store_true',help="bool, default: None")parser.add_argument('--no-bar','--no-b',dest='bar',action='store_false')parser.set_defaults(bar=None)parser.add_argument('--bar-baz',dest='bar_baz',default=42,help="int, default: 42. bar_baz help")args=parser.parse_args()print(args)❯ python playground.py -a "aaa bbb" -f 100500 --no-b
>>> Args(bar=False, a='aaa bbb', foo=100500, bar_baz=42)❯ python playground.py -h
usage: playground.py [-h] [--bar] [--no-bar] [-a A] [--foo F] [--bar-baz B]

optional arguments:
    -h, --help           show this help message and exit
    --bar, -b            bool, default: None
    --no-bar, --no-b
    -a A                 str, default: 'a'
    --foo F, -f F        int, default: 1
    --bar-baz B, --bb B  int, default: 42. bar_baz helpGet arguments from functionimportargserdeffoo(a,b:int,c=1.2):return[a,b,c]assertargser.call(foo,'1 2 -c 3.4')==['1',2,3.4]Sub-commandsfromargserimportparse_args,sub_commandclassArgs:a:boolb=[]c=5classSubArgs:d=1e='2'sub=sub_command(SubArgs,help='help message for sub-command')args=parse_args(Args,'-a -b a b -c 10',parser_help='help message for root parser')assertargs.aisTrueassertargs.b==['a','b']assertargs.c==10assertargs.subisNoneargs=parse_args(Args,'--no-a -c 10 sub -d 5 -e "foo bar"')assertargs.aisFalseassertargs.sub.d==5assertargs.sub.e=='foo bar'❯ python playground.py -h
usage: playground.py [-h] [-a] [--no-a] [-b [B [B ...]]] [-c C] {sub} ...

positional arguments:
    {sub}

optional arguments:
    -h, --help      show this help message and exit
    -a              bool, default: None
    --no-a
    -b [B [B ...]]  List[str], default: []
    -c C            int, default: 5❯ python playground.py sub1 -h
usage: playground.py sub [-h] [-d D] [-e E]

help message for sub-command

optional arguments:
    -h, --help  show this help message and exit
    -d D        int, default: 1
    -e E        str, default: '2'Can be deep nested:fromargserimportparse_args,sub_commandclassArgs:a=1classSub1:b=1classSub2:c=1classSub3:d=1sub3=sub_command(Sub3)sub2=sub_command(Sub2)sub1=sub_command(Sub1)args=parse_args(Args,'-a 1 sub1 -b 2 sub2 -c 3 sub3 -d 4')Sub-commands from functionsimportargsersubs=argser.SubCommands()@subs.adddeffoo():return'foo'@subs.add(description="foo bar")# with additional arguments for sub-parserdefbar(a,b=1):return[a,b]assertsubs.parse('foo')=='foo'assertsubs.parse('bar 1 -b 2')==['1',2]