# Commandline help for frustrated newb?

**URL:** <https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616>\
**Category:** Ruby\
**Created:** [April 24, 2006, 2:47am UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616 "2006-04-24T02:47:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 24, 2006, 2:47am UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/1 "2006-04-24T02:47:42Z")

</div>

ye gods,

I’m trying to learn my 1st OO lang and chose ruby. I’ve already  
got a couple of simple batch processing programs written and  
functioning but CommandLine is kicking my butt.

As I read the docs ‘program -a -b’ can be called with ‘program -ab’,  
setting flags a & b in either case. I get an Unknown option error.

I cut this example directly from:  
/usr/lib/ruby/gems/1.8/gems/commandline-0.7.10/docs/posted-docs.index.html

#!/usr/bin/env ruby

require ‘rubygems’  
require ‘commandline’

class App \< CommandLine::Application  
def initialize  
options :help, :debug  
end

def main  
puts “call your library here”  
end  
end#class App

Then added :verbose to options (options :help, :debug, :version).

My results:

% ruby example.rb -d -v  
call your library here

% ruby example.rb -dv  
Unknown option ‘-dv’.

Usage: example.rb

% \_

I’ve read everything I can find and tried a dozen different  
permutations on the theme, including :posix instead of :flag.  
I’m sure I’m overlooking something basic due to newbie-myopia.  
Any advice?

pb

---

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 24, 2006, 5:23am UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/2 "2006-04-24T05:23:21Z")

</div>

On Apr 23, 2006, at 7:47 PM, pb wrote:

> I’m trying to learn my 1st OO lang and chose ruby. I’ve already  
> got a couple of simple batch processing programs written and  
> functioning but CommandLine is kicking my butt.

So sorry about that.

> As I read the docs ‘program -a -b’ can be called with ‘program -ab’,  
> setting flags a & b in either case. I get an Unknown option error.

The problem is you are trying to mix Posix mode and Gnu-style mode.  
Right now, CommandLine does not try to mix the two due to the complexity  
of resolving the options. For example, is the option ‘-help’ belong  
to “-help”  
or to ‘-h’, ‘-e’, ‘-l’ and ‘-p’?

To get the posix features you want you need to go a little more manual:

> #!/usr/bin/env ruby
> 
> require ‘rubygems’  
> require ‘commandline’
> 
> class App \< CommandLine::Application  
> def initialize  
> use\_posix  
> options :help, :debug  
> option :posix, :flag, :names =\> “-d”  
> option :posix, :flag, :names =\> “-v”,  
> :opt\_found =\> lambda { puts “Version 1.0” }  
> % ruby example.rb -dv  
> This should now work.

Jim

> pb
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Jim F.

---

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 24, 2006, 6:43am UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/3 "2006-04-24T06:43:50Z")

</div>

Jim F. wrote:

> To get the posix features you want you need to go a little more manual:
> 
> > #!/usr/bin/env ruby
> > 
> > require ‘rubygems’  
> > require ‘commandline’
> > 
> > class App \< CommandLine::Application  
> > def initialize  
> > use\_posix

A-HA!

> > ```
> > options :help, :debug
> > 
> > ```
> 
> ```
> option :posix, :flag, :names => "-d"
> option :posix, :flag, :names => "-v",
> :opt_found => lambda { puts "Version 1.0" }
> 
> ```
> 
> > % ruby example.rb -dv  
> > This should now work.
> 
> Jim

I shoulda grep’d a little harder! Thanks Jim, I appreciate the effort  
you’ve put into these libraries. Very useful.

pb

---

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 24, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/4 "2006-04-24T19:29:37Z")

</div>

pb wrote:

> Jim F. wrote:
> 
> > > #!/usr/bin/env ruby
> > > 
> > > require ‘rubygems’  
> > > require ‘commandline’
> > > 
> > > class App \< CommandLine::Application  
> > > def initialize  
> > > use\_posix
> 
> A-HA!
> 
> > > ```
> > > options :help, :debug
> > > 
> > > ```
> > 
> > ```
> > option :posix, :flag, :names => "-d"
> > option :posix, :flag, :names => "-v",
> > :opt_found => lambda { puts "Version 1.0" }
> > 
> > ```
> > 
> > > % ruby example.rb -dv  
> > > This should now work.
> > 
> > Jim
> 
> I shoulda grep’d a little harder! Thanks Jim, I appreciate the effort  
> you’ve put into these libraries. Very useful.
> 
> pb

Oops, spoke too soon. When I actually try this suggestion I get a  
‘unknown variable or method’ error at the “use\_posix” line. The code  
(minus use\_posix) returns the original error: ‘option -dv unknown’.

I understand the difference between POSIX & GNU option styles but  
CommandLine’s documentation states

"The Gnu and the Unix option types can be mixed on the same commandline.  
The following are equivalent:

app -a -b --with-c  
app -ab --with-c  
app -ba --with-c  
app --with-c -ab"

I’ve yet to find a way to accomplish this claim within the example  
above.  
Did I misunderstand the reply? I know I’m doing something wrong since  
CommandLine is established code. Where did I wander off the path?

pb

---

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 27, 2006, 12:00am UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/5 "2006-04-27T00:00:49Z")

</div>

Jim F. wrote:

> …  
> If the Posix functional feature of combining options is turned off, then  
> all three may be combined in the same set of options. CommandLine  
> prefers this since it doesn’t want to limit how you define options.
> 
> But, if we turn the Posix combining feature on, we have the following  
> possible combinations:  
> gnu, xtools - ok  
> gnu, posix - ok  
> posix, xtools - illegal  
> gnu, posix, xtools - illegal
> 
> To implement this change, application.rb and optionparser.rb will need  
> to be  
> modified (and possibly option.rb).
> 
> So, please help me out here on the interface.  
> Most people will use gnu style options with the posix style short  
> options.  
> If the options list only contains gnu and posix style options, should  
> posix  
> be on by default? How should posix be turned on and off?

I can only speak to my experience but POSIX and gnu seem to be far and  
away  
the most common styles. I find I seldom start X apps from the command  
line,  
usually they’re launched from a GUI menu.

It may break all kinds of things but my preference would be:

1. full support of POSIX flags, singularly and combined
2. gnu’s --longwords. Unambiguious abbreviations supported if not in  
conflict with #1
3. xtool support would require an enable. Once enabled, we have the  
current situation with POSIX combinatons “broken”

Admittedly, not a elegant solution. I feel POSIX should be ‘ON’ by  
default and xtools must be enabled if -longword options are required.

Meanwhile, I’ll continue to use the current library. Its simple and very  
flexible. Thanks again for the clarification.

pb

---

<div class="post-metadata">

**Author:** ![pbpbpb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pbpbpb](https://www.ruby-forum.com/u/pbpbpb)\
**Post date:** [April 24, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/commandline-help-for-frustrated-newb/58616/6 "2006-04-24T21:06:06Z")

</div>

On 4/24/06, pb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> "The Gnu and the Unix option types can be mixed on the same commandline.  
> CommandLine is established code. Where did I wander off the path?  
> Nope. You understood correctly. I am afraid that you have found the soft  
> underbelly of CommandLine. That behavior used to be supported, but was  
> removed.  
> So, in short, the documentation has lied to you.

The problem comes about from trying to support too many different option  
types,  
so at the time, they were removed so we could make a timely release. So  
far,  
you are  
the first that wanted to mix the two. There shouldn’t be a problem  
adding  
this feature, but we would need to add a check to test for legal  
combinations.

If you want to provide a patch I am more that willing to add it in.  
Or, you can wait on me. 🙂 Not sure when I can get around to this,  
but I think it should be done.

Let me provide a little history of the problem.

The three option styles are Gnu, XTools and Posix.  
Other than syntax, there is also functional features of each.  
Gnu and XTools can always be abbreviated. That is, the option  
“–fred” can be represented as “–fre”, “–fr”, or “–f”, if any of  
those  
are unique. The same holds for XTools, “-fred” can be represented  
as “-fre”, “-fr” or “-f”. This feature is always on and cannot be turned  
off.

The functional feature of Posix is that the options “-a” and “-b” can be  
written as “-ab” or as “-ba”. Supporting this with XTools could get  
confusing,  
so we have opted not to support this.

If the Posix functional feature of combining options is turned off, then  
all three may be combined in the same set of options. CommandLine  
prefers this since it doesn’t want to limit how you define options.

But, if we turn the Posix combining feature on, we have the following  
possible combinations:  
gnu, xtools - ok  
gnu, posix - ok  
posix, xtools - illegal  
gnu, posix, xtools - illegal

To implement this change, application.rb and optionparser.rb will need  
to be  
modified (and possibly option.rb).

So, please help me out here on the interface.  
Most people will use gnu style options with the posix style short  
options.  
If the options list only contains gnu and posix style options, should  
posix  
be on by default? How should posix be turned on and off?
