reliure.utils.cli

copyright:
  1. 2013 - 2014 by Yannick Chudy, Emmanuel Navarro.
license:

${LICENSE}

Helper function to setup argparser from reliure components and engines

reliure.utils.cli.argument_from_option(parser, component, opt_name, prefix='')

Add an argparse argument to a parse from one option of one Optionable

>>> comp = Optionable()
>>> comp.add_option("num", Numeric(default=1, max=12, help="An exemple of option"))
>>> parser = argparse.ArgumentParser(prog="PROG")
>>> argument_from_option(parser, comp, "num")
>>> parser.print_help()
usage: PROG [-h] [--num NUM]

optional arguments:
  -h, --help  show this help message and exit
  --num NUM   An exemple of option
>>> parser.parse_args(["--num", "2"])
Namespace(num=2)
>>> parser.parse_args(["--num", "20"])          
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value ("20") is less than or equal to 12.']
reliure.utils.cli.arguments_from_optionable(parser, component, prefix='')

Add argparse arguments from all options of one Optionable

>>> # Let's build a dummy optionable component:
>>> comp = Optionable()
>>> comp.add_option("num", Numeric(default=1, max=12, help="An exemple of option"))
>>> comp.add_option("title", Text(help="The title of the title"))
>>> comp.add_option("ok", Boolean(help="is it ok ?", default=True))
>>> comp.add_option("cool", Boolean(help="is it cool ?", default=False))
>>>
>>> # one can then register all the options of this component to a arg parser
>>> parser = argparse.ArgumentParser(prog="PROG")
>>> arguments_from_optionable(parser, comp)
>>> parser.print_help()
usage: PROG [-h] [--num NUM] [--title TITLE] [--not-ok] [--cool]

optional arguments:
  -h, --help     show this help message and exit
  --num NUM      An exemple of option
  --title TITLE  The title of the title
  --not-ok       is it ok ?
  --cool         is it cool ?

The option values for a componant can then be retrieved with get_config_for()

>>> args = parser.parse_args() 
>>> config = get_config_for(args, comp)
>>> comp("input", **config) 
"comp_result"
reliure.utils.cli.get_config_for(args, component, prefix='')

Returns a dictionary of option value for a given component

See arguments_from_optionable() documentation