cmd2
cmd2 copied to clipboard
replay select choices in transcript
Hello,
Is there a way to replay select choices in transcript so that it doesn't wait for user input while running transcript?
Using the following example code
class SelectApp(cmd2.Cmd):
def do_eat(self, arg):
"""Eat something, with a selection of sauces to choose from."""
# Pass in a single string of space-separated selections
sauce = self.select('sweet salty', 'Sauce? ')
result = '{food} with {sauce} sauce, yum!'
result = result.format(food=arg, sauce=sauce)
self.stdout.write(result + '\n')
def do_study(self, arg):
"""Learn something, with a selection of subjects to choose from."""
# Pass in a list of strings for selections
subject = self.select(['math', 'science'], 'Subject? ')
result = 'Good luck learning {}!\n'.format(subject)
self.stdout.write(result)
def do_procrastinate(self, arg):
"""Waste time in your manner of choice."""
# Pass in a list of tuples for selections
leisure_activity = self.select(
[('Netflix and chill', 'Netflix'), ('YouTube', 'WebSurfing')
], 'How would you like to procrastinate? '
)
result = 'Have fun procrasinating with {}!\n'.format(leisure_activity)
self.stdout.write(result)
def do_play(self, arg):
"""Play your favorite musical instrument."""
# Pass in an uneven list of tuples for selections
instrument = self.select(
[('Guitar', 'Electric Guitar'), ('Drums',)], 'Instrument? ')
result = 'Charm us with the {}...\n'.format(instrument)
self.stdout.write(result)
def do_return_type(self, arg):
"""Test that return values can be non-strings"""
choice = self.select(
[(1, 'Integer'), ("test_str", 'String'), (self.do_play, 'Method')], 'Choice? ')
result = f'The return type is {type(choice)}\n'
self.stdout.write(result)
(Cmd) eat
1. sweet
2. salty
Sauce? 2
with salty sauce, yum!
❯ python sam/lib/test2.py --test transcript.txt
======================================================================================================= cmd2 transcript test ========================================================================================================
platform darwin -- Python 3.9.7, cmd2-2.4.2, readline-RlType.GNU
cmd2 app: sam/lib/test2.py
collected 1 transcript
Sauce? --->stuck here
This seems like something @kmvanbrunt would know best.
Place your select answers in a text file, each on its own line. For instance, answers.txt.
1
2
1
Then run your tests like this: cat answers.txt | python sam/lib/test2.py --test transcript.txt
Place your select answers in a text file, each on its own line. For instance, answers.txt.
1 2 1Then run your tests like this:
cat answers.txt | python sam/lib/test2.py --test transcript.txt
Got it. It needs external input piped. Thanks.