# Argument parsing with embedded parentheses

**URL:** <https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978>\
**Category:** Ruby\
**Created:** [April 17, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978 "2006-04-17T13:53:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [April 17, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/1 "2006-04-17T13:53:36Z")

</div>

Hello,

Can someone please point me at a simple recipe for  
parsing a parameter list with embedded parentheses  
and commas, e.g.,

class String  
def get\_args  
split(’,’).map{|e| e.strip} # naive recipe  
end  
end

require ‘test/unit’

class TestArgumentParser \< Test::Unit::TestCase  
def test\_simple\_cases  
assert\_equal %w[a b], ‘a,b’.get\_args  
assert\_equal %w[a\_frog b25], ‘a\_frog, b25’.get\_args  
end  
def test\_embedded\_parens  
assert\_equal %w[a(1) b], ‘a(1), b’.get\_args  
assert\_equal %w[a b(i)], ‘a, b(i)’.get\_args  
end  
def test\_embedded\_parens\_with\_commas  
assert\_equal %w[a(0,j) b], ‘a(0,j),b’.get\_args  
assert\_equal %w[a() b(j,k)], ‘a(),b(j,k)’.get\_args  
assert\_equal %w[c(i,j,t(1))], ‘c(i,j,t(1))’.get\_args  
end  
end

Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [April 17, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/2 "2006-04-17T15:23:11Z")

</div>

On Apr 17, 2006, at 6:50 AM, Bil K. wrote:

> Hello,
> 
> Can someone please point me at a simple recipe for  
> parsing a parameter list with embedded parentheses  
> and commas, e.g.,

I think StringScanner makes this kind of work fairly simple:

require “strscan”

class String  
def get\_args  
scanner = StringScanner.new(self)  
result = scanner.eos? ? Array.new : [""]  
paren\_depth = 0

```
 until scanner.eos?
   if scanner.scan(/[^(),]+/)
     # do nothing--we found the part of the argument we need to add
   elsif scanner.scan(/\(/)
     paren_depth += 1
   elsif scanner.scan(/\)/)
     paren_depth -= 1
   elsif scanner.scan(/,\s*/) and paren_depth.zero?
     result << ""
     next
   end

   result.last << scanner.matched
 end

 result

```

end  
end

require ‘test/unit’

class TestArgumentParser \< Test::Unit::TestCase  
def test\_simple\_cases  
assert\_equal %w[a b], ‘a,b’.get\_args  
assert\_equal %w[a\_frog b25], ‘a\_frog, b25’.get\_args  
end  
def test\_embedded\_parens  
assert\_equal %w[a(1) b], ‘a(1), b’.get\_args  
assert\_equal %w[a b(i)], ‘a, b(i)’.get\_args  
end  
def test\_embedded\_parens\_with\_commas  
assert\_equal %w[a(0,j) b], ‘a(0,j),b’.get\_args  
assert\_equal %w[a() b(j,k)], ‘a(),b(j,k)’.get\_args  
assert\_equal %w[c(i,j,t(1))], ‘c(i,j,t(1))’.get\_args  
end  
end

**END**

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [April 17, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/3 "2006-04-17T16:31:00Z")

</div>

James Edward G. II wrote:

> Hope that helps.

It does more than help; the tests pass!

3 tests, 7 assertions, 0 failures, 0 errors

## Thanks,

Bil

> **[FUN3D Manual](https://fun3d.larc.nasa.gov/)**
>
> FUN3D suite of CFD simulation and design tools

P.S. I really hate working in with Open Source – I get  
more help than I asked for within 2 hours! I bet my  
vendor wouldn’t have even been open yet. 😉

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 23, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/4 "2006-08-23T23:44:40Z")

</div>

Hi,

How about parsing a parameter list with embedded white space between two  
words and EOF?

## Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 24, 2006, 7:07am UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/5 "2006-08-24T07:07:12Z")

</div>

QApassion wrote:

> Hi,

Hello.

> How about parsing a parameter list with embedded white space between two  
> words and EOF?

Whoa… I posted that one back in April?

Thanks though,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 24, 2006, 12:06am UTC](https://www.ruby-forum.com/t/argument-parsing-with-embedded-parentheses/57978/6 "2006-08-24T00:06:42Z")

</div>

The below works, excpet for the case highlighted, but its a start!

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

class String  
def get\_args  
(split(/(._[^(]._),(._[^)]._)/) - [“”]).map{|e| e.strip}  
end  
end  
require ‘test/unit’

class TestArgumentParser \< Test::Unit::TestCase  
def test\_simple\_cases  
assert\_equal %w[a b], ‘a,b’.get\_args  
assert\_equal %w[a\_frog b25], ‘a\_frog, b25’.get\_args  
end  
def test\_embedded\_parens  
assert\_equal %w[a(1) b], ‘a(1), b’.get\_args  
assert\_equal %w[a b(i)], ‘a, b(i)’.get\_args  
end  
def test\_embedded\_parens\_with\_commas  
assert\_equal %w[a(0,j) b], ‘a(0,j),b’.get\_args  
assert\_equal %w[a() b(j,k)], ‘a(),b(j,k)’.get\_args ## doesnt work  
assert\_equal %w[c(i,j,t(1))], ‘c(i,j,t(1))’.get\_args  
end  
end
