Skip to main content

Use decorators for dealing with argparse

Project description

Installation

pip install decparse

Usage

Before with argeparse

import argparse

parser = argparse.ArgumentParser(
    prog='foo',
    description='Print bar',
    add_help=True
)
parser.add_argument(
    '--baz',
    action='store_true'
    help='Also print baz'
)
args = parser.parse_arguments()

print('bar')
if args.baz:
    print('baz')

Using decopts instead

from decopts import entrypoint, option

@entrypoint(
    prog='foo',
    description='Print bar',
    add_help=True
)
@option(
    '--baz',
    action='store_true'
    help='Also print baz'
)
def main():
    print('bar')
    if main.args.baz:
        print('baz')

main()

Adding subcommands

from decopts import entrypoint, option, action

@entrypoint(
    prog='foo',
    description='Print something',
    add_help=True
)
def main():
    return

@action(
    main,
    'foo',
    description='Print foo'
)
def foo():
    print('foo')

@action(
    main,
    'baz',
    description='Print baz'
)
def baz():
    print('baz')

main()

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

decopts-1.0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

decopts-1.0.2-py2.py3-none-any.whl (3.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file decopts-1.0.2.tar.gz.

File metadata

  • Download URL: decopts-1.0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for decopts-1.0.2.tar.gz
Algorithm Hash digest
SHA256 2bfab155021adf9373c6b465afc3f1bff6b027c43634684ed3547fbe6e70135d
MD5 fb6df47d122b6d0890e4d1e2253bc911
BLAKE2b-256 91f9702abcaff841dfc03e52913cc2caad6b6e55fec32a98f16bffc7d386939a

See more details on using hashes here.

File details

Details for the file decopts-1.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for decopts-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3fd566d06ce3c74495b3817c8401a5ebe857f2fed8080644016e04b2eff2ea6f
MD5 a228979375882a9228e0d5931facbc3a
BLAKE2b-256 091639e6cd69cc990d224887706da1425bd38cc30f6e66170ca473f6b58a8bcf

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