Skip to main content

argparse wrapper around decorations

Project description

argparse_decoration

A argparse wrapper through decorations

30 sec tutorial

Given test.py with:

#!/bin/python3

from argparse_decorations import init, Command, parse_and_run

init()

@Command('mycommand')
def handler():
    print('Hello!')

parse_and_run()

then ./test.py produces:

usage: test.py [-h] {mycommand} ...

Help message

positional arguments:
  {mycommand}

options:
  -h, --help   show this help message and exit

and ./test.py mycommand:

Hello!

1 min tutorial

The program:

from argparse_decorations import init, Command, SubCommand, Argument, parse_and_run

init()

@Command('mycommand')
@SubCommand('add')
@Argument('a', type=int)
@Argument('b', type=int)
def add_handler_but_could_be_any_identifier(a, b):
    print(a + b)

parse_and_run()

when called ./test.py mycommand add 1 2 produces:

3

@Command and @SubCommand will make a call to ArgumentParser.add_parser() and @Argument will call ArgumentParser.add_argument These calls are made 'as-is' passing every *args and **kwargs passed on decorations to commands

A complete example whose explore all possibilities:

@Command('math')
@SubCommand('add')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def add_handler(valueA, valueB):
    print('Adding args ' + str(valueA) + ' and ' + str(valueB))
    print(valueA + valueB)


@Command('math')
@SubCommand('sub')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def subtract_handler(valueA, valueB):
    print('Subtracting args ' + str(valueA) + ' and ' + str(valueB))
    print(valueA - valueB)


@Command('math')
@SubCommand('mul')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def subtract_handler(valueA, valueB):
    print('Multiplying args ' + str(valueA) + ' and ' + str(valueB))
    print(valueA * valueB)


@Command('math')
@SubCommand('pi')
def pi():
    print('3.1415926535979')

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

argparse_decorations-1.1.0.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file argparse_decorations-1.1.0.tar.gz.

File metadata

  • Download URL: argparse_decorations-1.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for argparse_decorations-1.1.0.tar.gz
Algorithm Hash digest
SHA256 551f3b812dfa45f1e19487f1894bc25cd382e6315adb402b4d3baad617a5d9b0
MD5 1ff2ed07abc890990d667f864e61deec
BLAKE2b-256 7014e770a8bd0d329047387a8fda79fe20d32132fc55b0e1939dee26c6e6b14b

See more details on using hashes here.

Supported by

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