Skip to main content

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.

Metadata

Release files for blargparse 0.0.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 blargparse 0.0.3
File Size Uploaded
blargparse-0.0.3.tar.gz 2.1 kB Details

Release files / blargparse-0.0.3.tar.gz

Download URL blargparse-0.0.3.tar.gz
Size 2.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7592d9607d48242cff024c2d4da798e5ee27f65864709392b7690f6bd397611c
BLAKE2b-256 checksum
How to use checksums
bfb5b04ef8247a09fe8a1a6074c176e991b777b0a71af7ac54eb2d2ebdd2d1db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

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