https://github.com/csdummi/cli_framework
A Framework to make it easier for myself to develop small cli apps
https://github.com/csdummi/cli_framework
app cli python python3
Last synced: 8 days ago
JSON representation
A Framework to make it easier for myself to develop small cli apps
- Host: GitHub
- URL: https://github.com/csdummi/cli_framework
- Owner: CSDUMMI
- License: gpl-3.0
- Created: 2019-05-22T18:51:09.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T15:43:18.000Z (about 7 years ago)
- Last Synced: 2025-02-14T04:48:16.055Z (over 1 year ago)
- Topics: app, cli, python, python3
- Language: Python
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**This is documentation refering to [this Repository](https://github.com/CSDUMMI/cli_framework)**
# cli_framework
A Framework to make it easier for myself to develop small cli apps
# Install
This project can be installed using
pip:
```bash
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:
```python
from CLI.CLI import CLI
```
Then you define a class that inherits
from `CLI` and define `__init__` like this:
```python
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:
```python
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:
```bash
$ python3 App.py add
$ python3 App.py remove
$ 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:
```python
class App(CLI):
"""
Usage:
add \t:Add new user
remove \t:Remove user
list:\tList all current users
help:\tDisplay this help
"""
...
```
Then you can use execute the CLI with this:
```python
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.