diff options
-rwxr-xr-x | cmd2/argparse_completer.py | 5 | ||||
-rw-r--r-- | cmd2/pyscript_bridge.py | 4 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 15 | ||||
-rw-r--r-- | tests/test_cmd2.py | 38 |
4 files changed, 43 insertions, 19 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index bf6a0e90..1995b8d5 100755 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -565,7 +565,10 @@ class AutoCompleter(object): prefix = '{}{}'.format(flags, param) else: - prefix = '{}'.format(str(action.dest).upper()) + if action.dest != SUPPRESS: + prefix = '{}'.format(str(action.dest).upper()) + else: + prefix = '' prefix = ' {0: <{width}} '.format(prefix, width=20) pref_len = len(prefix) diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index 277d8531..196be82b 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -230,7 +230,7 @@ class ArgparseFunctor: if action.option_strings: cmd_str[0] += '{} '.format(action.option_strings[0]) - if isinstance(value, List) or isinstance(value, Tuple): + if isinstance(value, List) or isinstance(value, tuple): for item in value: item = str(item).strip() if ' ' in item: @@ -250,7 +250,7 @@ class ArgparseFunctor: cmd_str[0] += '{} '.format(self._args[action.dest]) traverse_parser(action.choices[self._args[action.dest]]) elif isinstance(action, argparse._AppendAction): - if isinstance(self._args[action.dest], List) or isinstance(self._args[action.dest], Tuple): + if isinstance(self._args[action.dest], list) or isinstance(self._args[action.dest], tuple): for values in self._args[action.dest]: process_flag(action, values) else: diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index 65679e4f..adfe9702 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -235,17 +235,22 @@ class TabCompleteExample(cmd2.Cmd): actor_action = vid_movies_add_parser.add_argument('actor', help='Actors', nargs='*') vid_movies_load_parser = vid_movies_commands_subparsers.add_parser('load') - movie_file_action = vid_movies_load_parser.add_argument('movie_file', help='Movie database') + vid_movie_file_action = vid_movies_load_parser.add_argument('movie_file', help='Movie database') + + vid_movies_read_parser = vid_movies_commands_subparsers.add_parser('read') + vid_movie_fread_action = vid_movies_read_parser.add_argument('movie_file', help='Movie database') # tag the action objects with completion providers. This can be a collection or a callable setattr(director_action, argparse_completer.ACTION_ARG_CHOICES, static_list_directors) setattr(actor_action, argparse_completer.ACTION_ARG_CHOICES, 'instance_query_actors') # tag the file property with a custom completion function 'delimeter_complete' provided by cmd2. - setattr(movie_file_action, argparse_completer.ACTION_ARG_CHOICES, + setattr(vid_movie_file_action, argparse_completer.ACTION_ARG_CHOICES, ('delimiter_complete', {'delimiter': '/', 'match_against': file_list})) + setattr(vid_movie_fread_action, argparse_completer.ACTION_ARG_CHOICES, + ('path_complete', [False, False])) vid_movies_delete_parser = vid_movies_commands_subparsers.add_parser('delete') @@ -324,6 +329,9 @@ class TabCompleteExample(cmd2.Cmd): movies_delete_parser = movies_commands_subparsers.add_parser('delete') + movies_load_parser = movies_commands_subparsers.add_parser('load') + movie_file_action = movies_load_parser.add_argument('movie_file', help='Movie database') + shows_parser = media_types_subparsers.add_parser('shows') shows_parser.set_defaults(func=_do_media_shows) @@ -351,7 +359,8 @@ class TabCompleteExample(cmd2.Cmd): def complete_media(self, text, line, begidx, endidx): """ Adds tab completion to media""" choices = {'actor': query_actors, # function - 'director': TabCompleteExample.static_list_directors # static list + 'director': TabCompleteExample.static_list_directors, # static list + 'movie_file': (self.path_complete, [False, False]) } completer = argparse_completer.AutoCompleter(TabCompleteExample.media_parser, arg_choices=choices) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 6e4a5a3e..89aa9d03 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -406,21 +406,26 @@ def test_history_output_file(base_app): content = normalize(f.read()) assert content == expected -def test_history_edit(base_app, monkeypatch): +def test_history_edit(base_app, mock): # Set a fake editor just to make sure we have one. We aren't really # going to call it due to the mock base_app.editor = 'fooedit' # Mock out the os.system call so we don't actually open an editor - m = mock.MagicMock(name='system') - monkeypatch.setattr("os.system", m) + count = [0] + + def fake_system(*args, **kwargs): + count[0] += 1 + + mock.patch.object(os, 'system', fake_system) # Run help command just so we have a command in history run_cmd(base_app, 'help') run_cmd(base_app, 'history -e 1') # We have an editor, so should expect a system call - m.assert_called_once() + assert count[0] == 1 + def test_history_run_all_commands(base_app): # make sure we refuse to run all commands as a default @@ -819,29 +824,36 @@ def test_edit_file_with_spaces(base_app, request, monkeypatch): # We think we have an editor, so should expect a system call m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename)) -def test_edit_blank(base_app, monkeypatch): +def test_edit_blank(base_app, mock): # Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock base_app.editor = 'fooedit' - # Mock out the os.system call so we don't actually open an editor - m = mock.MagicMock(name='system') - monkeypatch.setattr("os.system", m) + count = [0] + + def fake_system(*args, **kwargs): + count[0] += 1 + + mock.patch.object(os, 'system', fake_system) run_cmd(base_app, 'edit') # We have an editor, so should expect a system call - m.assert_called_once() + assert count[0] == 1 -def test_base_py_interactive(base_app): +def test_base_py_interactive(base_app, mock): # Mock out the InteractiveConsole.interact() call so we don't actually wait for a user's response on stdin - m = mock.MagicMock(name='interact') - InteractiveConsole.interact = m + count = [0] + + def fake_interact(*args, **kwargs): + count[0] += 1 + + mock.patch.object(InteractiveConsole, 'interact', fake_interact) run_cmd(base_app, "py") # Make sure our mock was called once and only once - m.assert_called_once() + assert count[0] == 1 def test_exclude_from_history(base_app, monkeypatch): |