Sha256: 3c063304f553762ba8d8129c114d921a2dcb98b9ed83f25591432a7600d09056
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
require 'registry' require 'cl' require 'cl/args' require 'cl/dsl' require 'cl/opts' require 'cl/parser' class Cl singleton_class.send(:attr_accessor, :auto_register) # remove unless anyone needs this singleton_class.instance_variable_set(:@auto_register, true) # Base class for all command classes that can be run. # # Inherit your command classes from this class, use the {Cl::Cmd::Dsl} to # declare arguments, options, summary, description, examples etc., and # implement the method #run. # # See {Cl::Cmd::Dsl} for details on the DSL methods. class Cmd include Registry extend Dsl class << self include Merge, Suggest, Underscore inherited = ->(const) do # return unless Cl.auto_register if const.name key = underscore(const.name.split('::').last) key = [registry_key, key].compact.join(':') unless abstract? const.register(key) end const.define_singleton_method(:inherited, &inherited) end define_method(:inherited, &inherited) def cmds registry.values.uniq end def parse(ctx, cmd, args) parser = Parser.new(cmd, args) args, opts = parser.args, parser.opts unless self == Help opts = merge(ctx.config[registry_key], opts) if ctx.config[registry_key] [args, opts || {}] end def suggestions(opt) suggest(opts.map(&:name), opt.sub(/^--/, '')) end end abstract opt '--help', 'Get help on this command' attr_reader :ctx, :args def initialize(ctx, args) @ctx = ctx args, opts = self.class.parse(ctx, self, args) @opts = self.class.opts.apply(self, self.opts.merge(opts)) @args = self.class.args.apply(self, args, opts) unless help? && !is_a?(Help) end def opts @opts ||= {} end def deprecations @deprecations ||= {} end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cl-1.2.3 | lib/cl/cmd.rb |