Parsing command line argumentsΒΆ

Before reading this section, please make sure you have read the Extending qiBuild actions tutorial.

Briefly, you should create a file named looking like:

"""Add some eggs !"""

import argparse
import logging
import qibuild

LOGGER = logging.getLogger(__name__)

def configure_parser(parser):
    """Configure parser for this action """
      help="Number of eggs to add",

def do(args):
  """Main entry point""""adding %i eggs", args.num_eggs)

Now lets have a look of what happens when you type:

$ qibuild spam --num-eggs=42

You first go through qibuild script, in bin/qibuild

You will see it uses :

modules = qibuild.cmdparse.action_modules_from_package("qibuild.actions")
qibuild.cmdparse.root_command_main("qibuild", parser, modules)

The first line will look for every Python module in the qibuild.actions package that contains a do() and a configure_args methods.

The second line will do the main parsing.

Note that the last argument is simply a list of modules.

So if you ever wanted to add an action outside qibuild.actions package, you could do:

import spam
qibuild.cmdparse.root_command_main("qibuild", parser, modules + [spam])

So what does the root_command_main do?

You can see it taks a parser object as arument.

You should call this function with an argparse.ArgumentParser object.

The parser will then be updated.

parser = argparse.ArgumentParser()
qibuild.cmdparse.root_command_main("qibuild", parser)

Basically, we will call:

subparsers = parser.add_subparsers(dest="action", title="actions")
action_parser = subparsers.add_parser("spam")

for each module in the list.

Note how we format the help looking using module.__doc__

This means that contains everything to handle the parsing:

  • The documentation of the action is simply the docstring of the module
  • Specific arguments are added using the configure_parser function of the module

Thus, everything is put in one place, and the --help output is alwasy correct.