Set of decorators of to create transform a class into a command-line tool.
Project description
Set of decorators of to create transform a class into a command-line tool
Imagine, you’ve got a class you want to use from the command line, without having to explicitly parse the command line and make all the routing stuff.
That’s what’s cltools is doing by proving decorators on class and methods to make your class runnable, and transform your method into commands.
cltools create command tools like git/hg/svn/apt-get/apt-cache/…, that means your tool will have commands
simple example :
Imagine, you’ve got a simple class that make tasks, and you want to make a command line tool with that. Let’s say, it’s a calculator module calclib.py:
#!/usr/bin/env python
class Calc(object) :
def __init__(self) :
pass
def add(self, value1, value2) :
return value1+value2
def mult(self, value1, value2) :
return value1*value2
Then, we will write a simple class and transform into a runnable tool:
#!/usr/bin/env python
import sys
from calclib import Calc
from cltools import CLRunner
@CLRunner.runnable()
class CalcTool(object) :
'''A simple command-line wrapper for calclib'''
def __init__(self) :
self._calc = Calc()
def get_two_params(self, args) :
if len(args) != 2 :
# errorexit provided by CLRunnable parent
self.errorexit("Need two values VALUE1 and VALUE2 as arguments")
try :
value1 = int(args[0])
except Exception :
self.errorexit("Value [%s] should be a valid number" % (args[0],))
try :
value2 = int(args[1])
except Exception :
self.errorexit("Value [%s] should be a valid number" % (args[1],))
return value1, value2
@CLRunner.command()
def add(self, args, kwargs) :
'''Add two values VALUE1 and VALUE2 given as parameters'''
value1, value2 = self.get_two_params(args)
value = self._calc.add(value1, value2)
self.status("Result : [%s]" % (value,))
@CLRunner.command()
def mult(self, args, kwargs) :
'''Multiplie two values VALUE1 and VALUE2'''
value1, value2 = self.get_two_params(args)
value = self._calc.mult(value1, value2)
self.status("Result : [%s]" % (value,))
@CLRunner.command()
def help(self, args=[], kwargs={}) :
'''Get this help'''
super().help()
if __name__ == '__main__' :
calctool = CalcTool()
if not(calctool.run( sys.argv )) :
sys.exit(1)
Now we can test our command line tool:
$ ./calc.py Usage: calc.py COMMAND_NAME [OPTION] [VALUES] A simple command-line wrapper for calclib Commands: add Add two values VALUE1 and VALUE2 given as parameters help Get this help mult Multiplie two values VALUE1 and VALUE2 Error : Need a command name
$ ./calc.py add 4 17 Result : [21]
$ ./calc.py add 15 66 33 Error : Need two values VALUE1 and VALUE2 as arguments
Note that the help is automatically generate based on commands declared in the class, and the online doc attached to the class and methods.
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 cltools-0.5.0.tar.gz
.
File metadata
- Download URL: cltools-0.5.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/2.7.17 Linux/4.15.0-140-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e27b06afc8bbf472cdd66f85de8bbddc662a53259522af6425b383765e75aae6 |
|
MD5 | d0c24f2ba43d21f90a63162768fd8eaf |
|
BLAKE2b-256 | 0fae925cabe3d351c77fe0b63152dff0e4a88d46381da6c8d9f4b96866d8ac3b |
File details
Details for the file cltools-0.5.0-py2.py3-none-any.whl
.
File metadata
- Download URL: cltools-0.5.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/2.7.17 Linux/4.15.0-140-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4bcd14c0cb0ce85440fdaf7f051c458e511a7112bfec3f864b68ac3a9ffa5e9 |
|
MD5 | 25b99ab870d473263699168af8104bc6 |
|
BLAKE2b-256 | 2836a756960df1a3258e71d94e584f863919b7a0522e98c360c50ad3dcd1559e |