Adding config items¶
Once you have created your ConfigParser
object, you can add
specifications of your config items using the
ConfigParser.add_config()
:
-
ConfigParser.
add_config
(name, **kwargs) Add a config item to the
ConfigParser
.The arguments that apply to all config items are:
name
(required, positional): the name of the config item.In the
Namespace
object returned byparse_config()
, the name of the attribute used for this config item will bename
and must be a valid Python identifier.name
is also used by source Source classes to generate the strings that will be used to find the config in config sources. The Source classes may, use a modified version ofname
, however. For example, theargparse
andsimple_argparse
sources will convert underscores (_
) to hyphens (-
) and add a--
prefix, so if a config item had the name"config_item1"
, theargparse
andsimple_argparse
sources would use the option string"--config-item1"
.action
(optional, keyword): the name of the action that should be performed when a config item is found in a config source. The default action is"store"
, and the built-in actions are described briefly below. See Actions for more detailed information about the built-in actions and creating your own actions. The built-in actions are all based onargparse
actions so theargparse documentation
may also provide useful information.store
: this action just stores the highest priority value for config item.store_const
: this stores the value specified in theconst
argument.store_true
: this stores the valueTrue
and sets thedefault
argument toFalse
. It is a special case ofstore_const
.store_false
: this stores the valueFalse
and sets thedefault
argument toTrue
. It is a special case ofstore_const
.append
: this creates alist
containing every value seen (with lower priority values first). Whennargs >= 1
,nargs == "+"
ornargs == "*"
, each value in the list s iteself a list containing the arguments for a mention of the config item.count
: this stores the number of mentions of the config item.extend
: this creates alist
containing every value seen (with lower priority values first). Unlikeappend
, whennargs >= 1
,nargs == "+"
ornargs == "*"
, arguments for mentions of the config item are not placed in separate sublists for each mention.
default
: the default value for this config item. Note that some actions will incorporate thedefault
value into the final value for the config item even if the config item is mentioned in one of the sources (e.g.append
,count
andextend
).Note that the default value for all config items can also be set by passing a value for the
config_default
argument ofConfigParser
. If both theconfig_default
argument toConfigParser
and thedefault
argument toadd_config()
are used then only thedefault
argument toadd_config()
is used.If a default value is not provided for the config item by the
default
argument, theconfig_default
argument or by the action class (like e.g. store_true does), then the final value for the config will beNone
if the config item is not mentioned in any source.The special value
SUPPRESS
can be passed as thedefault
argument. In this case, if the config item is not mentioned in any source, it will not be given an attribute in theNamespace
object returned byparse_config()
.dest
(optional, keyword): the name of the attribute for the config item in theNamespace
object returned byparse_config()
. By default,dest
is set to the name of the config item (name
).exclude_sources
(optional, keyword): a collection of source names orSource
classes that should ignore this config item. This argument is mutually exclusive withinclude_sources
. If neitherexclude_sources
norinclude_sources
is given, the config item will be looked for by all sources added to theConfigParser
.include_sources
(optional, keyword): a collection of source names orSource
classes that should look for this config item. This argument is mutually exclusive withexclude_sources
. If neitherexclude_sources
norinclude_sources
is given, the config item will be looked for by all sources added to theConfigParser
.help
: the help text/description for the config item. Set this toSUPPRESS
to prevent this config item from being mentioned in generated documentation.
The other arguments are all keyword arguments and are passed on to the class that implements the config items action and may have different default values or may not even be valid for all actions. See Actions for action specific documentation.
nargs
: specifies the number of arguments that the config item accepts. The values thatnargs
can take are:None
: the config item will take a single argument.0
: the config item will take no arguments. This value is usually not given toadd_config()
but may be implicit for an action (e.g.store_const
orcount
).- An
int
N >= 1
: the config item will takeN
arguments and the value for a mention of the config item will be alist
containing each argument. In particular, whennargs == 1
the value for each mention of a config item will be alist
containing a single element. "?"
: The config item will take a single optional argument. When the config item is mentioned without an accompanying value, the value for the mention is the value of the config item’sconst
argument."*"
: The config item will take zero or more arguments and the value for a mention of the config item will be alist
containing each argument."+"
The config item will take one or more arguments and the value for a mention of the config item will be alist
containing each argument.
const
: The value to use for a mention of the config item where there is no accompanying argument. This is only ever used whennargs == 0
ornargs == "+"
.type
: The type to which each argument of the config item should be converted. This can be any callable object that takes a single argument (an object with a__call__(self, arg)
method), including classes likeint
and functions that take a single argument. Note that some sources that read typed data may produce config item argument values that aren’t alwaysstr
objects.The default
type
isstr
unless that doesn’t make sense (e.g. whennargs == 0
.required
: specifies whether an exception should be raised if a value for this config item cannot be found in any source.The default
required
isFalse
.choices
: specifies a collection of valid values for the arguments of the config item. Ifchoices
is specified, an exception is raised if the config item is mentioned in a source with an argument that is not inchoices
.