Support 'nilmtool help command' just like 'nilmtool command --help'

This commit is contained in:
Jim Paris 2013-03-02 13:56:03 -05:00
parent 64897a1dd1
commit 521ff88f7c
11 changed files with 63 additions and 13 deletions

View File

@ -12,8 +12,8 @@ from argparse import ArgumentDefaultsHelpFormatter as def_form
# Valid subcommands. Defined in separate files just to break
# things up -- they're still called with Cmdline as self.
subcommands = [ "info", "create", "list", "metadata", "insert", "extract",
"remove", "destroy" ]
subcommands = [ "help", "info", "create", "list", "metadata",
"insert", "extract", "remove", "destroy" ]
# Import the subcommand modules
subcmd_mods = {}
@ -31,6 +31,7 @@ class Cmdline(object):
self.argv = argv or sys.argv[1:]
self.client = None
self.def_url = os.environ.get("NILMDB_URL", "http://localhost:12380")
self.subcmd = {}
def arg_time(self, toparse):
"""Parse a time string argument"""
@ -55,15 +56,14 @@ class Cmdline(object):
default=self.def_url,
help="NilmDB server URL (default: %(default)s)")
sub = self.parser.add_subparsers(title="Commands",
dest="command",
description="Specify --help after "
"the command for command-specific "
"options.")
sub = self.parser.add_subparsers(
title="Commands", dest="command",
description="Use 'help command' or 'command --help' for more "
"details on a particular command.")
# Set up subcommands (defined in separate files)
for cmd in subcommands:
subcmd_mods[cmd].setup(self, sub)
self.subcmd[cmd] = subcmd_mods[cmd].setup(self, sub)
def die(self, formatstr, *args):
fprintf(sys.stderr, formatstr + "\n", *args)
@ -86,11 +86,13 @@ class Cmdline(object):
self.client = nilmdb.Client(self.args.url)
# Make a test connection to make sure things work
try:
server_version = self.client.version()
except nilmdb.client.Error as e:
self.die("error connecting to server: %s", str(e))
# Make a test connection to make sure things work,
# unless the particular command requests that we don't.
if "no_test_connect" not in self.args:
try:
server_version = self.client.version()
except nilmdb.client.Error as e:
self.die("error connecting to server: %s", str(e))
# Now dispatch client request to appropriate function. Parser
# should have ensured that we don't have any unknown commands

View File

@ -26,6 +26,7 @@ Layout types are of the format: type_count
help="Path (in database) of new stream, e.g. /foo/bar")
group.add_argument("layout",
help="Layout type for new stream, e.g. float32_8")
return cmd
def cmd_create(self):
"""Create new stream"""

View File

@ -16,6 +16,7 @@ def setup(self, sub):
group = cmd.add_argument_group("Required arguments")
group.add_argument("path",
help="Path of the stream to delete, e.g. /foo/bar")
return cmd
def cmd_destroy(self):
"""Destroy stream"""

View File

@ -30,6 +30,7 @@ def setup(self, sub):
help="Show raw timestamps in annotated information")
group.add_argument("-c", "--count", action="store_true",
help="Just output a count of matched data points")
return cmd
def cmd_extract_verify(self):
if self.args.start is not None and self.args.end is not None:

26
nilmdb/cmdline/help.py Normal file
View File

@ -0,0 +1,26 @@
from nilmdb.utils.printf import *
import argparse
import sys
def setup(self, sub):
cmd = sub.add_parser("help", help="Show detailed help for a command",
description="""
Show help for a command. 'help command' is
the same as 'command --help'.
""")
cmd.set_defaults(handler = cmd_help)
cmd.set_defaults(no_test_connect = True)
cmd.add_argument("command", nargs="?",
help="Command to get help about")
cmd.add_argument("rest", nargs=argparse.REMAINDER,
help=argparse.SUPPRESS)
return cmd
def cmd_help(self):
if self.args.command in self.subcmd:
self.subcmd[self.args.command].print_help()
else:
self.parser.print_help()
return

View File

@ -12,6 +12,7 @@ def setup(self, sub):
version.
""")
cmd.set_defaults(handler = cmd_info)
return cmd
def cmd_info(self):
"""Print info about the server"""

View File

@ -47,6 +47,7 @@ def setup(self, sub):
help="Path of stream, e.g. /foo/bar")
group.add_argument("file", nargs="*", default=['-'],
help="File(s) to insert (default: - (stdin))")
return cmd
def cmd_insert(self):
# Find requested stream

View File

@ -35,6 +35,7 @@ def setup(self, sub):
group.add_argument("-e", "--end",
metavar="TIME", type=self.arg_time,
help="Ending timestamp (free-form, noninclusive)")
return cmd
def cmd_list_verify(self):
# A hidden "path_positional" argument lets the user leave off the

View File

@ -26,6 +26,7 @@ def setup(self, sub):
exc.add_argument("-u", "--update", nargs="+", metavar="key=value",
help="Update metadata using provided "
"key=value pairs")
return cmd
def cmd_metadata(self):
"""Manipulate metadata"""

View File

@ -23,6 +23,7 @@ def setup(self, sub):
group = cmd.add_argument_group("Output format")
group.add_argument("-c", "--count", action="store_true",
help="Output number of data points removed")
return cmd
def cmd_remove(self):
try:

View File

@ -193,6 +193,20 @@ class TestCmdline(object):
self.fail("extract --start 2000-01-01 --start 2001-01-02")
self.contain("duplicated argument")
# Verify that "help command" and "command --help" are identical
# for all commands.
self.fail("")
m = re.search(r"{(.*)}", self.captured)
for command in [""] + m.group(1).split(','):
self.ok(command + " --help")
cap1 = self.captured
self.ok("help " + command)
cap2 = self.captured
self.ok("help " + command + " asdf --url --zxcv -")
cap3 = self.captured
eq_(cap1, cap2)
eq_(cap2, cap3)
def test_02_parsetime(self):
os.environ['TZ'] = "America/New_York"
test = datetime_tz.datetime_tz.now()