Package: dataparsers
dataparsersA simple module to wrap aroundargparseto get command line argument parsers fromdataclasses.InstallationpipinstalldataparsersBasic usageCreate adataclassdescribing your command line interface, and callparse()with the class:# prog.pyfromdataclassesimportdataclassfromdataparsersimportparse@dataclassclassArgs:foo:strbar:int=42args=parse(Args)print("Printing `args`:")print(args)Thedataclassfields that have a "default" value are turned into optional arguments, while the non default fields will
be positional arguments.The script can then be used in the same way as used withargparse:$pythonprog.py-h
usage:prog.py[-h][--barBAR]foo

positionalarguments:foo

options:-h,--helpshowthishelpmessageandexit--barBARAnd the resulting type ofargsisArgs(recognized by type checkers and autocompletes):$pythonprog.pytest--bar12Printing`args`:
Args(foo='test',bar=12)Argument specificationTo specify detailed information about each argument, call thearg()function on thedataclassfields:# prog.pyfromdataclassesimportdataclassfromdataparsersimportparse,arg@dataclassclassArgs:foo:str=arg(help="foo help")bar:int=arg(default=42,help="bar help")args=parse(Args)It allows to customize the interface:$pythonprog.py-h
usage:prog.py[-h][--barBAR]foo

positionalarguments:foofoohelpoptions:-h,--helpshowthishelpmessageandexit--barBARbarhelpIn general, thearg()function accepts all parameters that are used in the originaladd_argument()method (with few
exceptions) and some additional parameters. Thedefaultkeyword argument used above makes the argument optional
(i.e., passed with flags like--bar) except in some specific situations.For more information, see thedocumentation.Formalities, features, benefits and drawbacksThis project basically consists of a simple moduledataparsers.pywith few
functions that allows to define typed arguments parsers in a single place, based
ondataclasses.FormalitiesThe main strategy of the module is based on the same approach of thepackagedatargs, which consists in using
themetadataattribute of the dataclass fieldsto store argument parameters. Some additional features of this project have
already been contributed back upstream.There are a lot of alternative libraries out there that do similar things. TheREADME fileof thedatargsrepositoryprovides a goodlist for existing solutions and differences.
I could also add to that list the librariesPythonfireand thepackagedargparser, just to
give few examples.Features and benefitsUse this project if you want particular added features, such as:Support for argument groups and mutually exclusive argument groupsMore control over the interface displayMore control over the argument flag--creationMore simplicityThe simplicity is mentioned because it is just a simple moduledataparsers.pythat doesn't have any additional dependency (it is pure Python) which can be
downloaded directly and placed in your CLI scripts folder to import from.In deed, the module consists of a 200 linesIPython code cell region(i.e., starts and ends with a#%%line comment block), that can also be placed
on top of your "single file" CLI script to directly distribute. The used names
are just the fewprovided functions,
thestdlibimports andClass(aTypeVar).Additionally, this project also provides astub file (.pyi)that can be used by type checkers but, moreover, may be used by some code
editors to give helper documentation including the related docs ofargparsemethods, which are also provided in this project's documentation, for
convenience. The stub file can be downloaded directly but it is installed with
the module by default.DrawbacksUnlike thedatargspackage,dataparsersdoesn't support:The libraryattrs(only works with pure pythondataclasses)Enums classesComplex types (Sequences, Optionals, and Literals)Sub Commands (subparsers)If you want any of these features, use thepackagedatargs. If you need the added
features ofdataparsers, use this module instead.