Skip to content

Commit d591679

Browse files
authored
Merge pull request #799 from python-cmd2/doc_updates
Doc updates
2 parents a18eef6 + 36796c2 commit d591679

19 files changed

+551
-98
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ Instructions for implementing each feature follow.
102102

103103
- Searchable command history
104104
- Readline history using `<Ctrl>+r`, arrow keys, and other [Readline Shortcut keys](http://readline.kablamo.org/emacs.html)
105-
- Readline history can be persistent between application runs via optional argument to `cmd2.Cmd` initializer
106105
- `cmd2` `history` command provides flexible and powerful search
107-
- By design, this history does NOT persist between application runs
108106
- If you wish to exclude some of your custom commands from the history, append their names to the list at `Cmd.exclude_from_history`.
109107
- Do `help history` in any `cmd2` application for more information
108+
- Both of the above types of history can be optionally persistent between application runs
109+
- Via optional `persistent_history_file` argument to `cmd2.Cmd` initializer
110110

111111
- Simple scripting using text files with one command + arguments per line
112112
- See the [Command Scripts](https://cmd2.readthedocs.io/en/latest/features/scripting.html#command-scripts) section of the `cmd2` docs for more info

docs/features/argument_processing.rst

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.. _decorators:
2-
31
Argument Processing
42
===================
53

@@ -33,19 +31,20 @@ applications.
3331
.. _argprint: https://github.com/python-cmd2/cmd2/blob/master/examples/arg_print.py
3432
.. _decorator: https://github.com/python-cmd2/cmd2/blob/master/examples/decorator_example.py
3533

34+
.. _decorators:
3635

3736
Decorators provided by cmd2 for argument processing
3837
---------------------------------------------------
3938

4039
``cmd2`` provides the following decorators for assisting with parsing arguments
4140
passed to commands:
4241

43-
.. automethod:: cmd2.decorators.with_argument_list
44-
:noindex:
4542
.. automethod:: cmd2.decorators.with_argparser
4643
:noindex:
4744
.. automethod:: cmd2.decorators.with_argparser_and_unknown_args
4845
:noindex:
46+
.. automethod:: cmd2.decorators.with_argument_list
47+
:noindex:
4948

5049
All of these decorators accept an optional **preserve_quotes** argument which
5150
defaults to ``False``. Setting this argument to ``True`` is useful for cases
@@ -342,3 +341,20 @@ use subcommands in your ``cmd2`` application.
342341

343342
.. _subcommands: https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py
344343
.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py
344+
345+
346+
Argparse Extensions
347+
-------------------
348+
349+
``cmd2`` augments the standard ``argparse.nargs`` with range tuple capability:
350+
351+
- ``nargs=(5,)`` - accept 5 or more items
352+
- ``nargs=(8, 12)`` - accept 8 to 12 items
353+
354+
``cmd2`` also provides the ``Cmd2ArgumentParser`` class which inherits from
355+
``argparse.ArgumentParser`` and improves error and help output:
356+
357+
.. autoclass:: cmd2.argparse_custom.Cmd2ArgumentParser
358+
:members:
359+
360+

docs/features/completion.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,50 @@ similar to the following to your class which inherits from ``cmd2.Cmd``::
3131

3232
# Make sure you have an "import functools" somewhere at the top
3333
complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, path_filter=os.path.isdir)
34+
35+
36+
Tab Completion Using Argparse Decorators
37+
----------------------------------------
38+
39+
When using one the Argparse-based :ref:`decorators`, ``cmd2`` provides
40+
automatic tab-completion of flag names.
41+
42+
Tab-completion of argument values can be configured by using one of five
43+
parameters to ``argparse.ArgumentParser.add_argument()``
44+
45+
- ``choices``
46+
- ``choices_function`` / ``choices_method``
47+
- ``completer_function`` / ``completer_method``
48+
49+
See the arg_decorators_ or colors_ example for a demonstration of how to
50+
use the ``choices`` parameter. See the tab_autocompletion_ example for a
51+
demonstration of how to use the ``choices_function`` and ``choices_method``
52+
parameters. See the arg_decorators_ or tab_autocompletion_ example for a
53+
demonstration of how to use the ``completer_method`` parameter.
54+
55+
When tab-completing flags and/or argument values for a ``cmd2`` command using
56+
one of these decorators, ``cmd2`` keeps track of state so that once a flag has
57+
already previously been provided, it won't attempt to tab-complete it again.
58+
When no completion results exists, a hint for the current argument will be
59+
displayed to help the user.
60+
61+
.. _arg_decorators: https://github.com/python-cmd2/cmd2/blob/master/examples/arg_decorators.py
62+
.. _colors: https://github.com/python-cmd2/cmd2/blob/master/examples/colors.py
63+
.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion..py
64+
65+
66+
CompletionItem For Providing Extra Context
67+
------------------------------------------
68+
69+
When tab-completing things like a unique ID from a database, it can often be
70+
beneficial to provide the user with some extra context about the item being
71+
completed, such as a description. To facilitate this, ``cmd2`` defines the
72+
``CompletionItem`` class which can be returned from any of the 4 completion
73+
functions: ``choices_function``, ``choices_method``, ``completion_function``,
74+
or ``completion_method``.
75+
76+
.. autoclass:: cmd2.argparse_custom.CompletionItem
77+
:members:
78+
79+
See the tab_autocompletion_ example or the implementation of the built-in
80+
**set** command for demonstration of how this is used.

docs/features/embedded_python_shells.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ IPython_ provides many advantages, including:
126126
* Get help on objects with ``?``
127127
* Extensible tab completion, with support by default for completion of
128128
python variables and keywords
129+
* Good built-in ipdb_ debugger
129130

130131
The object introspection and tab completion make IPython particularly efficient
131132
for debugging as well as for interactive experimentation and data analysis.
132133

133134
.. _IPython: http://ipython.readthedocs.io
135+
.. _ipdb: https://pypi.org/project/ipdb/
134136

135137

docs/features/generating_output.rst

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,6 @@ messages. Users can control whether they would like to see these messages by
4242
changing the value of the ``quiet`` setting.
4343

4444

45-
Output Redirection
46-
------------------
47-
48-
As in a Unix shell, output of a command can be redirected:
49-
50-
- sent to a file with ``>``, as in ``mycommand args > filename.txt``
51-
- appended to a file with ``>>``, as in ``mycommand args >> filename.txt``
52-
- piped (``|``) as input to operating-system commands, as in
53-
``mycommand args | wc``
54-
55-
56-
57-
.. note::
58-
59-
If you wish to disable cmd2's output redirection and pipes features, you can
60-
do so by setting the ``allow_redirection`` attribute of your ``cmd2.Cmd``
61-
class instance to ``False``. This would be useful, for example, if you want
62-
to restrict the ability for an end user to write to disk or interact with
63-
shell commands for security reasons::
64-
65-
from cmd2 import Cmd
66-
class App(Cmd):
67-
def __init__(self):
68-
self.allow_redirection = False
69-
70-
cmd2's parser will still treat the ``>``, ``>>``, and `|` symbols as output
71-
redirection and pipe symbols and will strip arguments after them from the
72-
command line arguments accordingly. But output from a command will not be
73-
redirected to a file or piped to a shell command.
74-
75-
If you need to include any of these redirection characters in your command, you
76-
can enclose them in quotation marks, ``mycommand 'with > in the argument'``.
77-
78-
7945
Colored Output
8046
--------------
8147

docs/features/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Features
2020
os
2121
plugins
2222
prompt
23+
redirection
2324
scripting
2425
settings
2526
shortcuts_aliases_macros

docs/features/initialization.rst

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
Initialization
22
==============
33

4-
Show how to properly initialize a ``cmd2`` app, showing parameters, sequencing,
5-
etc.
4+
Here is a basic example ``cmd2`` application which demonstrates many
5+
capabilities which you may wish to utilize while initializing the app::
6+
7+
#!/usr/bin/env python3
8+
# coding=utf-8
9+
"""A simple example cmd2 appliction demonstrating the following:
10+
1) Colorizing/stylizing output
11+
2) Using multiline commands
12+
3) Persistent history
13+
4) How to run an initialization script at startup
14+
5) How to group and categorize commands when displaying them in help
15+
6) Opting-in to using the ipy command to run an IPython shell
16+
7) Allowing access to your application in py and ipy
17+
8) Displaying an intro banner upon starting your application
18+
9) Using a custom prompt
19+
"""
20+
import cmd2
21+
from cmd2 import style
22+
23+
24+
class BasicApp(cmd2.Cmd):
25+
CUSTOM_CATEGORY = 'My Custom Commands'
26+
27+
def __init__(self):
28+
super().__init__(multiline_commands=['echo'], persistent_history_file='cmd2_history.dat',
29+
startup_script='scripts/startup.txt', use_ipython=True)
30+
31+
self.intro = style('Welcome to cmd2!', fg='red', bg='white', bold=True)
32+
self.prompt = 'myapp> '
33+
34+
# Allow access to your application in py and ipy via self
35+
self.locals_in_py = True
36+
37+
# Set the default category name
38+
self.default_category = 'cmd2 Built-in Commands'
39+
40+
@cmd2.with_category(CUSTOM_CATEGORY)
41+
def do_intro(self, _):
42+
"""Display the intro banner"""
43+
self.poutput(self.intro)
44+
45+
@cmd2.with_category(CUSTOM_CATEGORY)
46+
def do_echo(self, arg):
47+
"""Example of a multiline command"""
48+
self.poutput(arg)
49+
50+
51+
if __name__ == '__main__':
52+
app = BasicApp()
53+
app.cmdloop()

docs/features/misc.rst

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,55 +30,6 @@ to type::
3030
(Cmd) !ls -al
3131

3232

33-
Commands At Invocation
34-
----------------------
35-
36-
.. _Argparse: https://docs.python.org/3/library/argparse.html
37-
38-
You can send commands to your app as you invoke it by including them as extra
39-
arguments to the program. ``cmd2`` interprets each argument as a separate
40-
command, so you should enclose each command in quotation marks if it is more
41-
than a one-word command.
42-
43-
.. code-block:: shell
44-
45-
$ python examples/example.py "say hello" "say Gracie" quit
46-
hello
47-
Gracie
48-
49-
50-
.. note::
51-
52-
If you wish to disable cmd2's consumption of command-line arguments, you can
53-
do so by setting the ``allow_cli_args`` argument of your ``cmd2.Cmd`` class
54-
instance to ``False``. This would be useful, for example, if you wish to
55-
use something like Argparse_ to parse the overall command line arguments for
56-
your application::
57-
58-
from cmd2 import Cmd
59-
class App(Cmd):
60-
def __init__(self):
61-
super().__init__(allow_cli_args=False)
62-
63-
64-
Initialization Script
65-
---------------------
66-
67-
.. _AliasStartup: https://github.com/python-cmd2/cmd2/blob/master/examples/alias_startup.py
68-
69-
You can execute commands from an initialization script by passing a file
70-
path to the ``startup_script`` argument to the ``cmd2.Cmd.__init__()`` method
71-
like so::
72-
73-
class StartupApp(cmd2.Cmd):
74-
def __init__(self):
75-
cmd2.Cmd.__init__(self, startup_script='.cmd2rc')
76-
77-
This text file should contain a :ref:`Command Script
78-
<features/scripting:Command Scripts>`. See the AliasStartup_ example for a
79-
demonstration.
80-
81-
8233
select
8334
------
8435

0 commit comments

Comments
 (0)