Forum: Ruby commandline help for frustrated newb?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
pb (Guest)
on 2006-04-24 04:47
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
Jim F. (Guest)
on 2006-04-24 07:23
(Received via mailing list)
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/.
>

Jim F.
pb (Guest)
on 2006-04-24 08:43
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
pb (Guest)
on 2006-04-24 21:29
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
Jim F. (Guest)
on 2006-04-24 23:06
(Received via mailing list)
On 4/24/06, pb <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?
pb (Guest)
on 2006-04-27 02:00
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
This topic is locked and can not be replied to.