Skip to content

Commit

Permalink
Print all possible values of Enum type params (closes #52).
Browse files Browse the repository at this point in the history
* Added `CLI::Printing::Params#param_description`.
  • Loading branch information
postmodern committed Aug 20, 2024
1 parent b7fcf53 commit fbea9f1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ Style/GlobalStdStream:

Layout/HeredocIndentation: { Exclude: ['lib/ronin/core/cli/banner.rb'] }
Layout/ClosingHeredocIndentation: { Exclude: ['lib/ronin/core/cli/banner.rb'] }

# `param.type.values.each` false positive
Style/HashEachMethods:
Exclude:
- 'lib/ronin/core/cli/printing/params.rb'
32 changes: 31 additions & 1 deletion lib/ronin/core/cli/printing/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def print_params(klass)
else 'No'
end
default = param.default_value
description = param.desc
description = param_description(param)

rows << [name, param_type, required, default, description]
end
Expand Down Expand Up @@ -91,6 +91,36 @@ def param_usage(param)
param.name.upcase
end
end

#
# Returns the description text for the given param.
#
# @param [Core::Params::Param] param
# The param.
#
# @return [String]
# The description text.
#
# @note
# Will list all possible values for {Core::Params::Types::Enum Enum}
# type params.
#
# @since 0.3.0
#
def param_description(param)
description = param.desc

case param.type
when Core::Params::Types::Enum
description = description.dup

param.type.values.each do |value|
description << "#{$/} * #{value}"
end
end

return description
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/cli/printing/params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TestClass
desc: 'Foo param'
param :bar, Integer, default: 42,
desc: 'Bar param'
param :baz, Enum[:one, :two], desc: 'Baz param'

end

Expand Down Expand Up @@ -43,6 +44,9 @@ class TestCommand < Ronin::Core::CLI::Command
" ├──────┼─────────┼──────────┼─────────┼─────────────┤",
" │ foo │ String │ Yes │ │ Foo param │",
" │ bar │ Integer │ No │ 42 │ Bar param │",
" │ baz │ Enum │ No │ │ Baz param │",
" │ │ │ │ │ * one │",
" │ │ │ │ │ * two │",
" └──────┴─────────┴──────────┴─────────┴─────────────┘",
"",
""
Expand Down Expand Up @@ -124,4 +128,33 @@ class TestCommand < Ronin::Core::CLI::Command
end
end
end

describe "#param_description" do
let(:name) { :foo }
let(:param) do
Ronin::Core::Params::Param.new(name,type, desc: 'Test param')
end

context "when given a param with a Enum type" do
let(:type) { Ronin::Core::Params::Types::Enum[:bar, :baz] }

it "must return the param desc text plus all Enum values in list form" do
expect(subject.param_description(param)).to eq(
[
param.desc,
" * bar",
" * baz"
].join($/)
)
end
end

context "when given any other type of param" do
let(:type) { Ronin::Core::Params::Types::String.new }

it "must return the param's desc text" do
expect(subject.param_description(param)).to eq(param.desc)
end
end
end
end

0 comments on commit fbea9f1

Please sign in to comment.