Argparse integration

Dotconf provide an optionnal integration with the standard argparse module. This compatibility brings you a way to override some configuration values using a command line argument.

Define the schema

Each compatible Container can take five optionnals arguments:

  • argparse_names: the list of argument names or flags, the usage of this argument enable feature for the container.
  • argparse_metavar: the metavar value of the argument (read the argparse documentation for more informations).
  • argparse_help: the help to display.
  • argparse_names_invert: only for a flag value, create an argument to invert the boolean value (eg: for a --daemon argument, you can create a --foreground value which will force to disable the daemonization).
  • argparse_help_invert: help message for the invert argument.

Example:

debug = Value(Boolean, argparse_names=['-d', '--debug'],
              argparse_help='enable the debug mode')

Populate the argument parser and use it

Once your schema is defined, you must call the populate_argparse() method providing the argument parser to populate:

parser = argparse.ArgumentParser()
schema = MySchema()
schema.populate_argparse(parser)
args = parser.parse_args()
config = Dotconf(conf, schema=schema)
my_config = config.parse()

Table Of Contents

Related Topics

This Page