Skip to main content

CircleCI codecov PyPI version PyPI format

Arseparse is a simple Python utility/micro framework for writing command line interfaces. It provides some functionality around argparse that covers some common scenarios.

Installation:

pip install arseparse

Basic usage:

from arseparse import Parser, Option


parser = Parser()


# register a command that returns the square of an int
parser.register('square', lambda value: value**2, [Option('value', type=int)])


# register a command that simply returns a string
parser.register('ping', lambda: 'pong')


# or with a decorator
@parser.register_dec([Option('value', type=int)])
def cube(value):
    return value**3


sys.exit(parser.run())

You can then point an application entrypoint to the script or execute the file: your-entrypoint.py square 2

So far so boring. A more common scenario is to have a config file as the first argument, parse it, create objects that the command depends on, and pass those alongside the parsed attributes. The root_options and bootstrap constructor args to Parser allow us to implement this:

from arseparse import Parser, Option
import myapp


# these are options that come before our main command
root_options = [Option('config', type=str, help='path to ini file')]


# this lets us process/modify the kwargs before we execute the callable.
# we can rely on attributes resulting from root_options to be set.
# here config gets replaced by three objects: settings, db_session and user_svc
def bootstrap(**kwargs):
    config_uri = kwargs.pop('config')
    settings = myapp.parse_app_config(config_uri)
    dbsession = myapp.get_sessionmaker(settings)()
    user_svc = myapp.UserService(dbsession)
    kwargs.update(dict(settings=settings, dbsession=dbsession, user_svc=user_svc))
    return kwargs


parser = Parser(root_options, bootstrap)


@parser.register_dec([Option('username', type=str), Option('secret', type=str)])
def create_user(username, secret, user_svc, **kwargs):
    user_svc.create_user(username, secret)


@parser.register_dec([Option('user_id', type=int)])
def ban_user(user_id, user_svc, **kwargs):
    user_svc.ban_user(user_id)


@parser.register_dec()
def print_settings(settings, **kwargs):
    print(settings)

You can now provide the path to a config file as the first argument: your-entrypoint.py config.ini ban_user 23

Another common requirement is to be able to jump into a shell where some objects have been preconfigured for us. Here’s a simple recipe for that:

@parser.register_dec()
def shell(**kwargs):
    import IPython
    IPython.embed(user_ns=kwargs)

Calling your-entrypoint.py config.ini shell will drop you in an ipython shell where dbsession, settings and user_svc are in scope.

Release files for arseparse 0.1.2

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

Source distribution (sdist)

Source distribution for arseparse 0.1.2
File Size Uploaded
arseparse-0.1.2.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for arseparse 0.1.2
File Interpreter ABI Platform
arseparse-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / arseparse-0.1.2.tar.gz

Download URL arseparse-0.1.2.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a8fbb67dcf6161ee589d07d756ba16dc1429343ead6ae8629a5a1c6193c08840
BLAKE2b-256 checksum
How to use checksums
50daa220527f5ab6f36bf13f2ed432d9c392ec2efb4d6b6afbcee27b6dcb4fe4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / arseparse-0.1.2-py3-none-any.whl

Download URL arseparse-0.1.2-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3993a1b2d5d3ad8df63c0abcd19e0998cfa93f8d33cd06173456ad9551bcf56
BLAKE2b-256 checksum
How to use checksums
7f96671768f9d8c1ade98984f9b7d17012d131d4fabf22ceb9a6056c237c5626
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1

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