# Parsing a command

**URL:** <https://www.ruby-forum.com/t/parsing-a-command/73458>\
**Category:** Ruby\
**Created:** [October 24, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/parsing-a-command/73458 "2006-10-24T19:56:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@Paul\_I](https://www.ruby-forum.com/u/Paul_I)\
**Post date:** [October 24, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/parsing-a-command/73458/1 "2006-10-24T19:56:59Z")

</div>

Hi,

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:

Current:

> add  
> Type: item\_type\_goes\_here  
> Title: item\_title\_goes\_here  
> Description: item\_desc\_goes\_here  
> .  
> .  
> .

Wanted:

> 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  
> hilarious”

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…
