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

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.parse_args([]).numbers == range(0, 100)


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()
args = argparser.parse_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.

Release History

This version
History Node


History Node


History Node


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
(2.1 kB) Copy SHA256 Hash SHA256
Source None Aug 8, 2015

Supported By

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 Google Google Cloud Servers DreamHost DreamHost Log Hosting