Skip to main content

A convention based CLI framework for python

Project description

pyconvcli

A convention based CLI framework for python

This framework builds on the argparse library making it easy to setup and build a cli by convention from code without having to string together the hierarchy yourself. You simpley create a cli project using the cli which is built using the framework itself, and create classes with functions. We utilize the type annotation to configure your cli attributes for you using the same params as the argparse.add_argument function with an expansion of the 'group' attribute to let you assign a parameter to a group. You can create groups using our decorator which adds metadata to the function so that we can assign the parameters to the correct group.

How do I get set up?

step 1

run the install command pip3 install pyconvcli

step 2

run the command to create a new cli project in the directory you want to create it in.

for example

pyconvcli new cli --project_name example --cli_call_word mycli

the usage is as follows

usage: pyconvcli new cli [-h] --project_name PROJECT_NAME
                         [--root_package_name ROOT_PACKAGE_NAME]
                         [--cli_call_word CLI_CALL_WORD] [--version VERSION]
                         [--author AUTHOR] [--author_email AUTHOR_EMAIL]
                         [--description DESCRIPTION]

optional arguments:
  -h, --help            show this help message and exit
  --project_name PROJECT_NAME
                        the name of your project
  --root_package_name ROOT_PACKAGE_NAME
                        the name of your cli package
  --cli_call_word CLI_CALL_WORD
                        the string that will be added to your path to call
                        your cli
  --version VERSION     the version of your project
  --author AUTHOR       the author of your project
  --author_email AUTHOR_EMAIL
                        the email of the author of your project
  --description DESCRIPTION
                        the description of your project

step 3

in order to create a command you need to create a class in your project that either ends with _CLI in the name or _CLI_ROOT. if it ends with cli the command will be the file path from the root of your project followed by the function name. If it is _CLI_ROOT it will be the function name followed by the root command of your project.

This is an example of the code to add a root command

import time
from pyconvcli import ParserArgType


class Alternative_CLI_ROOT():
    def dance(
        self,
        rythem:ParserArgType(type=int, choices=range(60),dest="rythem",required=True),
        song:str,
        duration:int):
        """
        params 
        rythem: movements per minute
        time: minutes
        song: song to dance to

        """
        start_time = time.time()
        count=0
        print(f'{song} playing.....')
        while time.time()-start_time< (duration*60):
            if(count % 2) == 0:
                print('<("<)')
            else:
                print('(>")>')
            count = count +1
            time.sleep((60/rythem))

You can also optionally add a _cli_path attribut to the class with a value of the string array you want to declare the path as. This will have a higher priority and allow you to declare the path to your action set.

class Testing_CLI():
    _cli_path=['here','testing']
    def testCommand(self,arg1:int,arg2:str,arg3:ParserArgType(type=str,choices=['exclusive','inclusive','indecisive'])):
        print(f'arg1:{arg1}, arg2:{arg2}, arg3:{arg3}')

If you need a group created you can creat them through an annotation like this. Note that you tie the params to the group by matching the name, and the group attribute in the parser arg type.

class Groups_CLI():
    _cli_path=['here','custom','groups']
    @ArgGroupsDecorator(ParserArgGroupType(name='required',description="my required group"), ParserArgMutuallyExclusiveType(name='exclusive',required=True))
    def groupsCommand(self,arg1:ParserArgType(group='required'),arg2:ParserArgType(group='exclusive'),arg3:ParserArgType(group='exclusive')):
        print("groups command called")

If you want to create an action for a parser that does a separate piece of work you can use the action annotation as follows

 class Utility_CLI():
    _cli_path=[]

    @ActionArguement()
    def version(self):
        print(pkg_resources.get_distribution("pyconvcli").version)

the previous example appends a --version action to the root of your cli. this runs independently or could be thought of as in parallel with any other actions you call in the same parser. for example if you have the code3 belo and ran the test command with the additional --version parameter the version function would activate when your parser runs and then the testCommand function would run after.

class Utility_CLI():
    _cli_path=[]

    @ActionArguement()
    def version(self):
    print(pkg_resources.get_distribution("pyconvcli").version)

    def testCommand(self,arg1:int,arg2:str,arg3:ParserArgType(type=str,choices=['exclusive','inclusive','indecisive'])):
        print(f'arg1:{arg1}, arg2:{arg2}, arg3:{arg3}')

The App

after installing your cli you will be able to run it as an application as well by running the entrypoint command to run your cli with the -app postfix. This will open a gui that will allow you to select your commands from dropdowns and enter values in form fields. You will have the option of calling the command from the app or copying it to the clipboard to be pasted into the terminal later. The app handles any needed escaping of characters for your call.

Who do I talk to?

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

pyconvcli-0.0.6.tar.gz (18.4 kB view details)

Uploaded Source

Built Distributions

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

pyconvcli-0.0.6-py3.9.egg (43.9 kB view details)

Uploaded Egg

pyconvcli-0.0.6-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyconvcli-0.0.6.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for pyconvcli-0.0.6.tar.gz
Algorithm Hash digest
SHA256 039d1bc6af1e20b8ac60a8bba008ddc5df0b34485ce777c121cfd5b9d1bf6349
MD5 b247ba2bff5e02c4be3794a16321a992
BLAKE2b-256 fea1bed357a97d391034191961a364b7da87cadde77582051c733873c4e951ad

See more details on using hashes here.

File details

Details for the file pyconvcli-0.0.6-py3.9.egg.

File metadata

  • Download URL: pyconvcli-0.0.6-py3.9.egg
  • Upload date:
  • Size: 43.9 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for pyconvcli-0.0.6-py3.9.egg
Algorithm Hash digest
SHA256 38b289915cc4be01a9b87e669a0fdf318cc3a4517fb2a98c8962ef4e422702be
MD5 89e16c5734ee080e6bef2bf3292251a7
BLAKE2b-256 e3dd442ee8fafb820953b1cc3c666470d5a12f0a251b657d5c353e5b5f7e47d7

See more details on using hashes here.

File details

Details for the file pyconvcli-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyconvcli-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for pyconvcli-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4f84774f91e95799c13fdac5dcd692d5b012984257bd944bf0bd4ae3e65cc003
MD5 e4b58fe44e7b8367b6ffeb02f12fb4bf
BLAKE2b-256 a1b7fc62b767dc301d769d3cccb7b5dcf51412397d523381164812119a01a98a

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