Skip to content

A very simple tool to parse command line string within it's options.

Notifications You must be signed in to change notification settings

sergzach/xcmdparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What's xcmdparser?

A very simple tool to parse command line string within it's options.

An example

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."

The supported type qualifiers

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.

Custom type qualifiers (with regular expressions)

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.

Exceptions

  • CmdParseError. It occurs when a format of the command does not correspond to a parsing string.
  • CmdFormatError. It occurs when fmt option of cmdparse() has a wrong format.
  • CmdCustomTypeError. It occurs when one or more custom_types option of cmdparse() are wrong. Also the object of thrown exception has custom_types field which stores keys of custom type qualifiers which regular expressions are wrong.

The supported Python versions

  • 2.7
  • 3.x

How to install

pip install https://github.com/sergzach/xcmdparser/archive/master.zip

About

A very simple tool to parse command line string within it's options.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages