Skip to main content
https://travis-ci.org/eclarke/argutils.svg?branch=v0.2.0 https://coveralls.io/repos/eclarke/argutils/badge.svg?branch=v0.2.0&service=github Documentation Status https://badge.fury.io/py/argutils.svg

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.

Installation

$ 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, example.py.

In example_spec.yml:

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

In example.py:

try:
    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 = 'example.py'

    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
    if config.read(CONF_FILE):
        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__':
    main()

Let’s see what we’ve got:

$ python example.py --help
usage: example.py [-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 example.py --init
$ cat example.cfg
## A program that prints a message some number of times to an output file
[example.py]
# 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 example.py
Hello world!
Hello world!
Hello world!
$ python example.py --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 files for argutils 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argutils 0.3.3
File Size Uploaded
argutils-0.3.3.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argutils 0.3.3
File Interpreter ABI Platform
argutils-0.3.3-py2-none-any.whl Python 2 none any Details

Total release size: 13.3 kB

Release files / argutils-0.3.3.tar.gz

Download URL argutils-0.3.3.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3152e6e602847bbcc8868b3adc1284d6008c3dda4bb9b366d7c60778a2538894
BLAKE2b-256 checksum
How to use checksums
8e7f51286ee6cb8e1c223aa05c2a686f8ef00139bf91969f3109ec8c0d84e67d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

Release files / argutils-0.3.3-py2-none-any.whl

Download URL argutils-0.3.3-py2-none-any.whl
Size 6.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
0ad56e788c2436ee77189f37d72cf22014f209cb40af88a1299ad122e1808421
BLAKE2b-256 checksum
How to use checksums
3419e609917f313d2513b6918edeff17ae952ef0c7b170f9506c1d0072a8a4d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page