This release is a pre-release and may not be stable for production use.
With argparse_config your commandline applications will understand config files automatically, based on the commandline arguments you specify.
Let’s say I’m reimplementing the Mercurial commandline client. I specify the commandline argument processing with argparse, of course:
arg_parser = ArgumentParser('hg')
arg_parser.add_argument('--repository')
sub_parsers = arg_parser.add_subparsers()
merge_parser = sub_parsers.add_parser('merge')
merge_parser.add_argument('--tool')
merge_parser.add_argument('--force', type='store_const', const=True, default=False)
commit_parser = sub_parsers.add_parser('commit')
commit_parser.add_argument('--user')
commit_parser.add_argument('--message')
When I go to use this client, though, I have to keep specifying my --user with every commit, and --tool with every merge. That sucks! What I want is to have my client understand a simple config file format:
[merge] tool: meld [commit] user: Tikitu de Jager <tikitu@logophile.org>
And obviously, as I add more arguments and subcommands to my client, it should 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 argparse_config >>> 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 there:
>>> 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 log_level:).
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, that’s not pretty.
Gotchas
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.)
Hacking
It’s on BitBucket. Feel free to play. It comes with a handy zc.buildout wrapper too, overkill though that clearly is.
TODO
It’s “alpha software” at present; likely to be buggy and lots of stuff ain’t there yet. Check the issues list to stay up to date. Some things I plan to add:
A utility to output a config file, based on a set of commandline arguments.
Release files for argparse_config 0.4a4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| argparse_config-0.4a4.tar.gz | 4.7 kB | Details |
Release files / argparse_config-0.4a4.tar.gz
| Download URL | argparse_config-0.4a4.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7deb420ee27fd4f419fb89234dbb3f5422676d6dd36fa761747bdc3c8518f7af
|
|
BLAKE2b-256 checksum How to use checksums |
c12239760aaf30f98e45cd84a9e3465a4f9fed7eab1ab5fd88a8d365118c2a07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |