Skip to main content

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


Download files

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

Source Distribution

blargparse-0.0.3.tar.gz (2.1 kB view details)

Uploaded Source

File details

Details for the file blargparse-0.0.3.tar.gz.

File metadata

  • Download URL: blargparse-0.0.3.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for blargparse-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7592d9607d48242cff024c2d4da798e5ee27f65864709392b7690f6bd397611c
MD5 e10d5653da84cdf70bce342f2c7887d7
BLAKE2b-256 bfb5b04ef8247a09fe8a1a6074c176e991b777b0a71af7ac54eb2d2ebdd2d1db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page