Skip to main content

A lightweight verbose argument parser

Project description

Amersham

Test Coverage

Amersham: The name of a London Underground station which alliterates with "argument parser"

Motivation

Every CLI application wants two things:

  1. Vetted user input
  2. Descriptive usage and help messages

Amersham takes care of both with minimal boilerplate.

Installation

user:app$ git clone git@github.com:inigo-selwood/amersham.git

Quick Start

Import

import sys

from amersham import Parser, Flag, Parameter

Create a parser

parser = Parser("parser", "a parser")

Create a command

@parser.command("a command",
        flag = Flag("a flag"),
        parameter = Parameter("a parameter))
def command(parameter: str, flag: str):
    print(parameter, flag)

Why the duplication? The same could be acheived with type annotations

Take away the decorator, and it's just a normal function. The signature stays descriptive; changing parser is painless.


Run the parser

if __name__ == "__main__":
    parser.run(sys.argv[1:])

Help

Amersham generates descriptive help messages, so you don't have to.

For the application:

user:app$ python3 app --help
usage
  app [--help] {command} ...

description
  a parser

commands
  command  a command

And for each command:

user:app$ python3 app command --help
usage
  app command [--help] {command} ...

description
  a command

flags
  --help  -h  displays this message
  --flag  -f  a flag

parameters
  parameter  a parameter

Overriding Defaults

Amersham infers the names of your commands, arguments, and flag aliases. You can override that behaviour should you want to.

Rename a command:

@parser.command("a command", name="a-different-name")
def command():
    pass

Or a flag/parameter:

@parser.command("a command",
        flag = Flag("a flag", name="flag-name"),
        parameter = Parameter("a parameter, name="parameter-name"))
def command(parameter: str, flag: str):
    print(parameter, flag)

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

amersham-0.0.1.tar.gz (8.8 kB view hashes)

Uploaded Source

Built Distribution

amersham-0.0.1-py3-none-any.whl (9.1 kB view hashes)

Uploaded Python 3

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