A functional programming styled CLI console application framework based on argparse
Project description
Konsoru
A functional programming styled CLI console application framework based on argparse.
Installation
The easiest way to install konsoru is to use pip:
$ pip install konsoru
Usage
Konsoru enables you to build CLI applications quickly: either a text-based console or a program with multiple subcommands that executes in terminal.
Below is a bare minimum helloworld.py
program that launches a text-based console:
from konsoru import CLI
def helloworld():
print('Hello world!')
cli = CLI()
cli.add_function(helloworld)
cli.loop()
The console comes with 3 default commands: help, quit, exit. And every command added into the console, including default commands, will come with a -h
option that displays its help message.
When the above program runs, it looks like this:
$ python3 helloworld.py
Type 'help' to see help message.
Type 'quit' or 'exit' to exit the program.
> help
Available commands:
exit helloworld help quit
See help for a specific command by specifying the command name.
Help message for multi-layered command can still be seen by using quotation marks around them.
> helloworld -h
usage: helloworld [-h]
optional arguments:
-h, --help show this help message and exit
> helloworld
Hello world!
> quit
Alternatively, you can add your function as a command using the subroutine()
decorator. The decorator should be stacked on top of other decorators, if any. Also, by default, the framework will print the return of added functions, unless the return is None
. So the program below is completely equivalent to the example above:
from konsoru import CLI
cli = CLI()
@cli.subroutine()
def helloworld():
return 'Hello world!'
cli.loop()
To convert this into a program with subcommands that is interacted with through terminal, simply change cli.loop()
into cli.run()
at the end. Then, the program will behave like this:
$ python3 helloworld.py helloworld
Hello world!
Note that default commands under the console mode, i.e. help
, exit
, quit
, will not be added when using cli.run()
.
Put the script in a directory in your $PATH
and give it executable permissions to call it anywhere in your filesystem without python3
in the front.
For more sophisticated examples, check the examples/
directory in the github repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file konsoru-0.1.2.zip
.
File metadata
- Download URL: konsoru-0.1.2.zip
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e3c748f303f392c8e297fc107b52f1fcd2c9d886dbf1dd7230bc3c819127065 |
|
MD5 | 07171b2f697e7d8b032d34796d58c66b |
|
BLAKE2b-256 | 863469892cc029eab5e4c4a4f34a35ebb6e098a6a59f736fa4725a793370ea07 |
File details
Details for the file konsoru-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: konsoru-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87c4fcc3eea6e5beb111b15e982ab7fa033ef7cfd5ad782d4c85d8b47105f94f |
|
MD5 | 2199654e711c17b158567ba04a877a81 |
|
BLAKE2b-256 | b9ac03eeec3fe9637c009474c279216df45ec030816ea0e8f0fc1955b32695c2 |