This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Functions to build matched argument parsers and config files

Project Description

argutils provides a set of functions for quickly building command-line programs with matching config files. In particular, instead of separately building an ArgumentParser and ConfigParser, argutils lets the user build an interface from a JSON or YAML file, and then uses that to build both an argument parser and matching config file.


$ pip install argutils

Usage example

Let’s say we have a toy program that takes three arguments: a message to print, the number of times to print it, and where to print it. We have two files, an argument spec file we’ll call example_spec.yml, and our program,

In example_spec.yml:

  help: >
    A program that prints a message some number of times to an output
  help: the message to print
  default: "Hello world!"
  help: how many times to print the message
  default: 3
  type: int
  help: where to write the file
  _exclude: True
  default: stdout
  type: File-w
  help: write a config file with default values
  _exclude: True
  argtype: flag


    import ConfigParser
except ImportError:
    import configparser as ConfigParser
import argutils
from argutils import (read, export)

SPEC_FILE = 'example_spec.yml'
CONF_FILE = 'example.cfg'

def main():
    # Used in the config file and argument parser's help
    prog_name = ''

    config = ConfigParser.SafeConfigParser()

    # Read the spec and build a parser from it
    argsdict = read.from_yaml(open(SPEC_FILE).read())
    parser = export.to_argparser(prog_name, argsdict)

    # If the config file exists and we can read it, use it to set the
    # defaults
        parser = argutils.set_parser_defaults(parser, config)

    args = parser.parse_args()

    if args.init:
        export.to_config_file(prog_name, argsdict, CONF_FILE)

    for _ in range(args.times):
        args.output.write(args.message + '\n')

if __name__ == '__main__':

Let’s see what we’ve got:

$ python --help
usage: [-h] [--message MESSAGE] [--times TIMES]
                     [--output OUTPUT] [--init]

A program that prints a message some number of times to an output file

optional arguments:
  -h, --help         show this help message and exit
  --message MESSAGE  the message to print
  --times TIMES      how many times to print the message
  --output OUTPUT    where to write the file
  --init             write a config file with default values

We can see that all the arguments we specified in the YAML file are here. Let’s write a config file and check that out:

$ python --init
$ cat example.cfg
## A program that prints a message some number of times to an output file
# the message to print
message = Hello world!
# how many times to print the message
times = 3

Note that two arguments don’t show up here: output and init. These were excluded using the _exclude flag in the YAML file. This is useful for arguments that shouldn’t be set using a config file, including one-time arguments.

Let’s test it:

$ python
Hello world!
Hello world!
Hello world!
$ python --times 1
Hello world!

We can specify the arguments either with command-line flags or by modifying the values in the config file. Values specified on the command line take precedence, followed by the config file values, and resorting to the spec file defaults if nothing else is given.

Release History

Release History

This version
History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
argutils-0.3.2-py2-none-any.whl (9.2 kB) Copy SHA256 Checksum SHA256 py2 Wheel Nov 13, 2015
argutils-0.3.2.tar.gz (6.3 kB) Copy SHA256 Checksum SHA256 Source Nov 13, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting