lib/inch/cli/command/base.rb in inch-0.5.0.rc3 vs lib/inch/cli/command/base.rb in inch-0.5.0.rc4
- old
+ new
@@ -43,11 +43,12 @@
# Helper method to run an instance with the given +args+
#
# @see #run
# @return [Command::Base] the instance that ran
def self.run(*args)
- command = new
+ kwargs = args.last.is_a?(Hash) ? args.pop : {}
+ command = new(kwargs)
command.run(*args)
command
end
# Registers the current Command in the CommandParser
@@ -57,15 +58,13 @@
def self.register_command_as(name, default = false)
CLI::CommandParser.default_command = name if default
CLI::CommandParser.commands[name] = self
end
- def initialize
- name = self.class.to_s.split('::').last
- options_class = Command::Options.const_get(name)
- @options = options_class.new
- @options.usage = usage
+ def initialize(kwargs = {})
+ @ui = kwargs[:ui] if kwargs[:ui]
+ initialize_cli_options
end
# Returns a description of the command
#
# @return [String]
@@ -85,19 +84,41 @@
# Runs the command with the given +args+
#
# @abstract
# @note Override with implementation
- # @param *args [Array<String>]
- def run(*args)
- raise NotImplementedError
+ # @param *_args [Array<String>]
+ def run(*_args)
+ fail NotImplementedError
end
# Returns a description of the command's usage pattern
#
# @return [String]
def usage
"Usage: inch #{name} [options]"
+ end
+
+ protected
+
+ def initialize_cli_options
+ name = self.class.to_s.split("::").last
+ options_class = Command::Options.const_get(name)
+ @options = options_class.new
+ @options.usage = usage
+ end
+
+ # Creates a Config::Codebase object and returns it
+ # (merges relevant values of a given +options+ object before).
+ #
+ # @param options [Options::Base]
+ # @return [Config::Codebase]
+ def to_config(options)
+ config = Config.for(@options.language, Dir.pwd).codebase
+ config.included_files = options.paths unless options.paths.empty?
+ config.excluded_files = options.excluded unless options.excluded.empty?
+ config.read_dump_file = options.read_dump_file
+ config
end
end
end
end
end