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
Namespaceobject returned byparse_config(), the name of the attribute used for this config item will benameand must be a valid Python identifier.nameis 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, theargparseandsimple_argparsesources will convert underscores (_) to hyphens (-) and add a--prefix, so if a config item had the name"config_item1", theargparseandsimple_argparsesources 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 onargparseactions so theargparse documentationmay also provide useful information.store: this action just stores the highest priority value for config item.store_const: this stores the value specified in theconstargument.store_true: this stores the valueTrueand sets thedefaultargument toFalse. It is a special case ofstore_const.store_false: this stores the valueFalseand sets thedefaultargument toTrue. It is a special case ofstore_const.append: this creates alistcontaining 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 alistcontaining 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 thedefaultvalue into the final value for the config item even if the config item is mentioned in one of the sources (e.g.append,countandextend).Note that the default value for all config items can also be set by passing a value for the
config_defaultargument ofConfigParser. If both theconfig_defaultargument toConfigParserand thedefaultargument toadd_config()are used then only thedefaultargument toadd_config()is used.If a default value is not provided for the config item by the
defaultargument, theconfig_defaultargument or by the action class (like e.g. store_true does), then the final value for the config will beNoneif the config item is not mentioned in any source.The special value
SUPPRESScan be passed as thedefaultargument. In this case, if the config item is not mentioned in any source, it will not be given an attribute in theNamespaceobject returned byparse_config().dest(optional, keyword): the name of the attribute for the config item in theNamespaceobject returned byparse_config(). By default,destis set to the name of the config item (name).exclude_sources(optional, keyword): a collection of source names orSourceclasses that should ignore this config item. This argument is mutually exclusive withinclude_sources. If neitherexclude_sourcesnorinclude_sourcesis given, the config item will be looked for by all sources added to theConfigParser.include_sources(optional, keyword): a collection of source names orSourceclasses that should look for this config item. This argument is mutually exclusive withexclude_sources. If neitherexclude_sourcesnorinclude_sourcesis 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 toSUPPRESSto 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 thatnargscan 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_constorcount).- An
intN >= 1: the config item will takeNarguments and the value for a mention of the config item will be alistcontaining each argument. In particular, whennargs == 1the value for each mention of a config item will be alistcontaining 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’sconstargument."*": The config item will take zero or more arguments and the value for a mention of the config item will be alistcontaining each argument."+"The config item will take one or more arguments and the value for a mention of the config item will be alistcontaining 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 == 0ornargs == "+".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 likeintand functions that take a single argument. Note that some sources that read typed data may produce config item argument values that aren’t alwaysstrobjects.The default
typeisstrunless 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
requiredisFalse.choices: specifies a collection of valid values for the arguments of the config item. Ifchoicesis specified, an exception is raised if the config item is mentioned in a source with an argument that is not inchoices.