diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-02 19:30:50 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-02 19:30:50 -0400 |
commit | 5014fecf58bb546a453c57eaff7a226f29bfba46 (patch) | |
tree | 929202741e1456f4e01aab29c0000415889d8dcc /cmd2/cmd2.py | |
parent | b10cc8f39e94e60d9d6adbd4f2ca19f1866cd9ca (diff) | |
download | cmd2-git-5014fecf58bb546a453c57eaff7a226f29bfba46.tar.gz |
Patched argparse._ActionsContainer.add_argument() to support more settings like enabling tab completion and providing choice generating functions
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 88 |
1 files changed, 36 insertions, 52 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index da2e83b3..0272a7c4 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -46,7 +46,7 @@ from . import ansi from . import constants from . import plugin from . import utils -from .argparse_completer import AutoCompleter, ACArgumentParser, set_arg_choices_method, set_arg_completer_method +from .argparse_completer import AutoCompleter, ACArgumentParser from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer from .history import History, HistoryItem from .parsing import StatementParser, Statement, Macro, MacroArg, shlex_split @@ -2381,11 +2381,10 @@ class Cmd(cmd.Cmd): description=alias_create_description, epilog=alias_create_epilog) alias_create_parser.add_argument('name', help='name of this alias') - set_arg_choices_method(alias_create_parser.add_argument('command', help='what the alias resolves to'), - _get_commands_aliases_and_macros_for_completion) - set_arg_completer_method(alias_create_parser.add_argument('command_args', nargs=argparse.REMAINDER, - help='arguments to pass to command'), - path_complete) + alias_create_parser.add_argument('command', help='what the alias resolves to', + choices_method=_get_commands_aliases_and_macros_for_completion) + alias_create_parser.add_argument('command_args', nargs=argparse.REMAINDER, help='arguments to pass to command', + completer_method=path_complete) alias_create_parser.set_defaults(func=_alias_create) # alias -> delete @@ -2393,8 +2392,7 @@ class Cmd(cmd.Cmd): alias_delete_description = "Delete specified aliases or all aliases if --all is used" alias_delete_parser = alias_subparsers.add_parser('delete', help=alias_delete_help, description=alias_delete_description) - set_arg_choices_method(alias_delete_parser.add_argument('name', nargs='*', help='alias to delete'), - _get_alias_names) + alias_delete_parser.add_argument('name', nargs='*', help='alias to delete', choices_method=_get_alias_names) alias_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all aliases") alias_delete_parser.set_defaults(func=_alias_delete) @@ -2407,8 +2405,7 @@ class Cmd(cmd.Cmd): alias_list_parser = alias_subparsers.add_parser('list', help=alias_list_help, description=alias_list_description) - set_arg_choices_method(alias_list_parser.add_argument('name', nargs="*", help='alias to list'), - _get_alias_names) + alias_list_parser.add_argument('name', nargs="*", help='alias to list', choices_method=_get_alias_names) alias_list_parser.set_defaults(func=_alias_list) # Preserve quotes since we are passing strings to other commands @@ -2586,11 +2583,10 @@ class Cmd(cmd.Cmd): description=macro_create_description, epilog=macro_create_epilog) macro_create_parser.add_argument('name', help='name of this macro') - set_arg_choices_method(macro_create_parser.add_argument('command', help='what the macro resolves to'), - _get_commands_aliases_and_macros_for_completion) - set_arg_completer_method(macro_create_parser.add_argument('command_args', nargs=argparse.REMAINDER, - help='arguments to pass to command'), - path_complete) + macro_create_parser.add_argument('command', help='what the macro resolves to', + choices_method=_get_commands_aliases_and_macros_for_completion) + macro_create_parser.add_argument('command_args', nargs=argparse.REMAINDER, + help='arguments to pass to command', completer_method=path_complete) macro_create_parser.set_defaults(func=_macro_create) # macro -> delete @@ -2598,8 +2594,7 @@ class Cmd(cmd.Cmd): macro_delete_description = "Delete specified macros or all macros if --all is used" macro_delete_parser = macro_subparsers.add_parser('delete', help=macro_delete_help, description=macro_delete_description) - set_arg_choices_method(macro_delete_parser.add_argument('name', nargs='*', help='macro to delete'), - _get_macro_names) + macro_delete_parser.add_argument('name', nargs='*', help='macro to delete', choices_method=_get_macro_names) macro_delete_parser.add_argument('-a', '--all', action='store_true', help="delete all macros") macro_delete_parser.set_defaults(func=_macro_delete) @@ -2611,8 +2606,7 @@ class Cmd(cmd.Cmd): "Without arguments, all macros will be listed.") macro_list_parser = macro_subparsers.add_parser('list', help=macro_list_help, description=macro_list_description) - set_arg_choices_method(macro_list_parser.add_argument('name', nargs="*", help='macro to list'), - _get_macro_names) + macro_list_parser.add_argument('name', nargs="*", help='macro to list', choices_method=_get_macro_names) macro_list_parser.set_defaults(func=_macro_list) # Preserve quotes since we are passing strings to other commands @@ -2671,11 +2665,10 @@ class Cmd(cmd.Cmd): return matches help_parser = ACArgumentParser() - set_arg_completer_method(help_parser.add_argument('command', nargs="?", help="command to retrieve help for"), - complete_help_command) - set_arg_completer_method(help_parser.add_argument('subcommand', nargs=argparse.REMAINDER, - help="sub-command to retrieve help for"), - complete_help_subcommand) + help_parser.add_argument('command', nargs="?", help="command to retrieve help for", + completer_method=complete_help_command) + help_parser.add_argument('subcommand', nargs=argparse.REMAINDER, help="sub-command to retrieve help for", + completer_method=complete_help_subcommand) help_parser.add_argument('-v', '--verbose', action='store_true', help="print a list of all commands with descriptions of each") @@ -2944,8 +2937,7 @@ class Cmd(cmd.Cmd): set_parser = ACArgumentParser(description=set_description) set_parser.add_argument('-a', '--all', action='store_true', help='display read-only settings as well') set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') - set_arg_choices_method(set_parser.add_argument('param', nargs='?', help='parameter to set or view'), - _get_settable_names) + set_parser.add_argument('param', nargs='?', help='parameter to set or view', choices_method=_get_settable_names) set_parser.add_argument('value', nargs='?', help='the new value for settable') @with_argparser(set_parser) @@ -2987,11 +2979,9 @@ class Cmd(cmd.Cmd): onchange_hook(old=orig_value, new=new_value) shell_parser = ACArgumentParser() - set_arg_completer_method(shell_parser.add_argument('command', help='the command to run'), - shell_cmd_complete) - set_arg_completer_method(shell_parser.add_argument('command_args', nargs=argparse.REMAINDER, - help='arguments to pass to command'), - path_complete) + shell_parser.add_argument('command', help='the command to run', completer_method=shell_cmd_complete) + shell_parser.add_argument('command_args', nargs=argparse.REMAINDER, help='arguments to pass to command', + completer_method=path_complete) # Preserve quotes since we are passing these strings to the shell @with_argparser(shell_parser, preserve_quotes=True) @@ -3238,11 +3228,9 @@ class Cmd(cmd.Cmd): return bridge.stop run_pyscript_parser = ACArgumentParser() - set_arg_completer_method(run_pyscript_parser.add_argument('script_path', help='path to the script file'), - path_complete) - set_arg_completer_method(run_pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, - help='arguments to pass to script'), - path_complete) + run_pyscript_parser.add_argument('script_path', help='path to the script file', completer_method=path_complete) + run_pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, + help='arguments to pass to script', completer_method=path_complete) @with_argparser(run_pyscript_parser) def do_run_pyscript(self, args: argparse.Namespace) -> bool: @@ -3300,13 +3288,12 @@ class Cmd(cmd.Cmd): history_action_group.add_argument('-r', '--run', action='store_true', help='run selected history items') history_action_group.add_argument('-e', '--edit', action='store_true', help='edit and then run selected history items') - set_arg_completer_method(history_action_group.add_argument('-o', '--output-file', metavar='FILE', - help='output commands to a script file, implies -s'), - path_complete) - set_arg_completer_method(history_action_group.add_argument('-t', '--transcript', - help='output commands and results to a transcript\n' - 'file, implies -s'), - path_complete) + history_action_group.add_argument('-o', '--output-file', metavar='FILE', + help='output commands to a script file, implies -s', + completer_method=path_complete) + history_action_group.add_argument('-t', '--transcript', help='output commands and results to a transcript file,\n' + 'implies -s', + completer_method=path_complete) history_action_group.add_argument('-c', '--clear', action='store_true', help='clear all history') history_format_group = history_parser.add_argument_group(title='formatting') @@ -3596,8 +3583,8 @@ class Cmd(cmd.Cmd): " set editor (program-name)") edit_parser = ACArgumentParser(description=edit_description) - set_arg_completer_method(edit_parser.add_argument('file_path', nargs="?", help="path to a file to open in editor"), - path_complete) + edit_parser.add_argument('file_path', nargs="?", + help="path to a file to open in editor", completer_method=path_complete) @with_argparser(edit_parser) def do_edit(self, args: argparse.Namespace) -> None: @@ -3625,15 +3612,12 @@ class Cmd(cmd.Cmd): "typed in the console.\n" "\n" "If the -r/--record_transcript flag is used, this command instead records\n" - "the output of the script commands to a transcript for testing purposes.\n" - ) + "the output of the script commands to a transcript for testing purposes.\n") run_script_parser = ACArgumentParser(description=run_script_description) - set_arg_completer_method(run_script_parser.add_argument('-t', '--transcript', help='record the output of the ' - 'script as a transcript file'), - path_complete) - set_arg_completer_method(run_script_parser.add_argument('script_path', help="path to the script file"), - path_complete) + run_script_parser.add_argument('-t', '--transcript', help='record the output of the script as a transcript file', + completer_method=path_complete) + run_script_parser.add_argument('script_path', help="path to the script file", completer_method=path_complete) @with_argparser(run_script_parser) def do_run_script(self, args: argparse.Namespace) -> Optional[bool]: |