Example

Usage

$ python example.py -h
usage: example.py [-h] [-r] {math,echo} ...

Argon example.

positional arguments:
  {math,echo}

optional arguments:
  -h, --help     show this help message and exit
  -r, --reverse  reverse output

Math Sub-Command Group

$ python example.py math -h
usage: example.py math [-h] {sum,hex} ...

positional arguments:
  {sum,hex}
    hex       convert number to hex

optional arguments:
  -h, --help  show this help message and exit
$ python example.py math sum 1 2 3
6
$ python example.py math hex 1234567
0x12d687

Echo Sub-Command

$ python example.py echo -h
usage: example.py echo [-h] [-u] string

positional arguments:
  string

optional arguments:
  -h, --help  show this help message and exit
  -u          convert to uppercase
$ python example.py echo fooo
fooo
$ python example.py -r echo -u fooo
OOOF

Source Code

import sys

import argon


def main():
    app = argon.App(description="Argon example.")

    # If given, the output is reversed.
    app.arg("-r", "--reverse", default=False, action="store_true",
            help="reverse output")

    # Add math sub-commands. "sum" computes the sum args and "hex" converts
    # an integer to hex.
    with app.sub("math") as math:
        with math.command("sum") as math_sum:
            math_sum.arg("numbers", help="numbers to compute sum of",
                         nargs="+", type=int)
            math_sum.handler(do_sum)
        with math.command("hex", help="convert number to hex") as math_hex:
            math_hex.arg("number", type=int).handler(do_hex)

    # Add an echo command. It accepts an argument to uppercase its input.
    with app.command("echo") as echo:
        echo.arg("-u", dest="uppercase", default=False, action="store_true",
                 help="convert to uppercase") \
            .arg("string") \
            .handler(do_echo)

    app.run(sys.argv[1:])


def show(func):
    """Decorator to print the return value of func, possibly reversed. """
    def wrapper(args):
        output = str(func(args))
        if args.reverse:
            print "".join(reversed(output))
        else:
            print output
    return wrapper


@show
def do_sum(args):
    return sum(args.numbers)


@show
def do_hex(args):
    return hex(args.number)


@show
def do_echo(args):
    if args.uppercase:
        return args.string.upper()
    return args.string


if __name__ == "__main__":
    main()

Table Of Contents

Related Topics

This Page