From a3bc8ea6051a7873d66607cbe48ecc6c23c89310 Mon Sep 17 00:00:00 2001 From: Seblu Date: Mon, 3 Jan 2011 11:52:20 +0100 Subject: [PATCH] better list displaying --- cccli/command.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/cccli/command.py b/cccli/command.py index 3aa9fed..e534205 100644 --- a/cccli/command.py +++ b/cccli/command.py @@ -10,7 +10,9 @@ import sys import re import pprint import ConfigParser +from optparse import OptionParser +import cccli from cccli.exception import * from cccli.printer import color @@ -170,14 +172,33 @@ class Command(object): def cmd_list(self, argv): '''List objects''' - if len(argv) == 1: - argv.append("a") try: - objs = self.cli.rpc.list(str.join("", argv[1:])) + oparser = OptionParser(prog="list") + oparser.add_option("-t", action="store_true", dest="table") + (options, args) = oparser.parse_args(argv[1:]) + except SystemExit: + return + if len(args) == 0: + args.append("a") + try: + objs = self.cli.rpc.list(str.join("", args)) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) if len(objs) == 0: return + if options.table: + self._list_table(objs) + else: + self._list(objs) + cmd_list.usage = "list [tql]" + + def _list(self, objs): + for o in objs: + id = o.pop("id") + tags = " ".join([ "%s%s:%s%s"%(color["green"], t, color["reset"], v) for (t,v) in o.items() ]) + self.printer.out("%sid:%s%s %s"%(color["green"], color["yellow"], id, tags)) + + def _list_table(self, objs): # get all tag list tags = dict() for o in objs: @@ -186,17 +207,18 @@ class Command(object): # extract id info idsize = tags.pop("id") # print titles - self.printer.out("%s%s%s"%(color["yellow"], "id".ljust(idsize+1), color["reset"]), nl=" ") + self.printer.out(color["green"], nl="") + self.printer.out("id".ljust(idsize+1), nl=" ") for t,v in tags.items(): self.printer.out(t.ljust(v), nl=" ") - self.printer.out() + self.printer.out(color["reset"]) # print obj for obj in objs: self.printer.out("%s%s%s"%(color["yellow"], obj.pop("id").ljust(idsize+1), color["reset"]) ,nl=" ") for (t, v) in tags.items(): self.printer.out(str(obj.get(t, "")).ljust(v) ,nl=" ") self.printer.out() - cmd_list.usage = "list [tql]" + def _startstopsdestroypauseresume(self, argv): # arg stuff -- GitLab