Sha256: 5cfe90593c67918c5f7e270b4ae4afdb7eef9dba1f1bad6c05d41a608bdb9f22
Contents?: true
Size: 1.76 KB
Versions: 4
Compression:
Stored size: 1.76 KB
Contents
require 'cl/cmd' require 'cl/help' require 'cl/runner' require 'cl/errors' class Cl attr_reader :ctx, :name, :opts # @overload initialize(ctx, name, opts) # @param ctx [Cl::Ctx] the current execution context (optional) # @param name [String] the program (executable) name (optional, defaults to the last segment of $0) # @param opts [Hash] options (optional) # @option opts [Cl::Runner] :runner registry key for a runner (optional, defaults to :default) # @option opts [Cl::Ui] :ui the ui for handling user interaction def initialize(*args) ctx = args.shift if args.first.is_a?(Ctx) @opts = args.last.is_a?(Hash) ? args.pop : {} @name = args.shift || $0.split('/').last @ctx = ctx || Ctx.new(name, opts) end # Runs the command. # # Instantiates a runner with the given arguments, and runs it. # # If the command fails (raises a Cl::Error) then the exception is caught, and # the process aborted with the error message and help output for the given # command. # # @param args [Array<String>] arguments (usually ARGV) def run(args) runner(untaint(args)).run rescue UnknownCmd => e ctx.abort e rescue Error => e ctx.abort e, help(args.first) end # Returns a runner instance for the given arguments. def runner(args) runner = :default if args.first.to_s == 'help' runner ||= opts[:runner] || :default Runner[runner].new(ctx, args) end # Returns help output for the given command def help(*args) runner(['help', *args]).cmd.help end private # Tainting is being used for identifying values on options that have # been declared as secret. Ruby taints incoming arguments, so we want to # untaint them here. def untaint(args) args.map(&:dup).each(&:untaint) end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
cl-1.1.3 | lib/cl.rb |
cl-1.1.2 | lib/cl.rb |
cl-1.1.1 | lib/cl.rb |
cl-1.1.0 | lib/cl.rb |