Package: argmagiq
arg-magiqargmagiqis a Python library that allows for parsing command-line args automagically.
To that end, all you have to do is to define a configuration class that contains a property for each of the user-defined
args, and letargmagiqtake care of parsing them for youInstallationThe packageargmagiqcan be installed via pip as follows:pipinstallgit+https://github.com/phohenecker/arg-magiqHow-ToStep 1: Define Your Config ClassEvery property (notattribute) of your configuration class that has both a getteranda setter method is
considered as an arg.
Please notice that all args are treated as options, i.e.,argmagiqdoes not generate positional args, and the name of
a considered propertymy_propertyis translated into an according option--my-property.
For example, the following code defines an arg--my-property:@propertydefmy_property(self)->str:"""str: This property defines an arg."""returnself._my_property@my_property.setterdefmy_property(self,my_property:str)->None:self._my_property=my_propertyAn important detail of this code snippet is thereturn-type annotationof the getter method, which allowsargmagiqto identify the data type of the according arg and sanitize values provided by the user.
At the moment, the typesbool,int,float, andstrare supported.
Notice that generic type-aliases are not allowed except fortyping.Optional[X], ifXis any of the supported types.
Finally, notice that the docstring of the getter method, if present, is printed as description of the arg in the help
text of the application.Notice:Just like Python'sargparsepackage,argmagiqprints an automatically generated help text, if either-hor--helpis provided.Step 2: LetargmagiqParse ArgsOnce you defined your config class, parsing args is as easy as importingargmagiqand running a single command:importargmagiqparsed_config=argmagiq.parse_args(YourConfigClass,app_name,app_description)In this code snippet,app_nameandapp_descriptionare two strings that define the name of your application, which
is used in its synopsis (the usage instruction at the beginning of the help), as well as a description of the same,
which is displayed as part of the help text.
The return value ofparse_argsis an instance ofYourConfigClassthat has been populated with the values provided
for the corresponding args.Notice:When the help text is printed (i.e., the user provided-hor--help), thenparse_argsreturnsNone.Default ValuesIf an arg has a default value, then this has to be specified as an attribute of the config class.
The name of such an attribute has to be an all-caps version of the according property prefixed withDEFAULT_.
For example, the following snippet defines a default value for the propertymy_property(which, as before, defines the
arg--my-property):classYourConfigClass(object):DEFAULT_MY_PROPERTY="blub"...By default, every arg without default value is considered as required, and an error will be raised, if the user does not
specify the same.
If you want to explicitly mark an arg without default value as optional, though, then you can annotate the
according getter method with@argmagiq.optional:@argmagiq.optional@propertydefmy_property(self)->str:...Reading Args From A JSON FileAs an alternative way of specifying args, which is particularly handy, if an applications requires a lot of
user-defined configuration,argmagiqallows for providing them as a JSON file as follows:$./your-app.py--/path/to/config.jsonIn this example, the file at path/path/to/config.jsonspecifies the args for the application.
Notice that JSON files have to be shallow dictionaries that describe key-value pairs using the same naming as the
configuration class.
For example:{"my_property":"some value",...}ExamplesFor full working examples, please have a look at theexamplesfolder.