|
29 | 29 | import types |
30 | 30 |
|
31 | 31 | from cachemaker import loadcache, savecache, monkeycache, splitverbsubject |
32 | | - from config import __version__, cache_file |
33 | | - from config import read_config, write_config |
| 32 | + from config import __version__, __description__, __projecturl__ |
| 33 | + from config import read_config, write_config, config_file |
| 34 | + from optparse import OptionParser |
34 | 35 | from prettytable import PrettyTable |
35 | 36 | from printer import monkeyprint |
36 | 37 | from requester import monkeyrequest |
@@ -63,13 +64,14 @@ class CloudMonkeyShell(cmd.Cmd, object): |
63 | 64 | intro = ("☁ Apache CloudStack 🐵 cloudmonkey " + __version__ + |
64 | 65 | ". Type help or ? to list commands.\n") |
65 | 66 | ruler = "=" |
66 | | - cache_file = cache_file |
67 | 67 | config_options = [] |
68 | 68 | verbs = [] |
69 | 69 |
|
70 | | - def __init__(self, pname): |
| 70 | + def __init__(self, pname, cfile): |
71 | 71 | self.program_name = pname |
72 | | - self.config_options = read_config(self.get_attr, self.set_attr) |
| 72 | + self.config_file = cfile |
| 73 | + self.config_options = read_config(self.get_attr, self.set_attr, |
| 74 | + self.config_file) |
73 | 75 | self.loadcache() |
74 | 76 | self.prompt = self.prompt.strip() + " " # Cosmetic fix for prompt |
75 | 77 |
|
@@ -364,7 +366,7 @@ def do_set(self, args): |
364 | 366 | key, value = (args[0], args[2]) |
365 | 367 | setattr(self, key, value) # keys and attributes should have same names |
366 | 368 | self.prompt = self.prompt.strip() + " " # prompt fix |
367 | | - write_config(self.get_attr) |
| 369 | + write_config(self.get_attr, self.config_file) |
368 | 370 |
|
369 | 371 | def complete_set(self, text, line, begidx, endidx): |
370 | 372 | mline = line.partition(" ")[2] |
@@ -458,10 +460,39 @@ def do_quit(self, args): |
458 | 460 | return self.do_EOF(args) |
459 | 461 |
|
460 | 462 |
|
| 463 | +class MonkeyParser(OptionParser): |
| 464 | + def format_help(self, formatter=None): |
| 465 | + if formatter is None: |
| 466 | + formatter = self.formatter |
| 467 | + result = [] |
| 468 | + if self.usage: |
| 469 | + result.append("Usage: cloudmonkey [options] [cmds] [params]\n\n") |
| 470 | + if self.description: |
| 471 | + result.append(self.format_description(formatter) + "\n") |
| 472 | + result.append(self.format_option_help(formatter)) |
| 473 | + result.append("\nTry cloudmonkey [help|?]\n") |
| 474 | + return "".join(result) |
| 475 | + |
| 476 | + |
461 | 477 | def main(): |
462 | | - shell = CloudMonkeyShell(sys.argv[0]) |
| 478 | + parser = MonkeyParser() |
| 479 | + parser.add_option("-c", "--config-file", |
| 480 | + dest="cfile", default=config_file, |
| 481 | + help="config file for cloudmonkey", metavar="FILE") |
| 482 | + parser.add_option("-v", "--version", |
| 483 | + action="store_true", dest="version", default=False, |
| 484 | + help="prints cloudmonkey version information") |
| 485 | + |
| 486 | + (options, args) = parser.parse_args() |
| 487 | + print 'args', args |
| 488 | + print 'options', options |
| 489 | + if options.version: |
| 490 | + print "cloudmonkey", __version__ |
| 491 | + print __description__, "(%s)" % __projecturl__ |
| 492 | + |
| 493 | + shell = CloudMonkeyShell(sys.argv[0], options.cfile) |
463 | 494 | if len(sys.argv) > 1: |
464 | | - shell.onecmd(' '.join(sys.argv[1:])) |
| 495 | + shell.onecmd(' '.join(args)) |
465 | 496 | else: |
466 | 497 | shell.cmdloop() |
467 | 498 |
|
|
0 commit comments