summaryrefslogtreecommitdiff
path: root/examples/script_conditional.py
blob: f0ded920318f1934d0dbe6fe8d3c6bf5b3556e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# coding=utf-8
"""
This is a Python script intended to be used with the "python_scripting.py" cmd2 example application.

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')

    # Change back to where we were
    cmd('cd ..')
else:
    # Change to parent directory
    cmd('cd ..')
    print('Contents of parent directory:')
    cmd('dir')

    # Change back to where we were
    cmd('cd examples')