Skip to main content

A framework to write CLI projects.

Project description

This is documentation refering to this Repository

cli_framework

A Framework to make it easier for myself to develop small cli apps

Install

This project can be installed using pip:

pip3 install CLI-csdummi

An example implementation

To implement your own CLI app with this library you have to import the class CLI like this:

from CLI.CLI import CLI

Then you define a class that inherits from CLI and define __init__ like this:

class App(CLI):
  def __init__(self):
    super().__init__('app.json','add','remove','list')

The __init__ Function from CLI takes one positional argument file or in this case 'app.json'. This is the file where the variable self.state is loaded from and saved to before and after execution. You can thus modify self.state like any dict and then it will be saved afterwards. The other arguments are the names of methods you want to be accessed from the command-line. These names must be members of the class App in this case. This means you now have to implement these functions:

  def add(self,args):
    name = args[0]
    balance = args[1]
    self.state[name] = balance

  def remove(self,args):
    name = args[0]
    del self.state[name]

  def list(self,*args):
    for i in self.state.keys():
      print("Name: {}\nBalance: {}".format(i,self.state[i]))

As you can see the names of the methods and the arguments in __init__.

You can now use your CLI App like this:

$ python3 App.py add <name> <balance>
$ python3 App.py remove <name>
$ python3 App.py list
$ python3 App.py help
$ python3 App.py

You can execute these commands right now, all code here was written in the file App.py in this Repository.

help == ''

If there is no command, the help function, that was inherited from CLI is executed, it prints the docstring of self: self.__doc__. If you want usage information for your CLI App to appear, you just write the usage or help into the docstring like this:

class App(CLI):
  """
  Usage:
  add <name> <balance>\t:Add new user
  remove <name>\t:Remove user
  list:\tList all current users
  help:\tDisplay this help
  """
  ...

Then you can use execute the CLI with this:

if __name__ == '__main__':
  app = App() # Create Instance of CLI App
  app.run() # Execute Commandline Parsing

I hope this helped you and you can implement your own CL Apps or interfaces soon.

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

CLI-csdummi-1.0.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

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

CLI_csdummi-1.0.1-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file CLI-csdummi-1.0.1.tar.gz.

File metadata

  • Download URL: CLI-csdummi-1.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.3

File hashes

Hashes for CLI-csdummi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d16496dc3c96d0a3477d548fb41c0da661cfa551794cbd5bdbaf28852dff9302
MD5 c3ce6853bfad3fd2dfcbef51d094cfcc
BLAKE2b-256 b49fd931e0aec3c3c27c81803b86b07b397aea9d2e01b657d476794dc6be993d

See more details on using hashes here.

File details

Details for the file CLI_csdummi-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: CLI_csdummi-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.5.3

File hashes

Hashes for CLI_csdummi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9fbf9976cc8e0bf0aeac9718e33c19c09c33d701d88db8569c713ee6656d824c
MD5 808268294c26bb6159af61f97c0b4863
BLAKE2b-256 e38adb3ec3e96378bbbbf80a492105ac5c826c721fda05d6b1ee980e956654ef

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