Skip to content

Commit 60a212c

Browse files
authored
Merge pull request #869 from python-cmd2/docs_os
Updated docs on integrating with various operating system features
2 parents da48280 + 322d46a commit 60a212c

File tree

2 files changed

+98
-31
lines changed

2 files changed

+98
-31
lines changed

docs/features/misc.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ Exiting
1515
Mention quit, and EOF handling built into ``cmd2``.
1616

1717

18-
Shell Command
19-
-------------
20-
21-
``cmd2`` includes a ``shell`` command which executes it's arguments in the
22-
operating system shell::
23-
24-
(Cmd) shell ls -al
25-
26-
If you use the default :ref:`features/shortcuts_aliases_macros:Shortcuts`
27-
defined in ``cmd2`` you'll get a ``!`` shortcut for ``shell``, which allows you
28-
to type::
29-
30-
(Cmd) !ls -al
31-
32-
3318
select
3419
------
3520

@@ -91,15 +76,6 @@ HelpCategories_ example for a demonstration.
9176
.. _HelpCategories: https://github.com/python-cmd2/cmd2/blob/master/examples/help_categories.py
9277

9378

94-
Exit code
95-
---------
96-
97-
The ``self.exit_code`` attribute of your ``cmd2`` application controls what
98-
exit code is returned from ``cmdloop()`` when it completes. It is your job to
99-
make sure that this exit code gets sent to the shell when your application
100-
exits by calling ``sys.exit(app.cmdloop())``.
101-
102-
10379
Default to shell
10480
----------------
10581

docs/features/os.rst

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,70 @@
11
Integrating with the OS
22
=======================
33

4-
- how to redirect output
5-
- executing OS commands from within ``cmd2``
6-
- editors
7-
- paging
8-
- exit codes
9-
- Automation and calling cmd2 from other CLI/CLU tools via commands at
10-
invocation and quit
4+
How to redirect output
5+
----------------------
116

7+
See :ref:`features/redirection:Output Redirection and Pipes`
8+
9+
Executing OS commands from within ``cmd2``
10+
------------------------------------------
11+
12+
``cmd2`` includes a ``shell`` command which executes it's arguments in the
13+
operating system shell::
14+
15+
(Cmd) shell ls -al
16+
17+
If you use the default :ref:`features/shortcuts_aliases_macros:Shortcuts`
18+
defined in ``cmd2`` you'll get a ``!`` shortcut for ``shell``, which allows you
19+
to type::
20+
21+
(Cmd) !ls -al
22+
23+
NOTE: ``cmd2`` provides user-friendly tab-completion throughout the process of
24+
running a shell command - first for the shell command name itself, and then for
25+
file paths in the argument section.
26+
27+
Editors
28+
-------
29+
30+
``cmd2`` includes the built-in ``edit`` command which runs a text editor and
31+
optionally opens a file with it::
32+
33+
(Cmd) edit foo.txt
34+
35+
The editor used is determined by the ``editor`` settable parameter and can
36+
be either a text editor such as **vim** or a graphical editor such as
37+
**VSCode**. To set it::
38+
39+
set editor <program_name>
40+
41+
If you have the ``EDITOR`` environment variable set, then this will be the
42+
default value for ``editor``. If not, then ``cmd2`` will attempt to search
43+
for any in a list of common editors for your operating system.
44+
45+
Terminal pagers
46+
---------------
47+
48+
Output of any command can be displayed one page at a time using the
49+
:meth:`~.cmd2.Cmd.ppaged` method.
50+
51+
Alternatively, a terminal pager can be invoked directly using the ability
52+
to run shell commands with the ``!`` shortcut like so::
53+
54+
(Cmd) !less foo.txt
55+
56+
NOTE: Once you are in a terminal pager, that program temporarily has control
57+
of your terminal, **NOT** ``cmd2``. Typically you can use either the arrow
58+
keys or <PageUp>/<PageDown> keys to scroll around or type ``q`` to quit the
59+
pager and return control to your ``cmd2`` application.
60+
61+
Exit codes
62+
----------
63+
64+
The ``self.exit_code`` attribute of your ``cmd2`` application controls what
65+
exit code is returned from ``cmdloop()`` when it completes. It is your job to
66+
make sure that this exit code gets sent to the shell when your application
67+
exits by calling ``sys.exit(app.cmdloop())``.
1268

1369
Invoking With Arguments
1470
-----------------------
@@ -84,3 +140,38 @@ your own argument parsing of the command line::
84140

85141
Check the source code of this example, especially the ``main()`` function, to
86142
see the technique.
143+
144+
Alternatively you can simply wrap the command plus arguments in quotes (either
145+
single or double quotes)::
146+
147+
$ python example/example.py "speak -p hello there"
148+
ellohay heretay
149+
(Cmd)
150+
151+
Automating cmd2 apps from other CLI/CLU tools
152+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153+
154+
While ``cmd2`` is designed to create **interactive** command-line applications
155+
which enter a Read-Evaluate-Print-Loop (REPL), there are a great many times
156+
when it would be useful to use a ``cmd2`` application as a run-and-done
157+
command-line utility for purposes of automation and scripting.
158+
159+
This is easily achieved by combining the following capabilities of ``cmd2``:
160+
161+
#. Ability to invoke a ``cmd2`` application with arguments
162+
#. Ability to set an exit code when leaving a ``cmd2`` application
163+
#. Ability to exit a ``cmd2`` application with the ``quit`` command
164+
165+
Here is a simple example which doesn't require the quit command since the
166+
custom ``exit`` command quits while returning an exit code::
167+
168+
$ python examples/exit_code.py "exit 23"
169+
'examples/exit_code.py' exiting with code: 23
170+
$ echo $?
171+
23
172+
173+
Here is another example using ``quit``::
174+
175+
$ python example/example.py "speak -p hello there" quit
176+
ellohay heretay
177+
$

0 commit comments

Comments
 (0)