Search code examples
pythonpexpect

Is there anyway to pass multiple values to a single argument


I have a function defined like this:

def cmd_success(cmd_session, success_cmd, failure_cmd=None):
    ....
    ....

I am calling that function here:

for command in commands:
success = cmd_success(session, str_prompt, 'Invalid')
if success == False:
    print "Invalid Command"
    sys.exit()

Currently, it is only checking for 'invalid' string but I would like to check for 'Error', 'Incomplete'. For many reason, I can not change code in cmd_success function so I was looking for a way to do it during function call.

Something like this:

for command in commands:
    success = cmd_success(session, str_prompt, ['Invalid', 'Error', Incomplete')
    if success == False:
        print "Invalid Command"
        sys.exit()

That doesn't work and gives me this:

TypeError: got <type 'tuple'> (['Invalid', 'Error', 'Incomplete' ]) as pattern, must be one of: <type 'basestring'>, pexpect.EOF, pexpect.TIMEOUT

Is there anyway it can look for all three strings? The argument is 'failure_cmd' and it is expecting only 1 string but I would like to have all 3 strings passed so f any one of them is found, success is set to false.

Thanks Damon


Solution

  • According to its documentation, pexpect takes a regular expression as its pattern parameter. It is possible that a regular expression might work for the cmd_success() pattern, also.

    Try this:

    success = cmd_success(session, str_prompt, 'Invalid|Error|Incomplete')