summaryrefslogtreecommitdiff
authorDustin Frisch <fooker@lab.sh>2013-02-20 00:53:13 (GMT)
committer Dustin Frisch <fooker@lab.sh>2013-02-20 00:53:13 (GMT)
commit0e54933aaf2db63410e3a8eadb2bde5fb6c24346 (patch) (side-by-side diff)
tree2b560a320f24a06a60dc28fa18d99018d11e47b0
parentc7e30d0d1ee0e109f129cddabd001fb8c2edd342 (diff)
downloadehctrl-master.zip
ehctrl-master.tar.gz
Help function added to show command list and command usageHEADmaster
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--ehctrl.py51
1 files changed, 48 insertions, 3 deletions
diff --git a/ehctrl.py b/ehctrl.py
index bf33c45..506cc56 100644
--- a/ehctrl.py
+++ b/ehctrl.py
@@ -20,6 +20,8 @@ along with ehctrl. If not, see <http://www.gnu.org/licenses/>.
import sys
import serial
+from functools import partial
+
class Registry(object):
@@ -34,7 +36,10 @@ class Registry(object):
return self.__commands[args[0]](connection, *args[1:])
else:
- return self.__func(connection, *args)
+ command = partial(self.__func, connection, *args)
+ command.__doc__ = self.__func.__doc__
+
+ return command
def __getitem__(self, key):
@@ -45,6 +50,10 @@ class Registry(object):
return key in self.__commands
+ def __iter__(self):
+ return iter(self.__commands)
+
+
def register(self, name):
def __(func):
registry = Registry(func)
@@ -69,7 +78,6 @@ register = root.register
class Error(Exception): pass
-class NotImplemented(Exception): pass
@@ -117,8 +125,32 @@ class Connection(object):
+@register('help')
+def __help(connection, *args):
+ '''Show the list of commands or help for a specific command'''
+
+ if not args:
+ print 'The following commands are available:'
+
+ def __(registry, path = []):
+ for r in sorted(registry):
+ print ' %s' % (' '.join(path + [r]))
+ __(registry[r], path + [r])
+
+ __(root)
+
+ else:
+ command = root(connection, *args)
+
+ print 'Help for command "%s"' % ' '.join(args)
+ print ' %s' % command.__doc__
+
+
+
@register('pwr')
def pwr(connection):
+ '''Shows power status'''
+
result = connection('PWR?')
if result['PWR'] == '00': print 'off'
@@ -131,11 +163,15 @@ def pwr(connection):
@pwr.register('on')
def pwr_on(connection):
+ '''Turns the device on'''
+
connection('PWR ON')
@pwr.register('off')
def pwr_off(connection):
+ '''Turns the device off'''
+
connection('PWR OFF')
@@ -144,6 +180,14 @@ def main():
# Check if required arguments are given
if len(sys.argv) < 3:
print 'Usage: ehctrl DEVICE COMMAND [SUBCOMMANDS ...] [PARAMETERS ...]'
+ print ' DEVICE the serial port connected to the device'
+ print ' COMMAND the command to execute'
+ print ' SUBCOMMANDS the sub-command to execute'
+ print ' PARAMETERS the parameters to pass to the command'
+ print ''
+ print 'Use command "help" for further information.'
+ print ''
+ print 'Report bugs to Dustin Frisch<fooker@lab.sh>'
sys.exit(255)
# Create connection to serial device
@@ -153,7 +197,8 @@ def main():
# Find command by walking down the tree as deep as possible and call the
# last resolved node with the remaining parameters
try:
- root(connection, *sys.argv[2:])
+ command = root(connection, *sys.argv[2:])
+ command()
except Error:
print 'Invalid command.'