def invoke(self, args, initial_invocation_data=None, out_file=None): self.args = args self.register_event(event_name=EVENT_CLI_PRE_EXECUTE, handler=self.pre_execute) self.register_event(event_name=EVENT_CLI_POST_EXECUTE, handler=self.post_execute) return CLI.invoke(self, args, initial_invocation_data, out_file)
def invoke(self, args, initial_invocation_data=None, out_file=None): self.args = args self.register_event(event_name=EVENT_INVOKER_POST_PARSE_ARGS, handler=self.post_parse_args) self.register_event(event_name=EVENT_CLI_POST_EXECUTE, handler=self.post_execute) return CLI.invoke(self, args, initial_invocation_data, out_file)
def test_cli_exapp1(self): def a_test_command_handler(_): return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}] class MyCommandsLoader(CLICommandsLoader): def load_command_table(self, args): self.command_table['abc xyz'] = CLICommand( self.cli_ctx, 'abc xyz', a_test_command_handler) self.command_table['abc list'] = CLICommand( self.cli_ctx, 'abc list', a_test_command_handler) return OrderedDict(self.command_table) mycli = CLI(cli_name='exapp1', config_dir=os.path.expanduser(os.path.join('~', '.exapp1')), commands_loader_cls=MyCommandsLoader) expected_output = """[ { "a": 1, "b": 1234 }, { "a": 3, "b": 4 } ] """ mock_stdout = StringIO() exit_code = mycli.invoke(['abc', 'xyz'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) mock_stdout = StringIO() mycli.invoke(['abc', 'list'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) expected_output = """{ "a": 1, "b": 1234 } """ mock_stdout = StringIO() mycli.invoke(['abc', 'list', '--query', '[0]'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) expected_output = "1\n" mock_stdout = StringIO() mycli.invoke(['abc', 'list', '--query', '[0].a'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code)
def main(): mycli = CLI(cli_name='upnp', commands_loader_cls=CommandsLoader) exit_code = mycli.invoke(sys.argv[1:])
results = stub.Apply(request) print(MessageToJson(results)) class CommandsLoader(CLICommandsLoader): def load_command_table(self, args): with CommandGroup(self, '', '__main__#{}') as g: g.command('serve', 'serve_command_handler', confirmation=False), g.command('analyze', 'analyze_command_handler', confirmation=False) return super(CommandsLoader, self).load_command_table(args) def load_arguments(self, command): with ArgumentsContext(self, 'serve') as ac: ac.argument('env_grpc_port', default=False, required=False) ac.argument('grpc_port', default=3001, type=int, required=False) with ArgumentsContext(self, 'analyze') as ac: ac.argument('env_grpc_port', default=False, required=False) ac.argument('grpc_port', default=3001, type=int, required=False) ac.argument('text', required=True) ac.argument('fields', nargs='*', required=True) super(CommandsLoader, self).load_arguments(command) presidio_cli = CLI(cli_name=cli_name, config_dir=os.path.join('~', '.{}'.format(cli_name)), config_env_var_prefix=cli_name, commands_loader_cls=CommandsLoader, help_cls=PresidioCLIHelp) exit_code = presidio_cli.invoke(sys.argv[1:]) sys.exit(exit_code)
|_| |___/ """ class CommandLoader(CLICommandsLoader): def load_command_table(self, args): with CommandGroup(self, "proxy", "aproxy.proxy#{}") as g: g.command("single", "start_single_proxy") g.command("start", "start_from_config") return super(CommandLoader, self).load_command_table(args) def load_arguments(self, command): with ArgumentsContext(self, "proxy single") as ac: ac.argument("local_port", type=int) ac.argument("remote_port", type=int) return super(CommandLoader, self).load_arguments(command) name = "aproxy" cli = CLI( cli_name=name, config_dir=os.path.expanduser(os.path.join("~", ".{}".format(name))), config_env_var_prefix=name, commands_loader_cls=CommandLoader, ) signal.signal(signal.SIGINT, signal_handler) exit_code = cli.invoke(sys.argv[1:]) sys.exit(exit_code)