def _create_subparser(self, func: Callable, subparsers: argparse.Action) -> None: """ A subparser adder function that wraps the input function. This helper method implements a subparser adder function given an input function. The core ``__call__`` method uses this function by creating a `functools.partial` object that fixes the ``func`` argument with the function to be decorated, which is then returned as the desired wrapper. Args: func: The function to be wrapped. subparsers: A special action object in the `argparse` module that has a single ``add_parser`` method. """ parser = subparsers.add_parser(self.subcommand, help=self.subcommand_help) func_info = self._parse_func(func) for name, default, arg_help in func_info: parser.add_argument(f'--{name}', type=type(default), default=default, help=arg_help) parser_function = partial(self._parser_function, func, func_info) parser.set_defaults(func=parser_function)
def add_argparse_subparser(subparsers: argparse.Action, source_paths_arg_name: str) -> None: # TODO: Better fix? Have to silence mypy since Action does not have add_parser() and # argparse._SubParserAction is not public. parser = subparsers.add_parser('asm', # type: ignore aliases=['assembler'], help='output assembler for compilation unit') parser.set_defaults(run_command=run) parser.add_argument('-c', '--count', action='store_true', help='Count occurance of different opcodes per global label and in ' 'total if there is more than one global label. Result is printed ' 'as a comment before the generated assembly.') parser.add_argument('-va', '--verbose-asm', action='store_true', help='Tell compiler to output verbose assembler.') parser.add_argument(source_paths_arg_name, nargs=1, help='Source file to output assembler for.', metavar='<file>')
def define_signature(parser: Action): hook_parser = parser.add_parser(HookCommand.NAME, help=HookCommand.DESCRIPTION) hook_parser.add_argument('shell', nargs=1, help='Shell name to hook onto.', metavar='{shell}')
def define_signature(parser: Action): use_parser = parser.add_parser(UseCommand.NAME, help=UseCommand.DESCRIPTION) use_parser.add_argument('version', nargs=1, help='Composer version to use.', metavar='{version}')
def add_loadchat_args(subparser: Action) -> None: parser_help = 'start an interactive session with Qualichat' parser = subparser.add_parser('load', help=parser_help) # type: ignore parser.set_defaults(func=loadchat) # type: ignore path_arg_help = 'the paths for the chats' parser.add_argument('paths', help=path_arg_help, nargs='+') # type: ignore debug_args = ('-d', '--debug') debug_args_help = 'set the logging level to debug' parser.add_argument( # type: ignore *debug_args, help=debug_args_help, action='store_true' )
def add_argparse_subparser(subparsers: argparse.Action, source_paths_arg_name: str) -> None: # TODO: Better fix? Have to silence mypy since Action does not have add_parser() and # argparse._SubParserAction is not public. parser = subparsers.add_parser('analyze', help='run static analyzer') # type: ignore parser.set_defaults(run_command=run) parser.add_argument(source_paths_arg_name, nargs='*', help='Analyze path(s). Directories are recursed. All source code in ' 'project, subject to configuration in .sork, is analyzed if no ' '%(metavar)s is passed or if only %(metavar)s passed is the ' 'project\'s root.', metavar='<path>')
def config( subparsers: argparse.Action, config: Config, infer_type: bool = True, ) -> argparse.Action: ''' Configuration function that returns a pregenerated argparse subparser to add to your CLIs using `xdgconfig`. The subparser has a callback attached to it, which you can call with the arguments from itself. Example : args = vars(parser.parse_args()) callback = args.pop('callback', lambda *a, **kw: None) callback(**args) :param subparsers: The return value of argparse.add_subparsers :type subparsers: argparse._SubParsersAction :param config: The configuration object from xdgconfig. :type config: xdgconfig.Config :param infer_type: Whether to infer types from the value apparent type, defaults to True :type infer_type: bool, optional :return: The modified argparse._SubParsersAction :rtype: argparse._SubParsersAction ''' config_p = subparsers.add_parser( 'config', help=HELP_TEXT, ) config_p.add_argument('config_key', type=str, help='Config key to edit') config_p.add_argument('config_value', type=str, help='Value to assign to the config key.') config_p.add_argument( '--global', action='store_true', dest='_global', help='Whether to assign to value to the global configuration') config_p.set_defaults( callback=partial( config._cli_callback, infer_type=infer_type, # noqa ), ) return subparsers
def add_argparse_subparser(subparsers: argparse.Action, source_paths_arg_name: str) -> None: # TODO: Better fix? Have to silence mypy since Action does not have add_parser() and # argparse._SubParserAction is not public. parser = subparsers.add_parser('check', help='style check source code') # type: ignore parser.set_defaults(run_command=run) parser.add_argument('-c', '--checks', type=str, help='Comma separated list of checks to perform. Overrides ' 'configuration in .sork. Prepend - to disable a check. Regular ' 'expressions may be used. All checks except foo: --checks=-foo . ' 'Checks starting with clang-: --checks=clang-.* .', metavar='<checks>') parser.add_argument(source_paths_arg_name, nargs='*', help='Check path(s). Directories are recursed. All source code in ' 'project, subject to configuration in .sork, is checked if no ' '%(metavar)s is passed or if only %(metavar)s passed is the ' 'project\'s root.', metavar='<path>')
def add_sub_command(sub_parsers: argparse.Action, cmd_name: str) -> argparse.ArgumentParser: sub_command = sub_parsers.add_parser( cmd_name, formatter_class=argparse.ArgumentDefaultsHelpFormatter) return sub_command
def add_setup_args(subparser: Action) -> None: parser_help = 'setup internal libraries' parser = subparser.add_parser('setup', help=parser_help) # type: ignore parser.set_defaults(func=setup) # type: ignore
def define_signature(parser: Action): parser.add_parser(ListCommand.NAME, help=ListCommand.DESCRIPTION)