Skip to content

Commit d39c643

Browse files
committed
Merge redundant examples
The following 4 examples all demonstrate various aspects of using `argparse` for command argument processing and have been merged into a single comprehensive example called `argparse_example.py`: - arg_decorators.py - decorator_example.py - arg_print.py - subcommands.py `first_app.py` and `initialization.py` had a lot of overlap in demonstrating basic features of cmd2. I combined them into a single `getting_started.py` example
1 parent 83a2154 commit d39c643

27 files changed

+393
-480
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ environment is set up and working properly.
329329
You can also run the example app and see a prompt that says "(Cmd)" running the command:
330330

331331
```sh
332-
$ uv run examples/example.py
332+
$ uv run examples/getting_started.py
333333
```
334334

335335
You can type `help` to get help or `quit` to quit. If you see that, then congratulations – you're

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Settable environment parameters
1111
Parsing commands with `argparse` argument parsers (flags)
1212
Redirection to file or paste buffer (clipboard) with > or >>
13-
Easy transcript-based testing of applications (see examples/example.py)
13+
Easy transcript-based testing of applications (see examples/transcript_example.py)
1414
Bash-style ``select`` available
1515
1616
Note, if self.stdout is different than sys.stdout, then redirection with > and |

cmd2/transcript.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Cmd2TestCase(unittest.TestCase):
3434
that will execute the commands in a transcript file and expect the
3535
results shown.
3636
37-
See example.py
37+
See transcript_example.py
3838
"""
3939

4040
cmdapp: Optional['Cmd'] = None

docs/examples/first_app.md renamed to docs/examples/getting_started.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# First Application
1+
# Getting Started
22

3-
Here's a quick walkthrough of a simple application which demonstrates 8 features of `cmd2`:
3+
Here's a quick walkthrough of a simple application which demonstrates 10 features of `cmd2`:
44

55
- [Settings](../features/settings.md)
66
- [Commands](../features/commands.md)
@@ -14,17 +14,17 @@ Here's a quick walkthrough of a simple application which demonstrates 8 features
1414
If you don't want to type as we go, here is the complete source (you can click to expand and then
1515
click the **Copy** button in the top-right):
1616

17-
??? example
17+
!!! example "getting_started.py"
1818

1919
```py
2020
{%
21-
include "../../examples/first_app.py"
21+
include "../../examples/getting_started.py"
2222
%}
2323
```
2424

2525
## Basic Application
2626

27-
First we need to create a new `cmd2` application. Create a new file `first_app.py` with the
27+
First we need to create a new `cmd2` application. Create a new file `getting_started.py` with the
2828
following contents:
2929

3030
```py
@@ -47,7 +47,7 @@ We have a new class `FirstApp` which is a subclass of [cmd2.Cmd][]. When we tell
4747
file like this:
4848

4949
```shell
50-
$ python first_app.py
50+
$ python getting_started.py
5151
```
5252

5353
it creates an instance of our class, and calls the `cmd2.Cmd.cmdloop` method. This method accepts
@@ -77,7 +77,7 @@ In that initializer, the first thing to do is to make sure we initialize `cmd2`.
7777
run the script, and enter the `set` command to see the settings, like this:
7878

7979
```shell
80-
$ python first_app.py
80+
$ python getting_started.py
8181
(Cmd) set
8282
```
8383

@@ -88,8 +88,8 @@ you will see our `maxrepeats` setting show up with it's default value of `3`.
8888
Now we will create our first command, called `speak` which will echo back whatever we tell it to
8989
say. We are going to use an [argument processor](../features/argument_processing.md) so the `speak`
9090
command can shout and talk piglatin. We will also use some built in methods for
91-
[generating output](../features/generating_output.md). Add this code to `first_app.py`, so that the
92-
`speak_parser` attribute and the `do_speak()` method are part of the `CmdLineApp()` class:
91+
[generating output](../features/generating_output.md). Add this code to `getting_started.py`, so
92+
that the `speak_parser` attribute and the `do_speak()` method are part of the `CmdLineApp()` class:
9393

9494
```py
9595
speak_parser = cmd2.Cmd2ArgumentParser()

docs/examples/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!--intro-start-->
44

5-
- [First Application](first_app.md)
5+
- [Getting Started](getting_started.md)
66
- [Alternate Event Loops](alternate_event_loops.md)
77
- [List of cmd2 examples](examples.md)
88

docs/features/argument_processing.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ following for you:
1414

1515
These features are all provided by the `@with_argparser` decorator which is importable from `cmd2`.
1616

17-
See the either the [argprint](https://github.com/python-cmd2/cmd2/blob/main/examples/arg_print.py)
18-
or [decorator](https://github.com/python-cmd2/cmd2/blob/main/examples/decorator_example.py) example
19-
to learn more about how to use the various `cmd2` argument processing decorators in your `cmd2`
20-
applications.
17+
See the
18+
[argparse_example](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_example.py)
19+
example to learn more about how to use the various `cmd2` argument processing decorators in your
20+
`cmd2` applications.
2121

2222
`cmd2` provides the following [decorators](../api/decorators.md) for assisting with parsing
2323
arguments passed to commands:
@@ -286,8 +286,9 @@ argparse sub-parsers.
286286
You may add multiple layers of subcommands for your command. `cmd2` will automatically traverse and
287287
tab complete subcommands for all commands using argparse.
288288

289-
See the [subcommands](https://github.com/python-cmd2/cmd2/blob/main/examples/subcommands.py) example
290-
to learn more about how to use subcommands in your `cmd2` application.
289+
See the
290+
[argparse_example](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_example.py)
291+
example to learn more about how to use subcommands in your `cmd2` application.
291292

292293
## Argparse Extensions
293294

docs/features/initialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
Here is a basic example `cmd2` application which demonstrates many capabilities which you may wish to utilize while initializing the app:
44

5-
!!! example "examples/initialization.py"
5+
!!! example "examples/getting_started.py"
66

77
```py
88
{%
9-
include "../../examples/initialization.py"
9+
include "../../examples/getting_started.py"
1010
%}
1111
```
1212

docs/features/os.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,23 @@ user to enter commands, which are then executed by your program.
6969
You may want to execute commands in your program without prompting the user for any input. There are
7070
several ways you might accomplish this task. The easiest one is to pipe commands and their arguments
7171
into your program via standard input. You don't need to do anything to your program in order to use
72-
this technique. Here's a demonstration using the `examples/example.py` included in the source code
73-
of `cmd2`:
72+
this technique. Here's a demonstration using the `examples/transcript_example.py` included in the
73+
source code of `cmd2`:
7474

75-
$ echo "speak -p some words" | python examples/example.py
75+
$ echo "speak -p some words" | python examples/transcript_example.py
7676
omesay ordsway
7777

7878
Using this same approach you could create a text file containing the commands you would like to run,
7979
one command per line in the file. Say your file was called `somecmds.txt`. To run the commands in
8080
the text file using your `cmd2` program (from a Windows command prompt):
8181

82-
c:\cmd2> type somecmds.txt | python.exe examples/example.py
82+
c:\cmd2> type somecmds.txt | python.exe examples/transcript_example.py
8383
omesay ordsway
8484

8585
By default, `cmd2` programs also look for commands pass as arguments from the operating system
8686
shell, and execute those commands before entering the command loop:
8787

88-
$ python examples/example.py help
88+
$ python examples/transcript_example.py help
8989

9090
Documented commands (use 'help -v' for verbose/'help <topic>' for details):
9191
===========================================================================
@@ -99,8 +99,8 @@ example, you might have a command inside your `cmd2` program which itself accept
9999
maybe even option strings. Say you wanted to run the `speak` command from the operating system
100100
shell, but have it say it in pig latin:
101101

102-
$ python example/example.py speak -p hello there
103-
python example.py speak -p hello there
102+
$ python example/transcript_example.py speak -p hello there
103+
python transcript_example.py speak -p hello there
104104
usage: speak [-h] [-p] [-s] [-r REPEAT] words [words ...]
105105
speak: error: the following arguments are required: words
106106
*** Unknown syntax: -p
@@ -122,7 +122,7 @@ Check the source code of this example, especially the `main()` function, to see
122122
Alternatively you can simply wrap the command plus arguments in quotes (either single or double
123123
quotes):
124124

125-
$ python example/example.py "speak -p hello there"
125+
$ python example/transcript_example.py "speak -p hello there"
126126
ellohay heretay
127127
(Cmd)
128128

@@ -148,6 +148,6 @@ quits while returning an exit code:
148148

149149
Here is another example using `quit`:
150150

151-
$ python example/example.py "speak -p hello there" quit
151+
$ python example/transcript_example.py "speak -p hello there" quit
152152
ellohay heretay
153153
$

docs/features/prompt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This prompt can be configured by setting the `cmd2.Cmd.prompt` instance attribute. This contains the
88
string which should be printed as a prompt for user input. See the
9-
[Initialization](https://github.com/python-cmd2/cmd2/blob/main/examples/initialization.py) example
9+
[getting_started](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py) example
1010
for the simple use case of statically setting the prompt.
1111

1212
## Continuation Prompt
@@ -15,7 +15,7 @@ When a user types a [Multiline Command](./multiline_commands.md) it may span mor
1515
input. The prompt for the first line of input is specified by the `cmd2.Cmd.prompt` instance
1616
attribute. The prompt for subsequent lines of input is defined by the `cmd2.Cmd.continuation_prompt`
1717
attribute.See the
18-
[Initialization](https://github.com/python-cmd2/cmd2/blob/main/examples/initialization.py) example
18+
[getting_started](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py) example
1919
for a demonstration of customizing the continuation prompt.
2020

2121
## Updating the prompt

docs/features/startup_commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ program. `cmd2` interprets each argument as a separate command, so you should en
1616
in quotation marks if it is more than a one-word command. You can use either single or double quotes
1717
for this purpose.
1818

19-
$ python examples/example.py "say hello" "say Gracie" quit
19+
$ python examples/transcript_example.py "say hello" "say Gracie" quit
2020
hello
2121
Gracie
2222

0 commit comments

Comments
 (0)