A very simple tool to parse command line string within it's options.
from xcmdparser import cmdparse
cmd = 'newsubscr <cid:int> <alias>[:<passwd>] [<description>]'
parsed = cmdparse(cmd, 'newsubscr 24 myalias:pass12345 A description of a new subscription item.')
print('Parsed fields: cid={cid}, alias={alias}, passwd={passwd}, description="{description}"'.format(**parsed))
The output.
Parsed fields: cid=24, alias=myalias, passwd=pass12345, description="A description of a new subscription item."
You may refer to a field as cid:int to parse it as integer or cid:float to parse it as float. Negative values are supported.
By default (without any qualifier) a field is str.
from xcmdparser import cmdparse
cmd = 'newsubscr <cid:float> <alias:slug>[:<passwd>] [<description>]'
parsed = cmdparse(cmd, 'newsubscr 24.0 new_subscr:12345 a new subscription', {'slug': r'[a-z\-]+'})
print('Parsed fields: cid={cid}, alias={alias}, passwd={passwd}, description="{description}"'.format(**parsed))
Note. User (custom) type qualifiers have a priority on the build-in int
and float
type qualifiers. E.g. you may, for instance, to make your own custom float
type qualifier.
CmdParseError.
It occurs when a format of the command does not correspond to a parsing string.CmdFormatError.
It occurs whenfmt
option ofcmdparse()
has a wrong format.CmdCustomTypeError.
It occurs when one or more custom_types option ofcmdparse()
are wrong. Also the object of thrown exception hascustom_types
field which stores keys of custom type qualifiers which regular expressions are wrong.
- 2.7
- 3.x
pip install https://github.com/sergzach/xcmdparser/archive/master.zip