lib/simple/cli/runner/command_help.rb in simple-cli-0.1.1 vs lib/simple/cli/runner/command_help.rb in simple-cli-0.1.2
- old
+ new
@@ -1,14 +1,19 @@
+# rubocop:disable Metrics/AbcSize
+# rubocop:disable Metrics/CyclomaticComplexity
+# rubocop:disable Metrics/MethodLength
+
class Simple::CLI::Runner::CommandHelp
def self.option_names(app, subcommand)
new(app, subcommand).option_names
rescue NameError
[]
end
def initialize(mod, method)
- @mod, @method = mod, method
+ @mod = mod
+ @method = method
end
# First line of the help as read from the method comments.
def head
comments.first
@@ -20,18 +25,20 @@
end
def option_names
method = @mod.instance_method(@method)
- method.parameters.map do |mode, name|
+ option_names = method.parameters.map do |mode, name|
case mode
when :key then name
when :keyreq then name
end
- end.compact.map do |name|
- [ "--#{name}", "--#{name}=" ]
- end.flatten.uniq
+ end.compact
+
+ option_names.map do |name|
+ ["--#{name}", "--#{name}="]
+ end.flatten
end
# A help string constructed from the commands method signature.
def interface(binary_name, command_name)
method = @mod.instance_method(@method)
@@ -69,35 +76,30 @@
def parsed_source(file)
File.readlines(file).map do |line|
case line
when /^\s*# ?(.*)$/ then $1
when /^\s*end/ then :end
- else nil
end
end
end
def extract_comments(from:, before_line:)
parsed_source = from
# go down from before_line until we see a line which is either a comment
# or an :end. Note that the line at before_line-1 should be the first
# line of the method definition in question.
- last_line = before_line-1
- while last_line >= 0 && !parsed_source[last_line] do
- last_line -= 1
- end
+ last_line = before_line - 1
+ last_line -= 1 while last_line >= 0 && !parsed_source[last_line]
first_line = last_line
- while first_line >= 0 && parsed_source[first_line] do
- first_line -= 1
- end
+ first_line -= 1 while first_line >= 0 && parsed_source[first_line]
first_line += 1
- comments = parsed_source[first_line .. last_line]
+ comments = parsed_source[first_line..last_line]
if comments.include?(:end)
[]
else
- parsed_source[first_line .. last_line]
+ parsed_source[first_line..last_line]
end
end
end