Friday, 9 August 2013

Take in unescaped input in Ruby command line app

Take in unescaped input in Ruby command line app

I'm writing a Ruby command line application in which the user has to enter
a "format string" (much like Date.strptime/strftime's strings).
I tried taking them in as arguments to the command, eg
> torque "%A\n%d\n%i, %u"
but it seems that bash actually removes all backslashes from input before
it can be processed (plus a lot of trouble with spaces). I also tried the
highline gem, which has some more advanced input options, but that
automatically escapes backslashes "\" -> "\\" and provides no alternate
options.
My current solution is to do a find-and-replace: "\\n" -> "\n". This would
take care of the problem, but it also seems hacky and awful.
I could have users write the string in a text file (complicated for the
user) or treat some other character, like "&&", as a newline (still
complicated for the user).
What is the best way for users to input escaped characters on the command
line?
(UPDATE: I checked the documentation for strptime/strftime, and the format
strings for those functions replace newline characters with "%n", tabs
with "%t", etc. So for now I'm doing that, but any other suggestions are
welcome)

No comments:

Post a Comment