Skip to main content

CLI Argument Parser

Project description

SysArg

CLI Argument Parser Functions

Contents

  1. Install
  2. Initialize
  3. Functions
    1. defind : Setup option/command/group/help....
    2. Cmd : Get Inputed Special Command (it need cmd_id=1 parameter at initialize)
    3. Get : Get parameter's input values
    4. Version : show version
    5. Check : Check option
    6. Help : Dispaly Help
  • Install
pip3 install sysarg
  • Initialize
    • program : Program Name
    • cmd_id : command ID
    • desc : Program Description
    • epilog : Program Epilog (tail string of the help)
    • version : define Program version
    • help_desc: Customize description of Help
import SysArg
arg=SysArg.SysArg(program='ArgTest',desc='ARG. Testing',version='1.0')

if you want make a special command in this application case: (ArgTest command ...)

import SysArg
arg=SysArg.SysArg(program='ArgTest',desc='ARG. Testing',version='1.0',cmd_id=1)
  • defind
    • name: (required) parameter name
    • short: short parameter(-XX)
    • long: long parameter(--XX)
    • params:
      • default:0(found parameter then True),
      • 0<n: how many required parameter input data(number)
      • - : until next parameter or until finished inputs
    • params_name: --abc=<PARAMS_NAME(Explain String)> (this format)
    • required: required parameter (default False, need then True)
    • default: default value when no input
    • type: default str, wrong type then not taken (support: str,int,list,tuple,dict)
      • spliter: if you set list or tuple at type. but input is single string then using spliter (example: , : )
    • group: make a special group's prameters
    • group_desc: group desciption
    • command: If Command Group then True
    • arg: If this group is command and need input argument then True
    • select: If you want selectable input, this is list type format, if wrong input then Get() command will make an error

example format: -n, --numbre=INT

arg.define('number',short='-n',long='--number',params_name='INT',type=int,desc='Number Input')

it this option required value

arg.define('number',short='-n',long='--number',params_name='INT',type=int,desc='Number Input',required=True)

Add some of GROUP(ex: INPUT)

arg.define('number',short='-n',long='--number',params_name='INT',type=int,desc='Number Input',group='INPUT')

example format: -n, --numbre INT

arg.define('number',short='-n',long='--number',params=1,type=int,desc='Number Input')

example format: -l, --list ['A','B']

arg.define('List',short='-l',long='--list',params=1,type=list,desc='List Input')

example format: -l, --list 'A,B,C'

arg.define('List',short='-l',long='--list',params=1,type=list,spliter=',',desc='List Input')

example format: -l, --list A B C

arg.define('List',short='-l',long='--list',params=3,type=list,desc='List Input')

example default : if not input then default get 7

arg.define('N',short='-n',long='--number',params=1,type=int,desc='Number Input',default=7)

example check parameter: if -n or --number parameter then True

arg.define('N',short='-n',long='--number')

example command group:

arg.define(group_desc='File list',group='ls',command=True)
arg.define('show_detail',short='-l',desc='show detail',group='ls')
arg.define('show_time',short='-t',desc='show time',group='ls')
arg.define('number',short='-n',long='--number',params_name='INT',type=int,desc='Number Input',group='INPUT')
arg.define(group_desc='Power command',group='power',command=True,arg=True,select=['on','off','reset'])
  • Cmd Get Input Command name
cmd=arg.Cmd()

Check input 'command'

   if arg.Cmd('command'):
       ~~~~
   else:
       ~~~~
  • Get parameter's value
    • All data
param=arg.Get()
  • Get group INPUT's All data
param=arg.Get(group='INPUT')
  • Get value of COMMAND power's data
# ./APP power -i x.x.x.x on
if arg.Cmd('power'):
    pw_cmd=arg.Get(group='power')
    if pw_cmd == 'on':
        ...
    elif pw_cmd == 'off':
        ...
    ....
  • Get Parameter number's data
param=arg.Get('number')
  • Get Parameter List's data in the INPUT group
param=arg.Get('List',group='INPUT')

Simple Example)

import SysArg
arg=SysArg.SysArg(program='ArgTest',desc='ARG. Testing',version='1.0',cmd_id=1)
arg.define('a',short='-a',long='--abc',desc='test input',params=1)
arg.define(group_desc='test command',group='ls',command=True)
arg.define('detail',short='-l',desc='show detail',group='ls')
arg.define('find_time',short='-t',desc='find time',group='ls',params=1)
arg.Version()
arg.Help()

cmd=arg.Cmd()
a=arg.Get('a')

Output:

$ python3 a.py --help
Usage: ArgTest <command> [OPTION] [<args>]
Version: 1.0
ARG. Testing

Supported <command>s are:
  ls                                 test command

[OPTION]
   -h, --help                        Help
   -a, --abc                         test input(input(1):S)

* ls                                 test command
   -l                                show detail
   -t                                find time(input(1):S)

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

sysarg-2.0.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sysarg-2.0.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file sysarg-2.0.1.tar.gz.

File metadata

  • Download URL: sysarg-2.0.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for sysarg-2.0.1.tar.gz
Algorithm Hash digest
SHA256 c6b180d01b1cb3b0346fbdcbf32718f1034c9360a207b2b1b15fabc323c98a31
MD5 5cfd0b262e093543444dc9b62b45fe03
BLAKE2b-256 e24da37d943b7290d6a4e58180af497b9703d6b34436be636e43eee2c47d530b

See more details on using hashes here.

File details

Details for the file sysarg-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: sysarg-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for sysarg-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f96c25985cc10886d4b1db70051dcf98cf5e1c2d9cbf25f6cb7f648c454a310
MD5 edfff9d4ac7b50d7bcbd3514b5ba0f02
BLAKE2b-256 0ffe048edb378726b77a9d8e9c8831651a91d7e22c025c7a7418fb92babbfa63

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