From fbea9f169d6ea5826f0f8435854dbc3a5819dc9e Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 19 Aug 2024 20:15:08 -0700 Subject: [PATCH] Print all possible values of `Enum` type params (closes #52). * Added `CLI::Printing::Params#param_description`. --- .rubocop.yml | 5 ++++ lib/ronin/core/cli/printing/params.rb | 32 +++++++++++++++++++++++++- spec/cli/printing/params_spec.rb | 33 +++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index f439703..285d95a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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' diff --git a/lib/ronin/core/cli/printing/params.rb b/lib/ronin/core/cli/printing/params.rb index 79b8eb9..87f1609 100644 --- a/lib/ronin/core/cli/printing/params.rb +++ b/lib/ronin/core/cli/printing/params.rb @@ -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 @@ -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 diff --git a/spec/cli/printing/params_spec.rb b/spec/cli/printing/params_spec.rb index c44fd0e..86ad76e 100644 --- a/spec/cli/printing/params_spec.rb +++ b/spec/cli/printing/params_spec.rb @@ -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 @@ -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 │", " └──────┴─────────┴──────────┴─────────┴─────────────┘", "", "" @@ -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