Parsing a command


My app has (amongst other UIs) a CLI that, as expected, has a small set
of commands. The CLI is written in Ruby, as the rest of the app.

I want to improve my CLI add command by passing all the parameters to it
in one line. Let me give an example of current and wanted behavior:


Type: item_type_goes_here
Title: item_title_goes_here
Description: item_desc_goes_here


add type title [desc] [attr 1] [attr 2] … [attr n]

Now, the strings for each paramater recv by add should be entered
between double-quotes, like:

add book “My dying bride” “A fictionary book” “Ed’s Publishing&CO”

My question is who is it best to parse this?

I could use regexp all the way, maybe String#scan too. But I think there
should be a better/cleaner way of doing this.

What I need/expect is a behavior similar to any UNIX shell.

How does Ruby handle tokens? Is there any library for that? I couldn’t
find anything in core (and that seemed a bit strange to me).

Going a little crazier I might even consider nested double-quotes for
something like:

add book “The “real” deal” “It’s complicated” “Critics say it’s

Well I can live without the last example, but the first one is a must.

Any tips&tricks are welcomed, otherwise regexp and a dozen test cases
will bring this to a happy end…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs