def run(self, argv): debug = False parser = self.parser if len(argv) == 0: # Print a more user-friendly help string if no arguments are provided # Note: We only set usage variable for the main parser. If we passed "usage" argument # to the main ArgumentParser class above, this would also set a custom usage string for # sub-parsers which we don't want. parser.usage = USAGE_STRING sys.stderr.write(parser.format_help()) return 2 # Provide autocomplete for shell argcomplete.autocomplete(self.parser) if '--print-config' in argv: # Hack because --print-config requires no command to be specified argv = argv + ['action', 'list'] # Parse command line arguments. args = self.parser.parse_args(args=argv) print_config = args.print_config if print_config: self._print_config(args=args) return 3 # Parse config and store it in the config module config = self._parse_config_file(args=args) set_config(config=config) # Setup client and run the command try: debug = getattr(args, 'debug', False) if debug: set_log_level_for_all_loggers(level=logging.DEBUG) # Set up client. self.client = self.get_client(args=args, debug=debug) # Execute command. args.func(args) return 0 except OperationFailureException as e: if debug: self._print_debug_info(args=args) return 2 except Exception as e: # We allow exception to define custom exit codes exit_code = getattr(e, 'exit_code', 1) print('ERROR: %s\n' % e) if debug: self._print_debug_info(args=args) return exit_code
def run(self, argv): debug = False # Provide autocomplete for shell argcomplete.autocomplete(self.parser) if '--print-config' in argv: # Hack because --print-config requires no command to be specified argv = argv + ['action', 'list'] # Parse command line arguments. args = self.parser.parse_args(args=argv) print_config = args.print_config if print_config: self._print_config(args=args) return 3 # Parse config and store it in the config module config = self._parse_config_file(args=args) set_config(config=config) # Setup client and run the command try: debug = getattr(args, 'debug', False) if debug: set_log_level_for_all_loggers(level=logging.DEBUG) # Set up client. self.client = self.get_client(args=args, debug=debug) # Execute command. args.func(args) return 0 except OperationFailureException as e: if debug: self._print_debug_info(args=args) return 2 except Exception as e: # We allow exception to define custom exit codes exit_code = getattr(e, 'exit_code', 1) print('ERROR: %s\n' % e) if debug: self._print_debug_info(args=args) return exit_code
def run(self, argv): debug = False parser = self.parser if len(argv) == 0: # Print a more user-friendly help string if no arguments are provided # Note: We only set usage variable for the main parser. If we passed "usage" argument # to the main ArgumentParser class above, this would also set a custom usage string for # sub-parsers which we don't want. parser.usage = USAGE_STRING sys.stderr.write(parser.format_help()) return 2 # Provide autocomplete for shell argcomplete.autocomplete(self.parser) if '--print-config' in argv: # Hack because --print-config requires no command to be specified argv = argv + ['action', 'list'] # Parse command line arguments. args = self.parser.parse_args(args=argv) print_config = args.print_config if print_config: self._print_config(args=args) return 3 # Parse config and store it in the config module config = self._parse_config_file(args=args, validate_config_permissions=False) set_config(config=config) self._check_locale_and_print_warning() # Setup client and run the command try: debug = getattr(args, 'debug', False) if debug: set_log_level_for_all_loggers(level=logging.DEBUG) # Set up client. self.client = self.get_client(args=args, debug=debug) # TODO: This is not so nice work-around for Python 3 because of a breaking change in # Python 3 - https://bugs.python.org/issue16308 try: func = getattr(args, 'func') except AttributeError: parser.print_help() sys.exit(2) # Execute command. func(args) return 0 except OperationFailureException as e: if debug: self._print_debug_info(args=args) return 2 except Exception as e: # We allow exception to define custom exit codes exit_code = getattr(e, 'exit_code', 1) print('ERROR: %s\n' % e) if debug: self._print_debug_info(args=args) return exit_code
def run(self, argv): debug = False parser = self.parser if len(argv) == 0: # Print a more user-friendly help string if no arguments are provided # Note: We only set usage variable for the main parser. If we passed "usage" argument # to the main ArgumentParser class above, this would also set a custom usage string for # sub-parsers which we don't want. parser.usage = USAGE_STRING sys.stderr.write(parser.format_help()) return 2 # Provide autocomplete for shell argcomplete.autocomplete(self.parser) if "--print-config" in argv: # Hack because --print-config requires no command to be specified argv = argv + ["action", "list"] # Parse command line arguments. args = self.parser.parse_args(args=argv) print_config = args.print_config if print_config: self._print_config(args=args) return 3 # Parse config and store it in the config module config = self._parse_config_file(args=args, validate_config_permissions=False) set_config(config=config) self._check_locale_and_print_warning() # Setup client and run the command try: debug = getattr(args, "debug", False) if debug: set_log_level_for_all_loggers(level=logging.DEBUG) # Set up client. self.client = self.get_client(args=args, debug=debug) # TODO: This is not so nice work-around for Python 3 because of a breaking change in # Python 3 - https://bugs.python.org/issue16308 try: func = getattr(args, "func") except AttributeError: parser.print_help() sys.exit(2) # Execute command. func(args) return 0 except OperationFailureException: if debug: self._print_debug_info(args=args) return 2 except Exception as e: # We allow exception to define custom exit codes exit_code = getattr(e, "exit_code", 1) print("ERROR: %s\n" % e) if debug: self._print_debug_info(args=args) return exit_code