Skip to main content

python for command line interface development

Project description

py4cli

  • Python for Command Line Interface development ( scalable argument parser ).
  • Argument Parser for developing Command Line Tools, that is scalable as per need.
  • Check out the example code in repo ( https://github.com/Palani-SN/py4cli ) for reference.

Installation

  python -m pip install py4cli

Minimal

  • minimal arg parser that can pass on the cli arguments to parse_args class method arguments as per the declarative type definition. (works fine on windows & linux)
  • Sample code as shown below can read arguments in specified type as per function signature. (refer use_minimal.py under EXAMPLES/)
from py4cli.minimal import arg_parser

# Multiple arguments example

class multi_args(arg_parser):

    # example parse_args template function with multiple arguments of different types
    def parse_args(self, 
            inp_int: int = 6,
            inp_float: float = 6.0,
            inp_str: str = "Six",
            inp_list: list = [6, 6.0, "Six"],
            inp_dict: dict = {'int': 6, 'float': 6.0, 'str': "Six"},
            inp_bool: bool = False) -> dict:
        """
        Six arguments of different data type can be passed
        any value of the respective data type can be passed for specific argument. for defaults refer above
        the function returns a dict containing all the arguments and its values.

        cmds :
            1. python <__file__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
            2. python <__file__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
        """
        return {
                'inp_int': inp_int,
                'inp_float': inp_float,
                'inp_str': inp_str,
                'inp_list': inp_list,
                'inp_dict': inp_dict,
                'inp_bool': inp_bool
            }
    
if __name__ == '__main__':

    import sys
    import json

    print(sys.argv)
    obj = multi_args()
    print("")
    if obj.returned:
        out_dict = obj.returned.copy()
        print(json.dumps(out_dict, indent=2, sort_keys=True), type(obj.returned))
    else:
        print(obj.returned, type(obj.returned))
  • To get help on how to use the script, execute python use_minimal.py -h or python use_minimal.py --help which will generate the doc content based on the comments in script as shown below.
(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py --help
['use_minimal.py', '--help']

 | > def parse_args
 |
 |  Description :
 |
 |    example parse_args template function with multiple arguments of different types
 |
 |  Arguments :
 |
 |   -inp_int: int = 6
 |   -inp_float: float = 6.0
 |   -inp_str: str = 'Six'
 |   -inp_list: list = [6, 6.0, 'Six']
 |   -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
 |   -inp_bool: bool = False
 |
 |  Usage :
 |
 |    Six arguments of different data type can be passed
 |    any value of the respective data type can be passed for specific argument. for defaults refer above
 |    the function returns a dict containing all the arguments and its values.
 |
 |    cmds :
 |    1. python D:\GitRepos\py4cli\EXAMPLES\use_minimal.py 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
 |    2. python D:\GitRepos\py4cli\EXAMPLES\use_minimal.py -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
 |
 | -> dict (Returnable)

None <class 'NoneType'>
  • The commands specified can be used to alter the values as command line arguments to the script.
output

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py
['use_minimal.py']

{
  "inp_bool": false,
  "inp_dict": {
    "float": 6.0,
    "int": 6,
    "str": "Six"
  },
  "inp_float": 6.0,
  "inp_int": 6,
  "inp_list": [
    6,
    6.0,
    "Six"
  ],
  "inp_str": "Six"
} <class 'dict'>

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py 100 100.0 "parse args example function call" "[1, 2, 3, 4, 5, 6]"
['use_minimal.py', '100', '100.0', 'parse args example function call', '[1, 2, 3, 4, 5, 6]']

{
  "inp_bool": false,
  "inp_dict": {
    "float": 6.0,
    "int": 6,
    "str": "Six"
  },
  "inp_float": 100.0,
  "inp_int": 100,
  "inp_list": [
    1,
    2,
    3,
    4,
    5,
    6
  ],
  "inp_str": "parse args example function call"
} <class 'dict'>

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py -inp_int=100 -inp_float=100.0 -inp_str="parse args example function call" -inp_list="[1, 2, 3, 4, 5, 6]"
['use_minimal.py', '-inp_int=100', '-inp_float=100.0', '-inp_str=parse args example function call', '-inp_list=[1, 2, 3, 4, 5, 6]']

{
  "inp_bool": false,
  "inp_dict": {
    "float": 6.0,
    "int": 6,
    "str": "Six"
  },
  "inp_float": 100.0,
  "inp_int": 100,
  "inp_list": [
    1,
    2,
    3,
    4,
    5,
    6
  ],
  "inp_str": "parse args example function call"
} <class 'dict'>

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py 100 100.0 -inp_str="parse args example function call" -inp_list="[1, 2, 3, 4, 5, 6]"
['use_minimal.py', '100', '100.0', '-inp_str=parse args example function call', '-inp_list=[1, 2, 3, 4, 5, 6]']

{
  "inp_bool": false,
  "inp_dict": {
    "float": 6.0,
    "int": 6,
    "str": "Six"
  },
  "inp_float": 100.0,
  "inp_int": 100,
  "inp_list": [
    1,
    2,
    3,
    4,
    5,
    6
  ],
  "inp_str": "parse args example function call"
} <class 'dict'>

Moderate

  • Vertically scalable version of minimal arg parser, aimed at use case like, hyper parameter tuning.
  • Sample code as shown below can read arguments in specified type as per function signature. (refer use_moderate.py under EXAMPLES/)
from py4cli.moderate import arg_parser

# Multiple arguments example

class vscaled_args(arg_parser):

    # example multi_args template function with multiple arguments of different types
    def multi_args1(self, 
            inp_int: int = 6,
            inp_float: float = 6.0,
            inp_str: str = "Six",
            inp_list: list = [6, 6.0, "Six"],
            inp_dict: dict = {'int': 6, 'float': 6.0, 'str': "Six"},
            inp_bool: bool = False) -> dict:
        """
        Six arguments of different data type can be passed
        any value of the respective data type can be passed for specific argument. for defaults refer above
        the function returns a dict containing all the arguments and its values.

        cmds :
            1. python <__file__> ~<__func__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
            2. python <__file__> ~<__func__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
        """
        return {
                'inp_int': inp_int,
                'inp_float': inp_float,
                'inp_str': inp_str,
                'inp_list': inp_list,
                'inp_dict': inp_dict,
                'inp_bool': inp_bool
            }
    
    # example multi_args template function with multiple arguments of different types
    def multi_args2(self, 
            inp_int: int = 6,
            inp_float: float = 6.0,
            inp_str: str = "Six",
            inp_list: list = [6, 6.0, "Six"],
            inp_dict: dict = {'int': 6, 'float': 6.0, 'str': "Six"},
            inp_bool: bool = False) -> dict:
        """
        Six arguments of different data type can be passed
        any value of the respective data type can be passed for specific argument. for defaults refer above
        the function returns a dict containing all the arguments and its values.

        cmds :
            1. python <__file__> ~<__func__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
            2. python <__file__> ~<__func__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
        """
        return {
                'inp_int': inp_int,
                'inp_float': inp_float,
                'inp_str': inp_str,
                'inp_list': inp_list,
                'inp_dict': inp_dict,
                'inp_bool': inp_bool
            }

if __name__ == '__main__':

    import sys
    import json

    print(sys.argv)
    obj = vscaled_args()
    print("")
    if obj.returned:
        out_dict = obj.returned.copy()
        print(json.dumps(out_dict, indent=2, sort_keys=True), type(obj.returned))
    else:
        print(obj.returned, type(obj.returned))
  • To get help on how to use the script, execute python use_moderate.py -h or python use_moderate.py --help which will generate the doc content based on the comments in script as shown below.
(examples) D:\GitRepos\py4cli\EXAMPLES>python use_moderate.py
['use_moderate.py']
Methods available for use, is listed below
[
  "~multi_args1",
  "~multi_args2"
]

None <class 'NoneType'>

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_moderate.py --help
['use_moderate.py', '--help']

 | > def multi_args1
 |
 |  Description :
 |
 |    example multi_args template function with multiple arguments of different types
 |
 |  Arguments :
 |
 |   -inp_int: int = 6
 |   -inp_float: float = 6.0
 |   -inp_str: str = 'Six'
 |   -inp_list: list = [6, 6.0, 'Six']
 |   -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
 |   -inp_bool: bool = False
 |
 |  Usage :
 |
 |    Six arguments of different data type can be passed
 |    any value of the respective data type can be passed for specific argument. for defaults refer above
 |    the function returns a dict containing all the arguments and its values.
 |
 |    cmds :
 |    1. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args1 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
 |    2. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args1 -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
 |
 | -> dict (Returnable)

 | > def multi_args2
 |
 |  Description :
 |
 |    example multi_args template function with multiple arguments of different types
 |
 |  Arguments :
 |
 |   -inp_int: int = 6
 |   -inp_float: float = 6.0
 |   -inp_str: str = 'Six'
 |   -inp_list: list = [6, 6.0, 'Six']
 |   -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
 |   -inp_bool: bool = False
 |
 |  Usage :
 |
 |    Six arguments of different data type can be passed
 |    any value of the respective data type can be passed for specific argument. for defaults refer above
 |    the function returns a dict containing all the arguments and its values.
 |
 |    cmds :
 |    1. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args2 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
 |    2. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args2 -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
 |
 | -> dict (Returnable)

None <class 'NoneType'>
  • The commands specified can be used to alter the values as command line arguments to the script. The methods can be selected with appropriate arguments as per definition, and the order of execution of the methods is also controllable. For illustration purpose only two methods [multi_args1, multi_args2] is defined which can be scaled to number of functions, as much as python supports.
output

(examples) D:\GitRepos\py4cli\EXAMPLES>python use_moderate.py ~multi_args1 100 100.0 "multi_args1 example function call" "[1, 2, 3, 4, 5, 6]" ~multi_args2 -inp_int=100 -inp_float=100.0 -inp_str="multi_args2 example function call" -inp_list="[1, 2, 3, 4, 5, 6]"
['use_moderate.py', '~multi_args1', '100', '100.0', 'multi_args1 example function call', '[1, 2, 3, 4, 5, 6]', '~multi_args2', '-inp_int=100', '-inp_float=100.0', '-inp_str=multi_args2 example function call', '-inp_list=[1, 2, 3, 4, 5, 6]']

{
  "multi_args1": {
    "inp_bool": false,
    "inp_dict": {
      "float": 6.0,
      "int": 6,
      "str": "Six"
    },
    "inp_float": 100.0,
    "inp_int": 100,
    "inp_list": [
      1,
      2,
      3,
      4,
      5,
      6
    ],
    "inp_str": "multi_args1 example function call"
  },
  "multi_args2": {
    "inp_bool": false,
    "inp_dict": {
      "float": 6.0,
      "int": 6,
      "str": "Six"
    },
    "inp_float": 100.0,
    "inp_int": 100,
    "inp_list": [
      1,
      2,
      3,
      4,
      5,
      6
    ],
    "inp_str": "multi_args2 example function call"
  }
} <class 'dict'>

Maximal (in progress)

  • Horizontally scalable version of minimal arg parser, aimed at use case like, workflow development for testing & debug needs.

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

py4cli-0.0.6.tar.gz (28.6 kB view details)

Uploaded Source

File details

Details for the file py4cli-0.0.6.tar.gz.

File metadata

  • Download URL: py4cli-0.0.6.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/6.7.0 pkginfo/1.10.0 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.66.2 CPython/3.7.16

File hashes

Hashes for py4cli-0.0.6.tar.gz
Algorithm Hash digest
SHA256 054b72bfc5fa882fae596a698345bb594198ff932706891bc60f5438920c4175
MD5 3a51e4b61cb60e522437266cbba86197
BLAKE2b-256 eaa9ca7b65f88f92e8543ee745a566d6c259490f1a1ee3a0281cafd9aa79ef0b

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