Skip to main content

Make CMD arg-parser easy and simple.

Project description

autoargparse

Make CMD arg-parser easy and simple.

dependency

  • python can import argparse

How to use

  1. pip install autoargparse

  2. import autoargparse

  3. Initialize :
    cmd = autoargparse.cmd()

  4. Use functon decorator :

    @cmd.args(short_name, long_name, nargs, help, action, type)
    set function argument as args type

    @cmd.kwargs(arg_name=short_name, nargs, help, action, type)
    set function argument as kwargs type

    @cmd.mark(run_order=None)
    set function run order(integer, bigger first run)

  5. Run: cmd.run()

example

#!coding:utf-8

import autoargparse

cmd = autoargparse.cmd('simple calculator for example.')

@cmd.mark(1)
@cmd.kwargs(ne='--negative', action='store_true')
@cmd.kwargs(ma='--max', action='store_true')
@cmd.args('-s', '--sum', '*', type=int)
def add(*args, ma, ne):
    if args or ma or ne:
        d = 0
        if ma:
            d = max(args)
        else:
            d = sum(args)
        if ne:
            d = -d
        print(d)

@cmd.mark(2)
@cmd.args('-v', action='count')
@cmd.args('-m', '--mul', 2, help='MUL!!!', type=int)
def mul(a,b,v=None):
    if not v:
        print(a*b)
    elif v==1:
        print('a * b =', a*b)
    elif v==2:
        print('func =', mul.__name__, 'args =', a, b, v)
        print('a * b =', a*b)

@cmd.mark(3)
@cmd.args('g', type=int, help='print great')
def great(a):
    if a==1:
        print('!!!')
    else:
        print('Great !!!')



if __name__ == "__main__":
    cmd.run()

OUTPUT

python .\example.py -h

usage: test.py [-h] [-s [SUM [SUM ...]]] [--max] [--negative] [-m MUL MUL]
               [-v]
               g

simple calculator for test.

positional arguments:
  g                     print great

optional arguments:
  -h, --help            show this help message and exit
  -s [SUM [SUM ...]], --sum [SUM [SUM ...]]
  --max
  --negative
  -m MUL MUL, --mul MUL MUL
                        MUL!!!
  -v

python .\example.py 1

!!!

python .\example.py 2

Great !!!

python .\example.py 1 -m 2 3

!!!
6

python .\example.py 1 -m 2 3 -v

!!!
a * b = 6

python .\example.py 1 -m 2 3 -vv

!!!
func = mul args = 2 3 2
a * b = 6

python .\example.py 1 -s 1 2 3 4

!!!
10

python .\example.py 1 -s 1 2 3 4 --max

!!!
4

python .\example.py 1 -s 1 2 3 4 --negative

!!!
-10

TODO

  • add GUI
  • more easy to use

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

autoargparse-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

autoargparse-0.1.0-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file autoargparse-0.1.0.tar.gz.

File metadata

  • Download URL: autoargparse-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for autoargparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c872d447f522cc7aa1897527def364439fec583b13d5b74ee8a295ad7944e7e
MD5 90955837d08c748b2458f8a58312ed05
BLAKE2b-256 9d3c95efdd61bcb899bef52728f798defc7928a7e0426153a19033e1f6931a52

See more details on using hashes here.

File details

Details for the file autoargparse-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: autoargparse-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for autoargparse-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 be3832f51d48b9d9ff32da0baeab5c6b8136974abfd68ffd5117315fabed2fe0
MD5 0a4bd2ba37bef3abe308768a0418f427
BLAKE2b-256 a7d077169aca1c9af273c40da65d7c6951de195771a9571a32e59c805af80415

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