Package: ruamel.appconfig
This module provides a way to easily add a config directory and config file to an
application. The config directory can be used for additional
user specific data files.By default it usesruamel.yamlfor reading and
writing the configuration files, as this library preserves any
comments added (which pythons ownConfigParserdiscards).The basic invocation is:from __future__ import print_function

import sys

from ruamel.appconfig import AppConfig

def to_stdout(*args):
    sys.stdout.write(' '.join(args))

ac = AppConfig(
    'appconfigbasic',  # name of config diretory, normally the application name
    warning=to_stdout,
)from __future__ import print_function

import sys

from ruamel.appconfig import AppConfig


def to_stdout(*args):
    sys.stdout.write(' '.join(args))


ac = AppConfig(
    'appconfigbasic',  # name of config directory, normally the application name
    warning=to_stdout,
)Resulting in:created directory /home/a114/.config/appconfigbasicBy default (on Linux/macOS) the config directory is created under~/.config, and the default config file would be~/.config/appconfigbasic/appconfigbasic.ini(theappconfigbasicbeing the value passed toAppConfig(). Before that it is
tested if that file already exists, or whether~/.appconfigbasic/appconfigbasic.inior~/.appconfigbasic.iniexists.On windows the config directory is to be$APPDATA/appconfigbasicInteraction with argparseAppConfig()has additional options to interact withargparseargument parsing:it is able to fill in the defaults for application options
(including those options for subparsers).it can add a--configoption to allow the user to specify an
alternative path for the config file.it can add a--save-defaultswith which the user can save values
for options specified on the commandline, to the config file. Global
options are stored in the[global]section of the configuration
file, options for subparsers in sections with the suparser name.Adding--configTo get this option added, pass in the parser and set
thefilenameparameter to theAppConfig.checkattribute:from __future__ import print_function

from argparse import ArgumentParser
from ruamel.appconfig import AppConfig

parser = ArgumentParser()

ac = AppConfig(
    'appconfigconfig',
    parser=parser,  # needed to set new options
    filename=AppConfig.check,  # sets --config option
)

parser.parse_args(['--help'])Resulting in:usage: addconfig.py [-h] [--config FILE]

optional arguments:
  -h, --help     show this help message and exit
  --config FILE  set FILE as configuration file
                 [~/.config/appconfigconfig/appconfigconfig.ini]Setting defaults and adding--save-defaultsTo add the saving the defaults to the config file:from __future__ import print_function

from argparse import ArgumentParser
from ruamel.appconfig import AppConfig

parser = ArgumentParser()

parser.add_argument('--delete', '-d', default='bla',
                    help='delete a file (default: %(default)s)')
parser.add_argument('--owner', default='itsme',
                    help='set owner (default: %(default)s)')
parser.add_argument('--force', action='store_true',
                    help='force action (default: %(default)s)')
ac = AppConfig(
    'addconfigsave',
    parser=parser,  # needed to set new options
    filename=AppConfig.check,  # sets --config option
    add_save=True,  # add save option
)
ac.set_defaults()  # set the ArgumentParser() defaults
# and save to config file
args = ac.parse_args(['--delete', 'two', '--save-defaults'])

with open(ac.get_file_name()) as fp:
    print(fp.read())Resulting in:[global]
delete = two
owner = itsme
force = FalseInteraction withruamel.std.argparsedecoratorsSince theruamel.std.argparsedecorator is a wrapper
around normal argparse usage, addingAppConfig()is easy:from __future__ import print_function

import sys
import os

from ruamel.std.argparse import ProgramBase, option, sub_parser, version, \
    SmartFormatter


class TestCmd(ProgramBase):
    def __init__(self):
        super(TestCmd, self).__init__(
            formatter_class=SmartFormatter
        )

    # you can put these on __init__, but subclassing TestCmd
    # will cause that to break
    @option('--quiet', '-q', help='suppress verbosity', action='store_true',
            global_option=True)
    @version('version: 1.2.3')
    def _pb_init(self):
        # special name for which attribs are included in help
        pass

    def run(self):
        if self._args.func:
            return self._args.func()

    def parse_args(self, *args):
        from ruamel.appconfig import AppConfig
        app = 'addconfigcomplex'
        # pre populate config file
        with open(os.path.expanduser(
            '~/.config/{}/{}.ini'.format(app, app)), 'w') as fp:
            fp.write('[readit]\nname=XYZ\n')
        self._config = AppConfig(
            app,
            parser=self._parser,  # self._parser set by ProgramBase
            filename=AppConfig.check,  # sets --config option
            add_save=True,  # add save option
        )
        self._config.set_defaults()
        self._parse_args(*args)

    @sub_parser(help='specific help for readit')
    @option('--name', default='abc',
            help='help for name (default: %(default)s)')
    def readit(self):
        print('calling readit')

    @sub_parser('writeit', help='help for writeit')
    @option('--target')
    def other_name(self):
        print('calling writeit')


n = TestCmd()
n.parse_args(['readit', '--help'])  # normaly no parameters -> sys.argv
n.run()  # never reachedWith outputs (please note the XYZ as default, because of the config
file being written before theAppConfiginstantiation):usage: complex.py readit [-h] [--name NAME] [--quiet] [--save-defaults]

optional arguments:
  -h, --help       show this help message and exit
  --name NAME      help for name (default: XYZ)
  --quiet, -q      suppress verbosity
  --save-defaults  save option values as defaults to config file