tj / commander

The complete solution for Ruby command-line executables
http://visionmedia.github.com/commander
MIT License
1.09k stars 64 forks source link

Avoiding Switch Clashes via OptionParser #55

Closed zachgersh closed 11 years ago

zachgersh commented 11 years ago

Hey All,

Hopefully you can throw a suggestion out as to how I would solve this problem.

Currently have a global option --plugins with no explicit -p switch (OptionParser implicitly creates the -p switch as suggested by the documentation).

Later on there is a command option off of something called :serve which takes a --port or -p (explicitly defined).

So, when writing something like this

server --plugins "/foo" -p 1111

plugins gets all screwy because it now thinks it is supposed to look in the 111 directory.

Is there a way to get around this?

olivierlacan commented 11 years ago

Thanks for opening this @zachgersh. @visionmedia you can find more context for this issue on a related Jekyll issue.

ggilder commented 11 years ago

Hmm, so reading through the Jekyll issue it sounds like the best workaround is to explicitly define the shorthand switches in a non-conflicting way, e.g. -p and -P, rather than letting OptionParser guess.

I'm open to suggestions about what commander could do to make this better...

zachgersh commented 11 years ago

Is there a way to make it so that the short flags do not get automatically set via OptionParser unless they are listed explicitly?

On Jun 7, 2013, at 1:14 AM, Gabriel Gilder notifications@github.com wrote:

Hmm, so reading through the Jekyll issue it sounds like the best workaround is to explicitly define the shorthand switches in a non-conflicting way, e.g. -p and -P, rather than letting OptionParser guess.

I'm open to suggestions about what commander could do to make this better...

— Reply to this email directly or view it on GitHub.

ggilder commented 11 years ago

I did some testing with OptionParser and I'm not seeing a way to disable the automatic creation of short flags.

A possible way to fix this issue would be to have commander parse command flags before parsing global flags. However, I'm not sure that that approach would be correct, and there's a strong possibility that it would break existing scripts.

zachgersh commented 11 years ago

I don't see anything in the Ruby documentation about OptionParser that shows it automatically generating a short flag anywhere. I definitely believe you tested it but what did you do to force it to create a short flag?

ggilder commented 11 years ago

Here's a sample script that shows some of OptionParser's behavior.

#!/usr/bin/env ruby

require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.on("--verbose") do |v|
    options[:verbose] = v
  end
  opts.on("--test") do |t|
    options[:test] = t
  end
  opts.on("-Q", "--trace") do |t|
    options[:trace] = t
  end
end.parse!

p options
p ARGV