|
1 | 1 | Integrating with the OS |
2 | 2 | ======================= |
3 | 3 |
|
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 | +---------------------- |
11 | 6 |
|
| 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())``. |
12 | 68 |
|
13 | 69 | Invoking With Arguments |
14 | 70 | ----------------------- |
@@ -84,3 +140,38 @@ your own argument parsing of the command line:: |
84 | 140 |
|
85 | 141 | Check the source code of this example, especially the ``main()`` function, to |
86 | 142 | 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