diff --git a/lib/ronin/core/cli/printing/params.rb b/lib/ronin/core/cli/printing/params.rb index a845945..8126dad 100644 --- a/lib/ronin/core/cli/printing/params.rb +++ b/lib/ronin/core/cli/printing/params.rb @@ -62,6 +62,33 @@ def print_params(klass) print_table(rows,header: PARAM_TABLE_HEADER, border: :line) end end + + # + # Returns a placeholder usage value for the given param. + # + # @param [Core::Params::Param] param + # The param. + # + # @return [String] + # The placeholder usage value. + # + # @note + # This method is primarily used to help build example commands + # that accept certain params. + # + # @since 0.2.0 + # + def param_usage(param) + case param.type + when Core::Params::Types::Boolean then 'BOOL' + when Core::Params::Types::Integer then 'INT' + when Core::Params::Types::Float then 'FLOAT' + when Core::Params::Types::Regexp then '/REGEX/' + when Core::Params::Types::URI then 'URL' + else + param.name.upcase + end + end end end end diff --git a/spec/cli/printing/params_spec.rb b/spec/cli/printing/params_spec.rb index 53fd02d..9c6d4ef 100644 --- a/spec/cli/printing/params_spec.rb +++ b/spec/cli/printing/params_spec.rb @@ -60,4 +60,67 @@ class TestCommand < Ronin::Core::CLI::Command end end end + + describe "#param_usage" 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 Boolean type" do + let(:type) { Ronin::Core::Params::Types::Boolean.new } + + it "must return 'BOOL'" do + expect(subject.param_usage(param)).to eq('BOOL') + end + end + + context "when given a param with an Integer type" do + let(:type) { Ronin::Core::Params::Types::Integer.new } + + it "must return 'INT'" do + expect(subject.param_usage(param)).to eq('INT') + end + end + + context "when given a param with a Float type" do + let(:type) { Ronin::Core::Params::Types::Float.new } + + it "must return 'FLOAT'" do + expect(subject.param_usage(param)).to eq('FLOAT') + end + end + + context "when given a param with a Regexp type" do + let(:type) { Ronin::Core::Params::Types::Regexp.new } + + it "must return '/REGEX/'" do + expect(subject.param_usage(param)).to eq('/REGEX/') + end + end + + context "when given a param with a URI type" do + let(:type) { Ronin::Core::Params::Types::URI.new } + + it "must return 'URL'" do + expect(subject.param_usage(param)).to eq('URL') + end + end + + context "when given a param with a String type" do + let(:type) { Ronin::Core::Params::Types::String.new } + + it "must return the upcased version of the param name" do + expect(subject.param_usage(param)).to eq(name.upcase) + end + end + + context "when given a param with a Enum type" do + let(:type) { Ronin::Core::Params::Types::Enum[:bar, :baz] } + + it "must return the upcased version of the param name" do + expect(subject.param_usage(param)).to eq(name.upcase) + end + end + end end