def test_parse_fail(): try: Args.parse( "-p -c /tmp something".split(" "), positionals_spec=STR_PARAM, options_spec=[ (["-p", "--port"], INT_PARAM), (["-c", "--config"], STR_PARAM), ] ) raise AssertionError() except ArgsParseError: pass
def _set(self, args: Args) -> AnyErr: """ set - show or set a setting """ setting_to_set = args.get_positionals() log.d(f"setting_to_set: {setting_to_set}") if setting_to_set and len(setting_to_set) == 2: key, val = setting_to_set log.i(f"set {key}={val}") if key and val: try: set_setting(key, val) except Exception as ex: log.w(f"Failed to set: {ex}") return str(ex) else: log.w(f"Unable to parse setting: {setting_to_set}") return ClientErrors.INVALID_COMMAND_SYNTAX else: # Show aliases log.d("No setting given, showing current ones") for key, val in settings._settings_values.items(): print(f"set {key}={val}") return ClientErrors.SUCCESS
def _alias(self, args: Args) -> AnyErr: """ alias - show or create an alias """ alias_to_create = args.get_positionals() log.d(f"alias_to_create: {alias_to_create}") if alias_to_create and len(alias_to_create) == 2: # Create aliases source, target = alias_to_create if source and target: log.i(f"Adding alias: {source}={target}") self._aliases[source] = target comm_info = COMMANDS_INFO.get(self._command_for(source)) # If comm_info is None, the command is uknown or it is a multiple # command, add as a null comm_info self._available_commands[source] = comm_info else: log.w(f"Unable to parse alias: {alias_to_create}") return ClientErrors.INVALID_COMMAND_SYNTAX else: # Show aliases log.d("No alias given, showing current ones") for source, target in self._aliases.items(): print(f"alias {source}={target}") return ClientErrors.SUCCESS
def _help(self, args: Args) -> AnyErr: cmd = args.get_positional() if not cmd: ok = command_man("usage") else: ok = False commands = self._commands_for(cmd, resolve_alias=False) target = self._resolve_alias(cmd, recursive=False) if target: print(f"alias {cmd}={target}") ok = True else: log.w(f"Neither a command nor an alias: '{cmd}'") if commands: if len(commands) == 1: command = commands[0] log.d(f"{cmd} resolve into known {command}") ok = command_man(command) elif len(commands) > 1: print("Available commands: ") for comm in isorted(commands): print(red(cmd) + comm[len(cmd):]) ok = True if not ok: print(f"Can't provide help for '{cmd}'") return ClientErrors.SUCCESS
def _trace(args: Args) -> AnyErr: """ trace - changes the tracing level """ # Increase tracing level (or disable if is already max) level = args.get_positional( default=(get_setting(Settings.TRACING) + 1) % (TRACING_MAX + 1)) log.i(f">> TRACING ({level})") set_setting(Settings.TRACING, level) return ClientErrors.SUCCESS
def test_variadic(): args = Args.parse( "-a 1 2 3 4 -c /tmp something".split(" "), positionals_spec=STR_PARAM, options_spec=[ (["-a", "--add"], VARIADIC_PARAMS), (["-c", "--config"], STR_PARAM), ] ) assert args.get_option_params(["-a", "--add"]) ==["1", "2", "3", "4"] assert args.get_option_param(["-c", "--config"]) == "/tmp"
def _verbose(args: Args) -> AnyErr: """ verbose - changes the verbosity level """ # Increase verbosity (or disable if is already max) verbosity = args.get_positional( default=(get_setting(Settings.VERBOSITY) + 1) % (VERBOSITY_MAX + 1)) log.i(f">> VERBOSE ({verbosity})") set_setting(Settings.VERBOSITY, verbosity) return ClientErrors.SUCCESS
def test_parse_success(): print("test_parse_success ----------") args = Args.parse( "-p 12020 -c /tmp something".split(" "), positionals_spec=STR_PARAM, options_spec=[ (["-p", "--port"], INT_PARAM), (["-c", "--config"], STR_PARAM), ] ) assert args.get_option_param(["-p", "--port"]) == 12020 assert args.get_option_param(["-c", "--config"]) == "/tmp" assert args.get_positional() == "something"