Package command_system :: Module mapping
[hide private]
[frames] | no frames]

Module mapping

source code

The module contains routines to parse command arguments and map them to the command handler's positonal and keyword arguments.

Mapping is done in two stages: 1) parse arguments into positional arguments and options; 2) adapt them to the specific command handler according to the command properties.

Functions [hide private]
 
parse_arguments(arguments)
Simple yet effective and sufficient in most cases parser which parses command arguments and returns them as two lists.
source code
 
adapt_arguments(command, arguments, args, opts)
Adapt args and opts got from the parser to a specific handler by means of arguments specified on command definition.
source code
 
generate_usage(command, complete=True)
Extract handler's arguments specification and wrap them in a human-readable format usage information.
source code
Variables [hide private]
  ARG_PATTERN = re.compile(r'([\'"])?(?P<body>(?:))(?:)')
  OPT_PATTERN = re.compile(r'(?<!\w)--?(?P<key>[\w-]+)(?:(?:=|\s...
  KEY_ENCODING = 'UTF-8'
  USAGE_PATTERN = 'Usage: %s %s'

Imports: re, BooleanType, UnicodeType, itemgetter, DefinitionError, CommandError


Function Details [hide private]

parse_arguments(arguments)

source code 

Simple yet effective and sufficient in most cases parser which parses command arguments and returns them as two lists.

First list represents positional arguments as (argument, position), and second representing options as (key, value, position) tuples, where position is a (start, end) span tuple of where it was found in the string.

Options may be given in --long or -short format. As --option=value or --option value or -option value. Keys without values will get None as value.

Arguments and option values that contain spaces may be given as 'one two three' or "one two three"; that is between single or double quotes.

adapt_arguments(command, arguments, args, opts)

source code 

Adapt args and opts got from the parser to a specific handler by means of arguments specified on command definition. That is transform them to *args and **kwargs suitable for passing to a command handler.

Dashes (-) in the option names will be converted to underscores. So you can map --one-more-option to a one_more_option=None.

If the initial value of a keyword argument is a boolean (False in most cases) - then this option will be treated as a switch, that is an option which does not take an argument. If a switch is followed by an argument - then this argument will be treated just like a normal positional argument.

generate_usage(command, complete=True)

source code 
Extract handler's arguments specification and wrap them in a human-readable format usage information. If complete is given - then USAGE_PATTERN will be used to render the specification completly.

Variables Details [hide private]

ARG_PATTERN

Value:
re.compile(r'([\'"])?(?P<body>(?:))(?:)')

OPT_PATTERN

Value:
re.compile(r'(?<!\w)--?(?P<key>[\w-]+)(?:(?:=|\s)([\'"])?(?P<value>(?:\
))(?:))?')

KEY_ENCODING

Value:
'UTF-8'

USAGE_PATTERN

Value:
'Usage: %s %s'