Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/boolean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options do
opt :default_false, "Boolean flag with false default", :default => false, :short => "f"
opt :default_true, "Boolean flag with true default", :default => true, :short => "t"
end

puts opts
2 changes: 1 addition & 1 deletion lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def educate
optionlist = []
optionlist.concat(short.chars.map { |o| "-#{o}" })
optionlist.concat(long.names.map { |o| "--#{o}" })
optionlist.compact.join(', ') + type_format + (flag? && default ? ", --no-#{long}" : "")
optionlist.compact.join(', ') + type_format + (flag? && default ? ", --no-#{long.long}" : "")
end

## Format the educate-line description including the default and permitted value(s)
Expand Down
12 changes: 12 additions & 0 deletions test/optimist/parser_educate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ def test_help_includes_option_types
assert help[10] =~ /<date\+>/
end

def test_help_handles_boolean_flags
parser.opt :default_false, 'default-false', :default => false
parser.opt :default_true, 'default-true', :default => true
sio = StringIO.new "w"
parser.educate sio

help = sio.string.split "\n"
assert help[1] =~ /--default-false/
assert help[2] =~ /--default-true, --no-default-true/
assert help[2] =~ /\(default: true\)/
end

def test_help_has_grammatical_default_text
parser.opt :arg1, 'description with period.', :default => 'hello'
parser.opt :arg2, 'description without period', :default => 'world'
Expand Down
Loading