diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-03-11 10:26:34 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-03-11 10:26:34 -0500 |
commit | 31036ef97bba35763b2025c4ea6befd872b69276 (patch) | |
tree | 298377bc58042f11428b01883954391c4596a141 /examples/script_conditional.py | |
parent | abe33f2813ecf7f2caaf6cf192394d7e56473563 (diff) | |
download | cmd2-git-31036ef97bba35763b2025c4ea6befd872b69276.tar.gz |
Added an example for how conditional control flow of a cmd2 application can be achieved via the py command and python scripts.
Diffstat (limited to 'examples/script_conditional.py')
-rw-r--r-- | examples/script_conditional.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/script_conditional.py b/examples/script_conditional.py new file mode 100644 index 00000000..cbfb0494 --- /dev/null +++ b/examples/script_conditional.py @@ -0,0 +1,24 @@ +# coding=utf-8 +""" +This is a Python script intended to be used with the "python_scripting.py" cmd2 example applicaiton. + +To run it you should do the following: + ./python_scripting.py + py run('script_conditional.py') + +Note: The "cmd" function is defined within the cmd2 embedded Python environment and in there "self" is your cmd2 +application instance. +""" + +# Try to change to a non-existent directory +cmd('cd foobar') + +# Conditionally do something based on the results of the last command +if self._last_result: + print('Contents of foobar directory:') + cmd('dir') +else: + # Change to parent directory + cmd('cd ..') + print('Contents of parent directory:') + cmd('dir') |