Package: optio
optiois a simple, yet configurable command-line argument parser.DefinitionsLet us define entities recognized by the parser.short viewis a sequence of chars starting with-and followed by a letter,
e.g.-v.long viewis a sequence of chars starting with--followed by a letter
followed by a sequence (maybe empty) of alphanumeric chars, e.g.--v3rb0se.viewis either ashort vieworlong view. This implies a validviewstarts with-. Anyviewshall be associated with someoption.parameteris a string token followed after a view associated with an option.optionis a program abstraction used for parser configuration.plain argumentis any string token not beginning with-if delimiter has
not been encountered, or any string token after delimiter.delimiteris a--sequence, after which onlyplain argumentscould be
encountered.OptioParserOptioParseris a central parsing unit configurable by.add_option(..)method.
Only this class is imported uponfrom optio import *.Internally, parsing is divided into the following phases.Configure expected options by calling.add_option()with appropriate parameters.viewsis a set of acceptable short and long synonyms, e.g.-hor--help.
Two different options must not haveviewsin common, collision is reported
via exception.acceptoris afunctionaccepting gathered parameters, e.g.-h 1 2 3.acceptoris provided by the user of the library to ensure specific behavior.acceptorcould return any object or do any action, further interaction with
such object is a responsibility of the user.countis a tuple containing two items,parameters at leastandparameters at most.(0, 0)is a parameterless flag,(1, None)is an option with at least one parameter andwithoutupper bound.(2, 4)is an option with any number of parameters from interval[2, 3, 4].(None, None)is an option with unbounded parameter count, any number from0toInf.requiredis a flag signalizing if an option shall appear in the arguments.short_infois a concise description of an option.long_infois a long piece of text describing an option.classOptioParser:defadd_option(self,views:set[str]={},acceptor:function=lambdaid:id,count:tuple[int|None,int|None]=(1,None),required:bool=True,short_info:str='',long_info:str='')->OptioParser:Recognize views of the configured options and gather parameters into lists.
Gathering is eager towards option parameters. Consider option-cconfigured
with count(1, 2)and input-c 1 2 3. Only the last parameter will be
recognized as plain argument, other will be recognized as parameters of-c.
In other words, options consume as many parameters as it possibly could.Check if configured constraints are fulfilled (parameter count, required/found).Accept parameters by the default or custom acceptor. After this transformed
are converted into optionvalue.For the user, all four phases are hidden in.parse(..)call.Examples#!/bin/importsysfromoptioimport*parser=OptioParser()\.add_option({'-a'},count=(1,1))\.add_option({'-b'},count=(0,0))\.add_option({'-c'},count=(None,None))\.add_option({'-d'})\.add_option({'--file'})\.parse(sys.args[1:])./program -a 1 2 -bc 3 4 -d5 --file 1.txt 2.txt --file=3.txt -- -hConsidering parser and arguments mentioned above, the following entities are
recognized:-aoption with exactly one parameter1.-bparameterless (flag) option.-coption with unbound amount of parameters, currently3and4.-doption with at least one parameter, currently5.--filewith at least one parameter, currently[1.txt, 2.txt, 3.txt].plain arguments are represented by the list['2', '-h'].Other more detailed examples can be found atoptio/examples. To runprinter.py, clone the repository and executepython3 -m examples.printerfrom the project root folder.