# Golf on CLI code

**URL:** <https://rubytalk.org/t/golf-on-cli-code/59083>\
**Category:** ruby-talk\
**Created:** [5 July 2010 23:29 UTC](https://rubytalk.org/t/golf-on-cli-code/59083 "2010-07-05T23:29:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [5 July 2010 23:29 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/1 "2010-07-05T23:29:20Z")

</div>

I have a registry (array) of CLI classes. Each class has a class  
method cli() that returns an array of strings or regular expressions  
to match against ARGV to see if it is the chosen cli. Eg.

&nbsp;&nbsp;class FooList \<\< CLIBase  
&nbsp;&nbsp;&nbsp;&nbsp;def self.cli ; ['foo', 'list'] ; end  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

So if someone puts on the command line:

&nbsp;&nbsp;$ foo list

Then the above class would match for it. I wrote the algorithm to  
handle this, but I think it's Ugh-ly. Wondering if the smart folk here  
might play a game of golf on it.

&nbsp;&nbsp;&nbsp;def command(argv)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
\<=\> a[1].size }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = lookup.find do |cc, cli|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cli.each\_with\_index do |r, i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = t && (r === argv[i])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmdc = x[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argv = argv[x[1].size..-1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return cmdc, argv  
&nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://rubytalk.org/u/Joel_VanderWerf)\
**Post date:** [6 July 2010 04:46 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/2 "2010-07-06T04:46:43Z")

</div>

Intransition wrote:

> &nbsp;&nbsp;&nbsp;def command(argv)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
> \<=\> a[1].size }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = lookup.find do |cc, cli|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cli.each\_with\_index do |r, i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = t && (r === argv[i])  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmdc = x[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argv = argv[x[1].size..-1]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return cmdc, argv  
> &nbsp;&nbsp;&nbsp;&nbsp;end

Untested:

&nbsp;&nbsp;&nbsp;def command(argv)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookup = registry.sort\_by {|cc| cc.cli.size}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmdc = lookup.find do |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cc.cli.zip(argv).all? do |r,arg|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r === arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return cmdc, argv[cmdc.cli.size..-1]  
&nbsp;&nbsp;&nbsp;end

The following (assuming it works!) would be more compact still:

&nbsp;&nbsp;&nbsp;class Symbol  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias old\_to\_proc to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :=== # etc.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc {|x,y| x === y}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def command(argv)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookup = registry.sort\_by {|cc| cc.cli.size}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmdc = lookup.find do |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cc.cli.zip(argv).all?(&:===)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return cmdc, argv[cmdc.cli.size..-1]  
&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [6 July 2010 11:24 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/3 "2010-07-06T11:24:26Z")

</div>

> def command(argv)  
> lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
> \<=\> a[1].size }

at the least this should be registry.map {|cc| [cc, cc.cli]}.sort\_by  
{|a| -a[1].size}

also, you're never using cc, so why carry it around?

lookup = registry.map {|cc| cc.cli}.sort\_by {|cli| -cli.size}

and using the ever-handy symbol.to\_proc

lookup = registry.map(&:cli).sort\_by(&:size).reverse

> ```
> x = lookup\.find do |cc, cli|
> t = true
> cli\.each\_with\_index do |r, i|
> t = t &amp;&amp; \(r === argv\[i\]\)
> end
> t
> end
> 
> ```

# untested  
lookup.find do |cli|  
&nbsp;&nbsp;cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}  
end

> ```
> cmdc = x\[0\]
> argv = argv\[x\[1\]\.size\.\.\-1\]
> return cmdc, argv
> 
> ```
> 
> end

martin

> **···**
>
> On Tue, Jul 6, 2010 at 4:59 AM, Intransition \<transfire@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [13 July 2010 00:49 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/4 "2010-07-13T00:49:52Z")

</div>

Thanks Joel and Martin. Amazingly concise answers -- it's amazes me  
sometimes how small a piece of code can get in Ruby 🙂

I haven't gotten back to the particular code I need this for yet which  
is why I haven't responded until now, but should be back to it soon  
and I'll let you know how it worked out.

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [13 July 2010 03:25 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/5 "2010-07-13T03:25:04Z")

</div>

> \> def command(argv)  
> \> lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
> \> \<=\> a[1].size }
> 
> at the least this should be registry.map {|cc| [cc, cc.cli]}.sort\_by  
> {|a| -a[1].size}
> 
> also, you're never using cc, so why carry it around?
> 
> lookup = registry.map {|cc| cc.cli}.sort\_by {|cli| -cli.size}
> 
> and using the ever-handy symbol.to\_proc
> 
> lookup = registry.map(&:cli).sort\_by(&:size).reverse
> 
> \> x = lookup.find do |cc, cli|  
> \> t = true  
> \> cli.each\_with\_index do |r, i|  
> \> t = t && (r === argv[i])  
> \> end  
> \> t  
> \> end
> 
> # untested  
> lookup.find do |cli|  
> cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}  
> end

lookup.find{|cli| cli == argv[0,cli.size] }

> **···**
>
> On Jul 6, 6:24 am, Martin DeMello \<martindeme...@gmail.com\> wrote:
> 
> > On Tue, Jul 6, 2010 at 4:59 AM, Intransition \<transf...@gmail.com\> wrote:
> 
> > \> cmdc = x[0]  
> > \> argv = argv[x[1].size..-1]  
> > \> return cmdc, argv  
> > \> end
> > 
> > martin

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [13 July 2010 15:35 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/6 "2010-07-13T15:35:04Z")

</div>

That won't work if cli contains a regexp.

lookup.find{|cli| cli.zip(argv).all?{|a,b| a == b }

> **···**
>
> On Jul 12, 10:20 pm, w\_a\_x\_man \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > On Jul 6, 6:24 am, Martin DeMello \<martindeme...@gmail.com\> wrote:
> > 
> > \> On Tue, Jul 6, 2010 at 4:59 AM, Intransition \<transf...@gmail.com\> wrote:
> > 
> > \> \> def command(argv)  
> > \> \> lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
> > \> \> \<=\> a[1].size }
> > 
> > \> at the least this should be registry.map {|cc| [cc, cc.cli]}.sort\_by  
> > \> {|a| -a[1].size}
> > 
> > \> also, you're never using cc, so why carry it around?
> > 
> > \> lookup = registry.map {|cc| cc.cli}.sort\_by {|cli| -cli.size}
> > 
> > \> and using the ever-handy symbol.to\_proc
> > 
> > \> lookup = registry.map(&:cli).sort\_by(&:size).reverse
> > 
> > \> \> x = lookup.find do |cc, cli|  
> > \> \> t = true  
> > \> \> cli.each\_with\_index do |r, i|  
> > \> \> t = t && (r === argv[i])  
> > \> \> end  
> > \> \> t  
> > \> \> end
> > 
> > \> # untested  
> > \> lookup.find do |cli|  
> > \> cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}  
> > \> end
> > 
> > lookup.find{|cli| cli == argv[0,cli.size] }

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [13 July 2010 17:00 UTC](https://rubytalk.org/t/golf-on-cli-code/59083/7 "2010-07-13T17:00:02Z")

</div>

Should be

lookup.find{|cli| cli.zip(argv).all?{|a,b| a === b }}

> **···**
>
> On Jul 13, 10:32 am, w\_a\_x\_man \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > On Jul 12, 10:20 pm, w\_a\_x\_man \<w\_a\_x\_...@yahoo.com\> wrote:
> > 
> > \> On Jul 6, 6:24 am, Martin DeMello \<martindeme...@gmail.com\> wrote:
> > 
> > \> \> On Tue, Jul 6, 2010 at 4:59 AM, Intransition \<transf...@gmail.com\> wrote:
> > 
> > \> \> \> def command(argv)  
> > \> \> \> lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size  
> > \> \> \> \<=\> a[1].size }
> > 
> > \> \> at the least this should be registry.map {|cc| [cc, cc.cli]}.sort\_by  
> > \> \> {|a| -a[1].size}
> > 
> > \> \> also, you're never using cc, so why carry it around?
> > 
> > \> \> lookup = registry.map {|cc| cc.cli}.sort\_by {|cli| -cli.size}
> > 
> > \> \> and using the ever-handy symbol.to\_proc
> > 
> > \> \> lookup = registry.map(&:cli).sort\_by(&:size).reverse
> > 
> > \> \> \> x = lookup.find do |cc, cli|  
> > \> \> \> t = true  
> > \> \> \> cli.each\_with\_index do |r, i|  
> > \> \> \> t = t && (r === argv[i])  
> > \> \> \> end  
> > \> \> \> t  
> > \> \> \> end
> > 
> > \> \> # untested  
> > \> \> lookup.find do |cli|  
> > \> \> cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}  
> > \> \> end
> > 
> > \> lookup.find{|cli| cli == argv[0,cli.size] }
> > 
> > That won't work if cli contains a regexp.
> > 
> > lookup.find{|cli| cli.zip(argv).all?{|a,b| a == b }
