Package: config-argument-parser
config-argument-parserA package help automatically create command-line interface from configuration or code.It contains two modules CAP🧢(ConfigArgumentParser) and TAP🚰(TypeArgumentParser).Read the documentationhere.MotivationConfiguration files are highly readable and useful for specifying options, but sometimes they are not convenient as command-line interface. However, it requires writing a lot of code to produce a CLI. This package automates the building process, by utilizing the Python standard librariesconfigparserandargparse.The design is to minimize the changes to your original scripts, so as to facilitate maintenance.FeaturesOnly a few extra lines are needed to build a CLI from an existing script.The comments are parsed as help messages. (Most libraries do not preserve the comments.)Consistent format between configuration and script provides ease of use.UsageCase 1: create CLI from an objectIf you used class to store arguments, create a scriptexample.pyas below. Default arguments are defined as class attributes, and parsed arguments are stored as instance attributes. The good is that auto-completion can be triggered in editors.For the best practice, seeCase 4.importconfigargparserclassArgs:# Help message of the first argument. Help is optional.a_string="abc"a_float=1.23# inline comments are omitted# Help can span multiple lines.# This is another line.a_boolean=Falsean_integer=0args=Args()parser=configargparser.ConfigArgumentParser()# if `shorts` is provided, add short options for the first few arguments in orderparser.parse_obj(args,shorts="sfb")print(args.a_string)print(args.a_float)print(args.a_boolean)print(args.an_integer)In fact, only the snippet below is added to the original script. Moreover, removing this minimal modification does not affect the original script.importconfigargparserparser=configargparser.ConfigArgumentParser()parser.parse_obj(args)Show help,python example.py -h:$pythonexample.py-husage: example.py [-h] [-s A_STRING] [-f A_FLOAT] [-b] [--an-integer AN_INTEGER]optional arguments:-h, --help            show this help message and exit-s A_STRING, --a-string A_STRINGHelp message of the first argument. Help is optional. (default: abc)-f A_FLOAT, --a-float A_FLOAT(default: 1.23)-b, --a-boolean       Help can span multiple lines. This is another line. (default: False)--an-integer AN_INTEGER(default: 0)Run with options, for example,python example.py -b -f 1:$pythonexample.py-b-f1abc1.0True0Note that the values are changed.Case 2: create CLI from configurationIf you used configuration file, create an example scriptexample.py:importconfigargparserparser=configargparser.ConfigArgumentParser()parser.read("config.ini")parser.parse_args(shorts="sfb")print("Configs:",parser.defaults)print("Args:   ",parser.args)Create a configuration fileconfig.iniin the same directory:[DEFAULT]# Help message of the first argument. Help is optional.a_string='abc'a_float=1.23# inline comments are omitted# Help can span multiple lines.# This is another line.a_boolean=Falsean_integer=0Regular run,python example.py:$pythonexample.pyConfigs: {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}Args:    {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}Run with options, such aspython example.py -b -f 1:$pythonexample.py-b-f1Configs: {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}Args:    {'a_string': 'abc', 'a_float': 1.0, 'a_boolean': True, 'an_integer': 0}Case 3: create CLI from script itselfIf you used global variables, create a scriptexample.py, with the variables defined at top of file as below:# [DEFAULT]# Help message of the first argument. Help is optional.a_string="abc"a_float=1.23# inline comments are omitted# Help can span multiple lines.# This is another line.a_boolean=Falsean_integer=0# [END]importconfigargparserparser=configargparser.ConfigArgumentParser()parser.read_py("example.py")parser.parse_args(shorts="sfb")# update global variablesglobals().update(parser.args)print(a_string)print(a_float)print(a_boolean)print(an_integer)Use it as in case 1. For example,python example.py -b -f 1:$pythonexample.py-b-f1abc1.0True0Case 4: create CLI from a dataclass object (preferred)Suppose you have a scriptexample.pybelow, which uses adataclassobject to store arguments:fromdataclassesimportdataclass@dataclassclassArgs:# Help message of the first argument. Help is optional.a_string:str="abc"a_float:float=1.23# inline comments are omitted# Help can span multiple lines.# This is another line.a_boolean:bool=Falsean_integer:int=0args=Args()print(args.__dict__)Add these lines to the script to create CLI:importconfigargparserparser=configargparser.TypeArgumentParser()parser.parse_obj(args,shorts="sfb")print(args.__dict__)Use it as in case 1. For example,python example.py -b -f 1to change the values:$pythonexample.py-b-f1{'a_string': 'abc', 'a_float': 1.0, 'a_boolean': True, 'an_integer': 0}InstallationInstall from PyPI:python-mpipinstall--upgradepip
pipinstallconfig-argument-parserAlternatively, install from source:gitclonehttps://github.com/yuanx749/config-argument-parser.gitcdconfig-argument-parserthen install in development mode:gitcheckoutmain
python-mpipinstall--upgradepip
pipinstall-e.or:gitcheckoutdev
python-mpipinstall--upgradepip
pipinstall-e.[dev]pre-commitinstallUninstall:pipuninstallconfig-argument-parserNotesThis package usesSemantic Versioning.