def root_command_main(name, parser, modules, args=None): """ Root Command Main name : name of the main program parser : an instance of ArgumentParser class modules : list of Python modules """ if not args: args = sys.argv[1:] subparsers = parser.add_subparsers(dest="action", title="actions") # A dict name -> python module for the the action action_modules = dict() for module in modules: try: check_module(module) except InvalidAction as err: print("Warning, skipping", module.__name__) print(err) continue name = module.__name__.split(".")[-1] # we want to type `foo bar-baz', and not type `foo bar_baz', # even if "bar-baz" is not a valid module name. name = name.replace("_", "-") configurator = module.configure_parser first_doc_line = module.__doc__.splitlines()[0] action_parser = subparsers.add_parser(name, help=first_doc_line) configurator(action_parser) action_parser.formatter_class = argparse.RawDescriptionHelpFormatter doc_lines = module.__doc__.splitlines() epilog = "\n".join(doc_lines[1:]) if epilog: action_parser.epilog = first_doc_line + "\n" + epilog action_modules[name] = module (help_requested, action) = parse_args_for_help(args) if help_requested: if not action: parser.print_help() else: if action not in action_modules: print("Invalid action!") print("Choose between: ", " ".join(action_modules.keys())) print("") parser.print_help() else: parser.parse_args([action, "--help"]) sys.exit(0) pargs = parser.parse_args(args) ui.configure_logging(pargs) module = action_modules[pargs.action] _dump_arguments(module.__name__, pargs) main_wrapper(module, pargs) return True
(help_requested, action) = parse_args_for_help(args) if help_requested: if not action: parser.print_help() else: if not action in action_modules: print "Invalid action!" print "Choose between: ", " ".join(action_modules.keys()) print parser.print_help() else: parser.parse_args([action, "--help"]) sys.exit(0) pargs = parser.parse_args(args) ui.configure_logging(pargs) module = action_modules[pargs.action] _dump_arguments(module.__name__, pargs) main_wrapper(module, pargs) return True def check_module(module): """Check that a module really is an action. Raises InvalidAction error if not """ if not hasattr(module, "do"): raise InvalidAction(module.__name__, "Could not find a do() method") if not hasattr(module, "configure_parser"): raise InvalidAction(module.__name__, "Could not find a configure_parser() method")
_cmdparse_no_action = True parser_fake = copy.copy(parser) parser_fake.error = fake_error _cmdparse_no_action = False try: parser_fake.parse_known_args(args) except: if _cmdparse_no_action == True: return False else: raise pargs = parser.parse_args(args) ui.configure_logging(pargs) module = action_modules[pargs.action] _dump_arguments(module.__name__, pargs) main_wrapper(module, pargs) return True def check_module(module): """Check that a module really is an action. Raises InvalidAction error if not """ if not hasattr(module, "do"): raise InvalidAction(module.__name__, "Could not find a do() method") if not hasattr(module, "configure_parser"): raise InvalidAction(module.__name__,