summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2018-04-21 22:54:29 -0400
committerEric Lin <anselor@gmail.com>2018-04-21 22:54:29 -0400
commit967f320e888fcf671768fc4d7d8838a6f6609b4f (patch)
treeea218390c45be5b161fce9e53025c888e3d84f07
parent965fa83804fec8ba3df8209b253e11acfb015d37 (diff)
downloadcmd2-git-967f320e888fcf671768fc4d7d8838a6f6609b4f.tar.gz
Added some documentation related to the latest changes.
-rw-r--r--CHANGELOG.md2
-rw-r--r--docs/argument_processing.rst10
-rwxr-xr-xexamples/tab_autocompletion.py10
3 files changed, 15 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aa2e785f..bb577994 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,8 @@
* All ``cmd2`` code should be ported to use the new ``argparse``-based decorators
* See the [Argument Processing](http://cmd2.readthedocs.io/en/latest/argument_processing.html) section of the documentation for more information on these decorators
* Alternatively, see the [argparse_example.py](https://github.com/python-cmd2/cmd2/blob/master/examples/argparse_example.py)
+ * Deleted ``cmd_with_subs_completer``, ``get_subcommands``, and ``get_subcommand_completer``
+ * Replaced by default AutoCompleter implementation for all commands using argparse
* Python 2 no longer supported
* ``cmd2`` now supports Python 3.4+
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst
index 183dde4e..ecf59504 100644
--- a/docs/argument_processing.rst
+++ b/docs/argument_processing.rst
@@ -346,12 +346,10 @@ Sub-commands
Sub-commands are supported for commands using either the ``@with_argparser`` or
``@with_argparser_and_unknown_args`` decorator. The syntax for supporting them is based on argparse sub-parsers.
-Also, a convenience function called ``cmd_with_subs_completer`` is available to easily add tab completion to functions
-that implement subcommands. By setting this as the completer of the base command function, the correct completer for
-the chosen subcommand will be called.
+You may add multiple layers of sub-commands for your command. Cmd2 will automatically traverse and tab-complete
+sub-commands for all commands using argparse.
-See the subcommands_ example to learn more about how to use sub-commands in your ``cmd2`` application.
-This example also demonstrates usage of ``cmd_with_subs_completer``. In addition, the docstring for
-``cmd_with_subs_completer`` offers more details.
+See the subcommands_ and tab_autocompletion_ example to learn more about how to use sub-commands in your ``cmd2`` application.
.. _subcommands: https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py
+.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py
index 2c200757..75ea1f00 100755
--- a/examples/tab_autocompletion.py
+++ b/examples/tab_autocompletion.py
@@ -277,7 +277,8 @@ class TabCompleteExample(cmd2.Cmd):
###################################################################################
# The media command demonstrates a completer with multiple layers of subcommands
- # - This example tags a completion attribute on each action
+ # - This example demonstrates how to tag a completion attribute on each action, enabling argument
+ # completion without implementing a complete_COMMAND function
def _do_vid_media_movies(self, args) -> None:
if not args.command:
@@ -320,16 +321,23 @@ class TabCompleteExample(cmd2.Cmd):
vid_movies_list_parser.add_argument('-t', '--title', help='Title Filter')
vid_movies_list_parser.add_argument('-r', '--rating', help='Rating Filter', nargs='+',
choices=ratings_types)
+ # save a reference to the action object
director_action = vid_movies_list_parser.add_argument('-d', '--director', help='Director Filter')
actor_action = vid_movies_list_parser.add_argument('-a', '--actor', help='Actor Filter', action='append')
+
+ # 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, query_actors)
vid_movies_add_parser = vid_movies_commands_subparsers.add_parser('add')
vid_movies_add_parser.add_argument('title', help='Movie Title')
vid_movies_add_parser.add_argument('rating', help='Movie Rating', choices=ratings_types)
+
+ # save a reference to the action object
director_action = vid_movies_add_parser.add_argument('-d', '--director', help='Director', nargs=(1, 2), required=True)
actor_action = vid_movies_add_parser.add_argument('actor', help='Actors', nargs='*')
+
+ # 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, query_actors)