rexpect icon indicating copy to clipboard operation
rexpect copied to clipboard

get the index of element that was matched by exp_any

Open pbartyik opened this issue 4 years ago • 4 comments

find fn returns a tuple with the unread buffer and the matched string by needle. The question is if it could also return the position of the expression used in ReadUntil::Any(vec)? Right now I'm looping through the passed expressions and checking which one matches the returned matched string.

Think "did I match username or password prompt, what to send back to the process".

pbartyik avatar Mar 22 '20 12:03 pbartyik

I think this is a good idea. But it would need to split up find for the ReadUntil variants without ReadUntil::Any (as this extra return doesn't make sense in this context) and a find for the ReadUntil::Any explicitly.

Do you have anything in mind? I didn't use rust lately so I don't know if there's any construct which could help here or if the best way forward would be just to create a separate function which would just be for ::Any and would support the new parameter

philippkeller avatar Mar 22 '20 15:03 philippkeller

honestly I'm still in the beginning of the learning curve of rust, so I may not be the best input for this. Not sure if a hacky solution would be to return an Option as the third element in the tuple, but that would save the need for a separate function. The separate fn seems more reasonable as that won't mess up the return values and keeps the feature explicit.

@TyPR124 what do you think? You are working on windows support, and this might affect you too.

pbartyik avatar Mar 22 '20 16:03 pbartyik

To be honest I haven't been looking closely at this part of the code, as I've been focusing mostly on the platform-specific parts and this code is mostly platform-agnostic. That said, I will give it some thought, as it sounds like a good idea.

To make sure I'm on the same page here, you're wanting to do something like session.exp_any(my_vec) and have it return an index into my_vec, correct?

TyPR124 avatar Mar 22 '20 18:03 TyPR124

Yep, along with the unread buffer till the match. The matched string have no benefit to me personally, but since it is currently returned I imagine it would be needed too.

pbartyik avatar Mar 22 '20 18:03 pbartyik