def register_commands(): from aria_cli.config.parser_config import parser_config parser_conf = parser_config() parser = argparse.ArgumentParser(description=parser_conf['description']) # Direct arguments for the 'aria' command (like -v) for argument_name, argument in parser_conf['arguments'].iteritems(): parser.add_argument(argument_name, **argument) subparsers = parser.add_subparsers() for command_name, command in parser_conf['commands'].iteritems(): if 'sub_commands' in command: # Add sub commands. Such as 'aria blueprints list', controller_help = command['help'] controller_parser = subparsers.add_parser(command_name, help=controller_help) controller_subparsers = controller_parser.add_subparsers() for controller_sub_command_name, controller_sub_command in \ command['sub_commands'].iteritems(): register_command(controller_subparsers, controller_sub_command_name, controller_sub_command) else: # Add direct commands. register_command(subparsers, command_name, command) return parser
def register_commands(): from aria_cli.config.parser_config import parser_config parser_conf = parser_config() parser = argparse.ArgumentParser(description=parser_conf['description']) # Direct arguments for the 'aria' command (like -v) for argument_name, argument in parser_conf['arguments'].iteritems(): parser.add_argument(argument_name, **argument) subparsers = parser.add_subparsers() for command_name, command in parser_conf['commands'].iteritems(): if 'sub_commands' in command: # Add sub commands. Such as 'aria blueprints list', controller_help = command['help'] controller_parser = subparsers.add_parser( command_name, help=controller_help ) controller_subparsers = controller_parser.add_subparsers() for controller_sub_command_name, controller_sub_command in \ command['sub_commands'].iteritems(): register_command(controller_subparsers, controller_sub_command_name, controller_sub_command) else: # Add direct commands. register_command(subparsers, command_name, command) return parser
def get_all_commands(): all_commands = [] from aria_cli.config.parser_config import parser_config parser_conf = parser_config() for command_name, command in parser_conf['commands'].iteritems(): if 'sub_commands' in command: for sub_command_name in command['sub_commands'].keys(): command_path = '{0} {1}'.format(command_name, sub_command_name) all_commands.append(command_path) else: all_commands.append(command_name) return all_commands
def get_combinations(command_path): def traverse(dictionary, key): if 'sub_commands' in dictionary: return dictionary['sub_commands'][key] return dictionary[key] def type_of(arg): if 'action' in arg and arg['action'] == 'store_true': return bool if 'type' in arg: if isinstance(arg['type'], argparse.FileType): return file if arg['type'] == json.loads: return dict if 'type' not in arg: return str return arg['type'] from aria_cli.config.parser_config import parser_config parser_conf = parser_config() reduced = reduce(traverse, command_path.split(), parser_conf['commands']) if 'arguments' not in reduced: return [] required_args = set() optional_args = set() arguments = reduced['arguments'] for argument_name, argument in arguments.iteritems(): possible_arguments = argument_name.split(',') type_of_argument = type_of(argument) def add_value(arg_name): return '{0} {1}'.format(arg_name, ARG_VALUES[type_of_argument]) if 'required' in argument and argument['required']: required_args.update(map(add_value, possible_arguments)) else: optional_args.update(map(add_value, possible_arguments)) all_commands = [] for i in range(0, len(optional_args) + 1): combs = set(combinations(optional_args, i)) for combination in combs: command = '{0} {1} {2}'.format( command_path, ' '.join(required_args), ' '.join(combination)) all_commands.append(command) return all_commands
def get_combinations(command_path): def traverse(dictionary, key): if 'sub_commands' in dictionary: return dictionary['sub_commands'][key] return dictionary[key] def type_of(arg): if 'action' in arg and arg['action'] == 'store_true': return bool if 'type' in arg: if isinstance(arg['type'], argparse.FileType): return file if arg['type'] == json.loads: return dict if 'type' not in arg: return str return arg['type'] from aria_cli.config.parser_config import parser_config parser_conf = parser_config() reduced = reduce(traverse, command_path.split(), parser_conf['commands']) if 'arguments' not in reduced: return [] required_args = set() optional_args = set() arguments = reduced['arguments'] for argument_name, argument in arguments.iteritems(): possible_arguments = argument_name.split(',') type_of_argument = type_of(argument) def add_value(arg_name): return '{0} {1}'.format(arg_name, ARG_VALUES[type_of_argument]) if 'required' in argument and argument['required']: required_args.update(map(add_value, possible_arguments)) else: optional_args.update(map(add_value, possible_arguments)) all_commands = [] for i in range(0, len(optional_args) + 1): combs = set(combinations(optional_args, i)) for combination in combs: command = '{0} {1} {2}'.format(command_path, ' '.join(required_args), ' '.join(combination)) all_commands.append(command) return all_commands
def get_combinations(command_path): def traverse(dictionary, key): if "sub_commands" in dictionary: return dictionary["sub_commands"][key] return dictionary[key] def type_of(arg): if "action" in arg and arg["action"] == "store_true": return bool if "type" in arg: if isinstance(arg["type"], argparse.FileType): return file if arg["type"] == json.loads: return dict if "type" not in arg: return str return arg["type"] from aria_cli.config.parser_config import parser_config parser_conf = parser_config() reduced = reduce(traverse, command_path.split(), parser_conf["commands"]) if "arguments" not in reduced: return [] required_args = set() optional_args = set() arguments = reduced["arguments"] for argument_name, argument in arguments.iteritems(): possible_arguments = argument_name.split(",") type_of_argument = type_of(argument) def add_value(arg_name): return "{0} {1}".format(arg_name, ARG_VALUES[type_of_argument]) if "required" in argument and argument["required"]: required_args.update(map(add_value, possible_arguments)) else: optional_args.update(map(add_value, possible_arguments)) all_commands = [] for i in range(0, len(optional_args) + 1): combs = set(combinations(optional_args, i)) for combination in combs: command = "{0} {1} {2}".format(command_path, " ".join(required_args), " ".join(combination)) all_commands.append(command) return all_commands