def main(self, argv): """ Main method takes argument from top level mesos and parses them to call the appropriate method. """ command_strings = cli.util.format_commands_help(self.COMMANDS) usage = self.USAGE.format( plugin=self.PLUGIN_NAME, short_help=self.SHORT_HELP, commands=command_strings) arguments = docopt( usage, argv=argv, version=self.VERSION, program="mesos " + self.PLUGIN_NAME, options_first=True) cmd = arguments["<command>"] argv = arguments["<args>"] if cmd in self.COMMANDS.keys(): if "external" not in self.COMMANDS[cmd]: argument_format, short_help, long_help, flag_format = \ cli.util.format_subcommands_help(self.COMMANDS[cmd]) usage = SUBCOMMAND_USAGE.format( plugin=self.PLUGIN_NAME, command=cmd, arguments=argument_format, flags=flag_format, short_help=short_help, long_help=long_help) arguments = docopt( usage, argv=argv, program="mesos " + self.PLUGIN_NAME + " " + cmd, version=self.VERSION, options_first=True) if "alias" in self.COMMANDS[cmd]: cmd = self.COMMANDS[cmd]["alias"] self.__setup__(cmd, argv) getattr(self, cmd.replace("-", "_"))(arguments) else: self.main(["--help"])
def main(argv): """ This is the main function for the Mesos CLI. """ # Load the CLI config. config = cli.config.Config(settings) plugins = cli.util.import_modules( cli.util.join_plugin_paths(settings, config), "plugins") cmds = { cli.util.get_module(plugins, plugin).PLUGIN_NAME: cli.util.get_module(plugins, plugin).SHORT_HELP for plugin in plugins.keys() } # Parse all incoming arguments using docopt. command_strings = "" if cmds != {}: command_strings = cli.util.format_commands_help(cmds) usage = USAGE.format(commands=command_strings) arguments = docopt(usage, argv=argv, version=VERSION, options_first=True) cmd = arguments["<command>"] argv = arguments["<args>"] # Use the meta-command `__autocomplete__` to perform # autocompletion on the remaining arguments. if cmd == "__autocomplete__": option = "default" comp_words = [] # If there is an error performing the autocomplete, treat it # as if we were just unable to complete any words. This avoids # passing the erroring stack trace back as the list of words # to complete on. try: option, comp_words = autocomplete(cmds, plugins, config, argv) except Exception: pass print option print " ".join(comp_words) # Use the meta-command "help" to print help information for the # supplied command and its subcommands. elif cmd == "help": if len(argv) > 0 and argv[0] in cmds: plugin = cli.util.get_module(plugins, argv[0]) plugin_class = getattr(plugin, plugin.PLUGIN_CLASS) plugin_class(settings, config).main(argv[1:] + ["--help"]) else: main(["--help"]) # Run the command through its plugin. elif cmd in cmds.keys(): plugin = cli.util.get_module(plugins, cmd) plugin_class = getattr(plugin, plugin.PLUGIN_CLASS) plugin_class(settings, config).main(argv) # Print help information if no commands match. else: main(["--help"])
def main(argv): """ This is the main function for the Mesos CLI. """ # Initialize the various plugins. plugins = cli.util.import_modules(settings.PLUGINS, "plugins") cmds = { cli.util.get_module(plugins, plugin).PLUGIN_NAME: cli.util.get_module(plugins, plugin).SHORT_HELP for plugin in plugins.keys() } # Parse all incoming arguments using docopt. command_strings = "" if cmds != {}: command_strings = cli.util.format_commands_help(cmds) usage = USAGE.format(commands=command_strings) arguments = docopt(usage, argv=argv, version=VERSION, options_first=True) cmd = arguments["<command>"] argv = arguments["<args>"] # Use the meta-command `__autocomplete__` to perform # autocompletion on the remaining arguments. if cmd == "__autocomplete__": current_word = argv[0] argv = argv[1:] option = "default" comp_words = [] # If there is an error performing the autocomplete, treat it # as if we were just unable to complete any words. This avoids # passing the erroring stack trace back as the list of words # to complete on. try: option, comp_words = autocomplete(cmds, plugins, current_word, argv) except Exception: pass print option print " ".join(comp_words) # Use the meta-command "help" to print help information for the # supplied command and its subcommands. elif cmd == "help": if len(argv) > 0 and argv[0] in cmds: plugin = cli.util.get_module(plugins, argv[0]) plugin_class = getattr(plugin, plugin.PLUGIN_CLASS) plugin_class(settings).main(argv[1:] + ["--help"]) else: main(["--help"]) # Run the command through its plugin. elif cmd in cmds.keys(): plugin = cli.util.get_module(plugins, cmd) plugin_class = getattr(plugin, plugin.PLUGIN_CLASS) plugin_class(settings).main(argv) # Print help information if no commands match. else: main(["--help"])