Skip to content

Commit

Permalink
Merge pull request #876 from andrewn617/improve-boolean-parse-documen…
Browse files Browse the repository at this point in the history
…tation

Document the '--skip-' option for boolean options.
  • Loading branch information
rafaelfranca authored Feb 26, 2024
2 parents e43a9cd + 686bcd5 commit 96a3974
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def usage(padding = 0)

sample = "[#{sample}]".dup unless required?

if boolean?
sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.match(/\Ano[\-_]/)
if boolean? && name != "force" && !name.match(/\A(no|skip)[\-_]/)
sample << ", [#{dasherize('no-' + human_name)}], [#{dasherize('skip-' + human_name)}]"
end

aliases_for_usage.ljust(padding) + sample
Expand Down
3 changes: 2 additions & 1 deletion lib/thor/parser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def parsing_options?
@parsing_options
end

# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
# Parse boolean values which can be given as --foo=true or --foo for true values, or
# --foo=false, --no-foo or --skip-foo for false values.
#
def parse_boolean(switch)
if current_is_value?
Expand Down
9 changes: 6 additions & 3 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ def option(name, options = {})
end

it "returns usage for boolean types" do
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo]")
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo], [--skip-foo]")
end

it "does not use padding when no aliases are given" do
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo]")
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo], [--skip-foo]")
end

it "documents a negative option when boolean" do
Expand All @@ -231,6 +231,9 @@ def option(name, options = {})

it "does not document a negative option for a negative boolean" do
expect(parse(:'no-foo', :boolean).usage).not_to include("[--no-no-foo]")
expect(parse(:'no-foo', :boolean).usage).not_to include("[--skip-no-foo]")
expect(parse(:'skip-foo', :boolean).usage).not_to include("[--no-skip-foo]")
expect(parse(:'skip-foo', :boolean).usage).not_to include("[--skip-skip-foo]")
end

it "does not document a negative option for an underscored negative boolean" do
Expand Down Expand Up @@ -261,7 +264,7 @@ def option(name, options = {})
end

it "does not negate the aliases" do
expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo]")
expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo], [--skip-foo]")
end

it "normalizes the aliases" do
Expand Down

0 comments on commit 96a3974

Please sign in to comment.