5

Is it possible to "wait" for different answers from an expect command at the same time?

E.g: child.expect('first', 'second')

And if YES, how can differentiate which one has triggered it?

Kaguei Nakueka
  • 993
  • 2
  • 13
  • 34

1 Answers1

17

Yes, you can do it like:

i = child.expect(['first', 'second'])

The expect() method returns the index of the pattern that was matched. So in your example:

if i == 0:
    # do something with 'first' match
else: # i == 1
    # do something with 'second' match

For more: http://pexpect.readthedocs.org/en/stable/overview.html

Quinn
  • 4,394
  • 2
  • 21
  • 19
  • Is there anyway to do this where you have multiple options? As in what if you want to execute both the first and second option? – M. Barbieri Apr 03 '19 at 19:57
  • 1
    @M.Barbieri: Yes, this becomes a programming question. When there are multiple options, you can do like, `i = child.expect (['option1', 'option2', 'option3']); if i in [0, 1]: do something for first 2 options...` – Quinn Apr 11 '19 at 13:50