| 1 | # Integrating with the OS |
| 2 | |
| 3 | ## How to redirect output |
| 4 | |
| 5 | See [Output Redirection and Pipes](./redirection.md#output-redirection-and-pipes) |
| 6 | |
| 7 | ## Executing OS commands from within `cmd2` |
| 8 | |
| 9 | `cmd2` includes a `shell` command which executes its arguments in the operating system shell: |
| 10 | |
| 11 | (Cmd) shell ls -al |
| 12 | |
| 13 | If you use the default [Shortcuts](./shortcuts_aliases_macros.md#shortcuts) defined in `cmd2` you'll |
| 14 | get a `!` shortcut for `shell`, which allows you to type: |
| 15 | |
| 16 | (Cmd) !ls -al |
| 17 | |
| 18 | !!! note |
| 19 | |
| 20 | `cmd2` provides user-friendly tab completion throughout the process of running a shell command - |
| 21 | first for the shell command name itself, and then for file paths in the argument section. |
| 22 | |
| 23 | However, a `cmd2` application effectively **becomes** the shell, so if you have _extra_ shell |
| 24 | completion configured for your particular shell such as `bash`, `zsh`, `fish`, etc. then this |
| 25 | will not be available within `cmd2`. |
| 26 | |
| 27 | ## Editors |
| 28 | |
| 29 | `cmd2` includes the built-in `edit` command which runs a text editor and optionally opens a file |
| 30 | with it: |
| 31 | |
| 32 | (Cmd) edit foo.txt |
| 33 | |
| 34 | The editor used is determined by the `editor` settable parameter and can be either a text editor |
| 35 | such as **vim** or a graphical editor such as **VSCode**. To set it: |
| 36 | |
| 37 | set editor <program_name> |
| 38 | |
| 39 | If you have the `EDITOR` environment variable set, then this will be the default value for `editor`. |
| 40 | If not, then `cmd2` will attempt to search for any in a list of common editors for your operating |
| 41 | system. |
| 42 | |
| 43 | ## Terminal pagers |
| 44 | |
| 45 | Output of any command can be displayed one page at a time using the [cmd2.Cmd.ppaged][] method. |
| 46 | |
| 47 | Alternatively, a terminal pager can be invoked directly using the ability to run shell commands with |
| 48 | the `!` shortcut like so: |
| 49 | |
| 50 | (Cmd) !less foo.txt |
| 51 | |
| 52 | !!! warning |
| 53 | |
| 54 | Once you are in a terminal pager, that program temporarily has control of your terminal, |
| 55 | **NOT** `cmd2`. Typically you can use either the arrow keys or `<PageUp>`/`<PageDown>` keys to |
| 56 | scroll around or type `q` to quit the pager and return control to your `cmd2` application. |
| 57 | |
| 58 | ## Exit codes |
| 59 | |
| 60 | The `self.exit_code` attribute of your `cmd2` application controls what exit code is returned from |
| 61 | `cmdloop()` when it completes. It is your job to make sure that this exit code gets sent to the |
| 62 | shell when your application exits by calling `sys.exit(app.cmdloop())`. |
| 63 | |
| 64 | ## Invoking With Arguments |
| 65 | |
| 66 | Typically you would invoke a `cmd2` program by typing: |
| 67 | |
| 68 | $ python mycmd2program.py |
| 69 | |
| 70 | or: |
| 71 | |
| 72 | $ mycmd2program.py |
| 73 | |
| 74 | Either of these methods will launch your program and enter the `cmd2` command loop, which allows the |
| 75 | user to enter commands, which are then executed by your program. |
| 76 | |
| 77 | You may want to execute commands in your program without prompting the user for any input. There are |
| 78 | several ways you might accomplish this task. The easiest one is to pipe commands and their arguments |
| 79 | into your program via standard input. You don't need to do anything to your program in order to use |
| 80 | this technique. Here's a demonstration using the `examples/cmd_as_argument.py` included in the |
| 81 | source code of `cmd2`: |
| 82 | |
| 83 | $ echo "speak -p some words" | python examples/cmd_as_argument.py |
| 84 | omesay ordsway |
| 85 | |
| 86 | Using this same approach you could create a text file containing the commands you would like to run, |
| 87 | one command per line in the file. Say your file was called `somecmds.txt`. To run the commands in |
| 88 | the text file using your `cmd2` program (from a Windows command prompt): |
| 89 | |
| 90 | c:\cmd2> type somecmds.txt | python.exe examples/cmd_as_argument.py |
| 91 | omesay ordsway |
| 92 | |
| 93 | By default, `cmd2` programs also look for commands passed as arguments from the operating system |
| 94 | shell, and execute those commands before entering the command loop: |
| 95 | |
| 96 | $ python examples/cmd_as_argument.py help |
| 97 | |
| 98 | Documented Commands |
| 99 | ─────────────────── |
| 100 | alias help macro orate run_pyscript say shell speak |
| 101 | edit history mumble quit run_script set shortcuts |
| 102 | |
| 103 | (Cmd) |
| 104 | |
| 105 | You may need more control over command line arguments passed from the operating system shell. For |
| 106 | example, you might have a command inside your `cmd2` program which itself accepts arguments, and |
| 107 | maybe even option strings. Say you wanted to run the `speak` command from the operating system |
| 108 | shell, but have it say it in pig latin: |
| 109 | |
| 110 | $ python examples/cmd_as_argument.py speak -p hello there |
| 111 | python cmd_as_argument.py speak -p hello there |
| 112 | usage: speak [-h] [-p] [-s] [-r REPEAT] words [words ...] |
| 113 | speak: error: the following arguments are required: words |
| 114 | *** Unknown syntax: -p |
| 115 | *** Unknown syntax: hello |
| 116 | *** Unknown syntax: there |
| 117 | (Cmd) |
| 118 | |
| 119 | Uh-oh, that's not what we wanted. `cmd2` treated `-p`, `hello`, and `there` as commands, which don't |
| 120 | exist in that program, thus the syntax errors. |
| 121 | |
| 122 | There is an easy way around this, which is demonstrated in |
| 123 | [cmd_as_argument.py](https://github.com/python-cmd2/cmd2/blob/main/examples/cmd_as_argument.py) |
| 124 | example. By setting `allow_cli_args=False` you can do your own argument parsing of the command line: |
| 125 | |
| 126 | $ python examples/cmd_as_argument.py speak -p hello there |
| 127 | ellohay heretay |
| 128 | |
| 129 | Check the source code of this example, especially the `main()` function, to see the technique. |
| 130 | |
| 131 | Alternatively you can simply wrap the command plus arguments in quotes (either single or double |
| 132 | quotes): |
| 133 | |
| 134 | $ python examples/cmd_as_argument.py "speak -p hello there" |
| 135 | ellohay heretay |
| 136 | (Cmd) |
| 137 | |
| 138 | ### Automating cmd2 apps from other CLI/CLU tools |
| 139 | |
| 140 | While `cmd2` is designed to create **interactive** command-line applications which enter a |
| 141 | Read-Evaluate-Print-Loop (REPL), there are a great many times when it would be useful to use a |
| 142 | `cmd2` application as a run-and-done command-line utility for purposes of automation and scripting. |
| 143 | |
| 144 | This is easily achieved by combining the following capabilities of `cmd2`: |
| 145 | |
| 146 | 1. Ability to invoke a `cmd2` application with arguments |
| 147 | 2. Ability to set an exit code when leaving a `cmd2` application |
| 148 | 3. Ability to exit a `cmd2` application with the `quit` command |
| 149 | |
| 150 | Here is a simple example which doesn't require the quit command since the custom `exit` command |
| 151 | quits while returning an exit code: |
| 152 | |
| 153 | $ python examples/exit_code.py "exit 23" |
| 154 | 'examples/exit_code.py' exiting with code: 23 |
| 155 | $ echo $? |
| 156 | 23 |
| 157 | |
| 158 | Here is another example using `quit`: |
| 159 | |
| 160 | $ python examples/cmd_as_argument.py "speak -p hello there" quit |
| 161 | ellohay heretay |
| 162 | $ |