Skip to content

Commit

Permalink
Add setting to prevent creation of short accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
florianpilz committed Dec 29, 2013
1 parent 0477e44 commit 52ef8ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/micro-optparse/parser.rb
Expand Up @@ -43,12 +43,13 @@ def process!(arguments = ARGV)
@result = @default_values.clone # reset or new
@optionparser ||= OptionParser.new do |p| # prepare only once
@options.each do |o|
@used_short << short = o[:settings][:short] || short_from(o[:name])
@used_short << short = o[:settings][:no_short] ? nil : o[:settings][:short] || short_from(o[:name])
@result[o[:name]] = o[:settings][:default] || false unless o[:settings][:optional] # set default
name = o[:name].to_s.gsub("_", "-")
klass = o[:settings][:default].class == Fixnum ? Integer : o[:settings][:default].class

args = [] << "-" + short << o[:description]
args = [o[:description]]
args << "-" + short if short
if [TrueClass, FalseClass, NilClass].include?(klass) # boolean switch
args << "--[no-]" + name
else # argument with parameter, add class for typecheck
Expand Down
7 changes: 7 additions & 0 deletions spec/parser_spec.rb
Expand Up @@ -265,5 +265,12 @@
result.should include("-b, --bac")
result.should include("-c, --cba")
end

it "should be possible to prevent creation of short arguments" do
result = `ruby spec/programs/noshort.rb --help`
result.should_not include("-f, --foo")
result.should include("--foo")
result.should include("-b, --bar")
end
end
end
11 changes: 11 additions & 0 deletions spec/programs/noshort.rb
@@ -0,0 +1,11 @@
require "rubygems"
require "micro-optparse"

options = Parser.new do |p|
p.option :foo, "Option 1", :default => "String", :no_short => true
p.option :bar, "Option 2", :default => "String"
end.process!

options.each_pair do |key, value|
puts ":#{key} => #{value}"
end

0 comments on commit 52ef8ff

Please sign in to comment.