def main(self, args=None): """Handles execution through command line interface""" # This is so you can see virtstrap starting in the log file logger.debug('------------------- vstrap starting -------------------') # Load all of the available commands if not args: args = sys.argv[1:] self.set_args(args) self.load_commands() parser = self.create_parser() # Get the arguments minus the command name raw_command_args = args[1:] # Parse the known args cli_args, remaining = parser.parse_known_args(args=args) self.handle_global_options(cli_args) command = cli_args.command # Run the command exit_code = 0 try: exit_code = self.run_command(command, cli_args, project=self.project, raw_args=raw_command_args) except CommandDoesNotExist: exit_code = EXIT_FAIL logger.debug('Unknown command "%s"' % command) parser.error('"%s" is not a vstrap command. (use "vstrap help" ' 'to see a list of commands)' % command) except SystemExit, e: if e.code == 0: exit_code = EXIT_OK else: logger.error( "virtstrap did not finish it's" " task correctly check the log. for more information")
def main(self, args=None): """Handles execution through command line interface""" # This is so you can see virtstrap starting in the log file logger.debug('------------------- vstrap starting -------------------') # Load all of the available commands if not args: args = sys.argv[1:] self.set_args(args) self.load_commands() parser = self.create_parser() # Get the arguments minus the command name raw_command_args = args[1:] # Parse the known args cli_args, remaining = parser.parse_known_args(args=args) self.handle_global_options(cli_args) command = cli_args.command # Run the command exit_code = 0 try: exit_code = self.run_command(command, cli_args, project=self.project, raw_args=raw_command_args) except CommandDoesNotExist: exit_code = EXIT_FAIL logger.debug('Unknown command "%s"' % command) parser.error('"%s" is not a vstrap command. (use "vstrap help" ' 'to see a list of commands)' % command) except SystemExit, e: if e.code == 0: exit_code = EXIT_OK else: logger.error("virtstrap did not finish it's" " task correctly check the log. for more information")
def parser_from_commands(commands): """Creates a parser from all the passed in commands""" base_parser = create_base_parser() top_parser = ArgumentParser(parents=[base_parser]) subparsers = top_parser.add_subparsers(help='Commands', metavar='command', dest='command') for command_name, command in commands: command_parser = command.parser if not isinstance(command_parser, ArgumentParser): logger.error('%s does not define parser with argparse. ' 'It will not be included in commands' % command_name) subparsers.add_parser(command_name, help=command.description, add_help=False, parents=[base_parser, command.parser]) return top_parser
def parser_from_commands(commands): """Creates a parser from all the passed in commands""" base_parser = create_base_parser() top_parser = ArgumentParser( parents=[base_parser] ) subparsers = top_parser.add_subparsers(help='Commands', metavar='command', dest='command') for command_name, command in commands: command_parser = command.parser if not isinstance(command_parser, ArgumentParser): logger.error('%s does not define parser with argparse. ' 'It will not be included in commands' % command_name) subparsers.add_parser(command_name, help=command.description, add_help=False, parents=[base_parser, command.parser] ) return top_parser
def collect(self): project = self._project collected_commands = [] if project: virtstrap_bin = constants.PROJECT_VIRTSTRAP_BIN_NAME try: json_data = project.call_bin(virtstrap_bin, ['commands', '--as-json'], collect_stdout=True) except OSError: logger.error('Found a possible project directory at %s' ' but it was not configured correctly.' % project.path()) return collected_commands, [] if json_data: commands_json = json.loads(json_data) command_dicts = commands_json['commands'] for command_dict in command_dicts: command = self.make_local_project_command( command_dict['name'], command_dict['description']) collected_commands.append(command) # Collectors need to return commands and plugins but this collector # does not collect plugins so return an empty list return collected_commands, []