lib/mercenary/command.rb in mercenary-0.3.6 vs lib/mercenary/command.rb in mercenary-0.4.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: false + module Mercenary class Command attr_reader :name attr_reader :description attr_reader :syntax @@ -45,13 +47,11 @@ # # Returns the syntax string and sets it if an argument is present def syntax(syntax = nil) @syntax = syntax if syntax syntax_list = [] - if parent - syntax_list << parent.syntax.to_s.gsub(/<[\w\s-]+>/, '').gsub(/\[[\w\s-]+\]/, '').strip - end + syntax_list << parent.syntax.to_s.gsub(%r!<[\w\s-]+>!, "").gsub(%r!\[[\w\s-]+\]!, "").strip if parent syntax_list << (@syntax || name.to_s) syntax_list.join(" ") end # Public: Sets or gets the command description @@ -70,15 +70,15 @@ # present # # Returns the default command if there is one, `nil` otherwise def default_command(command_name = nil) if command_name - if commands.has_key?(command_name) + if commands.key?(command_name) @default_command = commands[command_name] if command_name @default_command else - raise ArgumentError.new("'#{command_name}' couldn't be found in this command's list of commands.") + raise ArgumentError, "'#{command_name}' couldn't be found in this command's list of commands." end else @default_command end end @@ -131,15 +131,16 @@ # Public: Fetch a Logger (stdlib) # # level - the logger level (a Logger constant, see docs for more info) # # Returns the instance of Logger + def logger(level = nil) unless @logger @logger = Logger.new(STDOUT) @logger.level = level || Logger::INFO - @logger.formatter = proc do |severity, datetime, progname, msg| + @logger.formatter = proc do |severity, _datetime, _progname, msg| "#{identity} | " << "#{severity.downcase.capitalize}:".ljust(7) << " #{msg}\n" end end @logger.level = level unless level.nil? @@ -187,19 +188,19 @@ # # opts - instance of OptionParser # # Returns nothing def add_default_options(opts) - option 'show_help', '-h', '--help', 'Show this message' - option 'show_version', '-v', '--version', 'Print the name and version' - option 'show_backtrace', '-t', '--trace', 'Show the full backtrace when an error occurs' + option "show_help", "-h", "--help", "Show this message" + option "show_version", "-v", "--version", "Print the name and version" + option "show_backtrace", "-t", "--trace", "Show the full backtrace when an error occurs" opts.on("-v", "--version", "Print the version") do puts "#{name} #{version}" exit(0) end - opts.on('-t', '--trace', 'Show full backtrace if an error occurs') do + opts.on("-t", "--trace", "Show full backtrace if an error occurs") do @trace = true end opts.on_tail("-h", "--help", "Show this message") do puts self @@ -226,11 +227,11 @@ # sub_command - the name of the subcommand # # Returns true if this command is the parent of a command of name # 'sub_command' and false otherwise def has_command?(sub_command) - commands.keys.include?(sub_command) + commands.key?(sub_command) end # Public: Identify this command # # Returns a string which identifies this command @@ -240,19 +241,19 @@ # Public: Get the full identity (name & version) of this command # # Returns a string containing the name and version if it exists def identity - "#{full_name} #{version if version}".strip + "#{full_name} #{version}".strip end # Public: Get the name of the current command plus that of # its parent commands # # Returns the full name of the command def full_name the_name = [] - the_name << parent.full_name if parent && parent.full_name + the_name << parent.full_name if parent&.full_name the_name << name the_name.join(" ") end # Public: Return all the names and aliases for this command.