lib/lapidarius/cli.rb in lapidarius-3.4.0 vs lib/lapidarius/cli.rb in lapidarius-4.0.1
- old
+ new
@@ -1,47 +1,59 @@
+require "optparse"
require "lapidarius/cutter"
module Lapidarius
class CLI
HELP_FLAGS = %w[-h --help]
- COL_WIDTH = 23
- attr_reader :name, :version
+ attr_reader :name, :version, :remote
- def initialize(input,
- out = STDOUT,
- command = Command,
- cutter = Cutter,
- tree = Tree)
- @name, @version = Array(input).map(&:strip)
- @out = out
- @cutter = cutter.new(name: @name, cmd_klass: command, version: @version)
+ def initialize(args: [], io: STDOUT, command: Command, cutter: Cutter, tree: Tree)
+ @args = args
+ @io = io
@tree = tree
+ @command = command
+ @cutter = cutter
+ @name = @args.shift unless help?
+ parser.parse!(@args)
end
def call
- @out.puts output
+ @io.puts output
end
+ private def cutter
+ @cutter.new(name: @name, cmd_klass: @command, version: @version, remote: @remote)
+ end
+
private def output
- return help if help?
return unless @name
- gem = @cutter.call
+ gem = cutter.call
@tree::new(gem).to_s
rescue Gem::NotInstalledError => e
e.message
end
- private def help?
- HELP_FLAGS.include?(@name)
- end
+ private def parser
+ OptionParser.new do |opts|
+ opts.banner = %q{Usage: lapidarius sinatra --version=1.4.7 --remote}
- private def help
- [].tap do |h|
- h << %q{Usage: lapidarius <name> <version>}
- h << " %-#{COL_WIDTH}s Print this help" % "-h --help"
- h << " %-#{COL_WIDTH}s Gem's name to cut" % "<name>"
- h << " %-#{COL_WIDTH}s Gem's version to cut" % "<version>"
+ opts.on("-vVERSION", "--version=VERSION", "Specify the gem version to cut") do |version|
+ @version = version
+ end
+
+ opts.on("-r", "--remote", "Fetch gem remotely") do |remote|
+ @remote = true
+ end
+
+ opts.on(*HELP_FLAGS, "Prints this help") do
+ @io.puts opts
+ exit
+ end
end
+ end
+
+ private def help?
+ HELP_FLAGS.any? { |h| @args.first == h }
end
end
end