Package: jduargs
jduargsSimple command line argument parser.Installation>pip(3)install(-U)jduargsandfromjduargsimportArgumentParserInstanciationparser=ArgumentParser(description="default string",epilog="default string")"description" and "epilog" are optional parameters. The provided strings will be respectively written at the beginning and at the end of the help provided -h or --help.Methodsdefadd(self,key:str,short:str,type:type=str,required:bool=True,description:str="",choices:list=[])... to add an expected argument. The parameters are:key: the name of the parametershort: the short version of the key, as a single caractertype: the parameter type classrequired: define if the argument is mandatory or not. If set to False and the parameter is not provided, the default value is set by the type constructordescription: explanation of what this parameter is used for. If no description is provided, an empty string is used insteadchoices: a list containing all possible values for this argument. If the passed value is not in the list, the program will stop with an errordeffrom_json(self,path:str)... to import the expected parameters from a json file. The dictionnary keys are the parameters name. For each key, it should contains the "short" and "type" keys as strings, and a required key as a boolean.defto_json(self,filename:str)... to export the parameter dictionnary to a json file.Note: both methods exists in the "yaml" variant.defcompile(self,args:List[str])->dict... to parse the provided argument list with respect to the defined parameters. It returns a dictionnary to access the different passed arguments values.UsageFirst create an instance of the parser:parser=ArgumentParser()Then add the expected arguments to parse:parser.add("file","f",description="file name without extension",choices=["file1","file2"])parser.add("path","p",required=False,description="path to database tree")Compile the parser with the input arguments provided from command line:arguments=parser.compile(sys.argv[1:])arguments is a dictionnary containing all parsed values. THe key is the name of the parameter given to the add() method.You can also access each parameters with the simple bracket operator, directly on the parser, after compiling it:file=parser["file"]path=parser["path"]Full exampleMain python code:importsysfromjduargs.parserimportArgumentParserparser=ArgumentParser(description="Example use of the argument parser.",epilog="Have fun !",)parser.add("file","f",description="file name w/o extension",choices=["file1","file2"])parser.add("path","p",required=False,description="path to main folder")results=parser.compile(sys.argv[1:])file=results["file"]//similartoparser["file"]path=results["path"]//similartoparser["path"]print(f"{file=}")print(f"{path=}")Script execution:$pythonmain.py
Togethelp,use-hor--helpcommandlineoptions.$pythonmain.py-h
usage:.\test.py-ffile[-ppath]Exampleuseoftheargumentparser.

positionalarguments:
-f:file<class'str'>filenamew/oextensionPossiblevaluesare['file1','file2'].

optionalarguments:
-p:path<class'str'>pathtomainfolder
-h,--helpshowthishelpmessageandexitHavefun!$python.\test.py-ffile3Providedfilenotin['file1','file2']$python.\test.py-ffile2file='file2'path=''