Skip to content

Commit

Permalink
Added CLI::Printing::Params#param_usage (closes #42).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Jan 4, 2024
1 parent 7746891 commit 98ca6df
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/ronin/core/cli/printing/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions spec/cli/printing/params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 98ca6df

Please sign in to comment.