Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Combine arguments inside your blargument parser.

Project description

Blargparse extends argparse.ArgumentParser, adding an add_aggregate method that can help you separate your user interface logic from everything else.

How to use

Install from PyPI.

pip install blargparse

Here’s the simplest way to use it.

b = BlargParser()
b.add_argument('--left', '-l', type = int, default = 0)
b.add_argument('--right', 'r', type = int, default = 100)
b.add_aggregate('numbers', lambda args: range(args.left, args.right))

b.parse_args([]).numbers == range(0, 100)

You can get a bit fancier.

b = BlargParser()
b.add_argument('--left', '-l', type = int, default = 0)
b.add_argument('--right', 'r', type = int, default = 100)
def f(args):
    output = range(args.left, args.right)
    del(args.left, args.right)
b.add_aggregate(f)

b.parse_args([]).numbers == range(0, 100)

Why

I wrote Blargparse because I found myself doing stuff like this.

# Construct the parser.
a = argparse.ArgumentParser()
a.add_argument('--left', '-l', type = int, default = 0)
a.add_argument('--right', 'r', type = int, default = 100)

# Parse the arguments
import sys
ns = a.parse_args(sys.argv)

# Convert them into the form I want for my application.
numbers = range(ns.left, ns.right)

The input argument that made most sense to me while programming the thing was the numbers range, but I thought it was easiest if the end user could specify this range as a combination of --left and --right flags. And I might want to change that based on what users seem to prefer.

The above approach is fine because it’s small, but I had several arguments like this spread across several subparsers in different places, and the construction, parsing, and conversion of the arguments thus wound up spread across functions and even files.

With vanilla argparse I wound up doing stuff like this.

def _build_subparsers(argparser):
    'This adds subparsers to the parser.'
    subparsers = argparser.add_subparsers()
    # ...

def _apply_aggregates(args):
    args.range = range(args.left, args.right)
    del(args.left, args.right)

argparser = argparser.ArgumentParser()
_build_subparsers(argparser)
args = argparser.parse_args()
_apply_aggregates(args)

This is a bit better except that it groups subparsers together and aggregates together. I wanted subparsers to be grouped with relevant aggregates, and I did not want to group by whether something was a subparser or an aggregate; I think of the aggregates as components of the subparsers.

A more confusing alternative would have been to come up with my own non-argparse interface on top of this for specifying groupings of subparsers and aggregates.

Project details


Release history Release notifications

This version
History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
blargparse-0.0.3.tar.gz (2.1 kB) Copy SHA256 hash SHA256 Source None Aug 8, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page