1. Tikitu de Jager
  2. argparse_config


Tikitu de Jager  committed ff9e130 Draft

updated readme for soon-to-exist functionality

  • Participants
  • Parent commits 0f9f72f
  • Branches default

Comments (0)

Files changed (1)

File argparse_config.src/README.rst

View file
 allow me to add defaults in the config file without writing more code.
 This is what ``argparse_config`` gives you. To use it with the mercurial client
+``arg_parser`` above::
-    >>> import ConfigParser # from the standard library
-    >>> config_parser = ConfigParser.SafeConfigParser()
-    >>> config_parser.read('/home/tikitu/.my_app.cfg')
-    ['/home/tikitu/.my_app.cfg']
-    >>>
     >>> import argparse_config
-    >>> argparse_config.set_defaults(arg_parser, config_parser) # using the arg_parser from above
+    >>> argparse_config.read_config(arg_parser, '/home/tikitu/.my_hg.cfg')
 ... and that's it. Calling ``arg_parser.parse_args()`` will parse args as usual,
 but the *default* values will be taken from the config file, if they are given
+    >>> parsed_args = arg_parser.parse_args(['merge'])
+    >>> parsed_args.tool
+    'meld'
+What can I put in the config file?
+Under the hood ``argparse_config`` uses the standard library ConfigParser_.
+Arguments that aren't for a subcommand go in the section ``[default]``. The
+names are munged from the commandline argument, removing leading dashes and
+converting internal dashes to underscores (e.g. ``--log-level`` becomes
+.. _ConfigParser: http://docs.python.org/2/library/configparser.html
+Flags (i.e. commandline args that take no parameters) are turned on if present
+in the config, just like the commandline::
+    [default]
+    verbose:
+is the equivalent of ``--verbose``.
 How does it work?
-By gudgeling about in the private internals of ``argparse``. Yes, it's not pretty.
+By gudgeling about in the private internals of ``argparse``. Yes, that's not
+Any required arguments that are present in a config file will show as optional,
+not required, in the ``--help`` output. (This is a bug-by-design, due to not
+having any clever idea about how to do it better.) It may help to tell
+yourself, "It's not required *on the commandline* because I gave it in the
+config file." (I will gladly make this dodgy rationalisation disappear if I
+figure out how to handle required arguments more tidily.)
 .. _issues list: http://bitbucket.org/tikitu/argparse_config/issues?status=new&status=open
 * A utility to output a config file, based on a set of commandline arguments.
-* Some tidier wrapping around the ``ConfigParser`` boilerplate, for the common use case.