views:

129

answers:

1

I've written a simple shell-like program that uses readline in order to provide smart completion of arguments. I would like the mechanism to support arguments that have spaces and are quoted to signify as one argument (as with providing the shell with such).

I've seen that shlex.split() knows how to parse quoted arguments, but in case a user wants to complete mid-typing it fails (for example: 'complete "Hello ' would cause an exception to be thrown when passed to shlex, because of unbalanced quotes).

Is there code for doing this?

Thanks!

+2  A: 

I don't know of any existing code for the task, but if I were to do this I'd catch the exception, try adding a fake trailing quote, and see how shlex.split does with the string thus modified.

Alex Martelli
I've considered that, but wanted to see whether there's something less ugly. Because then you'd have to try for both add " or ', etc, and that just isn't... pretty :)
abyx
It isn't pretty because the use case seems rather complex.
S.Lott
Implemented like so today, for the lack of other options.
abyx
@abyx, yeah, a small inelegance is better than leaving a problem unsolved, I agree!
Alex Martelli