lib/repofetch/cli.rb in repofetch-0.5.0 vs lib/repofetch/cli.rb in repofetch-0.5.1

- old
+ new

@@ -2,10 +2,11 @@ require 'optparse' require 'repofetch' require 'repofetch/config' require 'repofetch/exceptions' +require 'repofetch/version' class Repofetch # Command line interface for repofetch. class CLI attr_reader :repository_path, :plugin @@ -25,28 +26,23 @@ # @return [Integer] The exit code. def start load_plugins define_options.parse!(@args) - begin - plugin = new_plugin - rescue Repofetch::PluginUsageError => e - warn e - return 1 - end + return @exit unless @exit.nil? - puts plugin - 0 + start_plugin end def define_options OptionParser.new do |opts| opts.banner = 'Usage: repofetch [options] -- [plugin arguments]' add_repository_options(opts) add_plugin_options(opts) add_options_notes(opts) + add_version_option(opts) end end def load_plugins @config.plugins.each { |plugin| require plugin } @@ -58,10 +54,17 @@ Repofetch.get_plugin(@repository_path, @args) end private + def add_version_option(opts) + opts.on('-v', '--version', 'Print the version number and exit.') do + puts "repofetch #{Repofetch::VERSION}" + @exit = 0 + end + end + def add_repository_options(opts) opts.on('-r', '--repository', '-p', '--path PATH', 'Use the provided path. Defaults to the current directory.') do |path| @repository_path = path end @@ -88,8 +91,20 @@ opts.separator "Installed plugins: #{available_plugins.keys.join(', ')}" end def available_plugins Repofetch.plugins.to_h { |plugin| [plugin.name, plugin] } + end + + def start_plugin + begin + plugin = new_plugin + rescue Repofetch::PluginUsageError => e + warn e + return 1 + end + + puts plugin + 0 end end end