Sha256: 28919bdffcc45ad2b475776530b9de8fe72e6f7c58b36b6332730784938a059e

Contents?: true

Size: 1.38 KB

Versions: 19

Compression:

Stored size: 1.38 KB

Contents

require 'registry'
require 'cl/args'
require 'cl/dsl'
require 'cl/opts'
require 'cl/parser'

class Cl
  # 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, Underscore

      inherited = ->(const) do
        const.register [registry_key, underscore(const.name.split('::').last)].compact.join(':') if const.name
        const.define_singleton_method(:inherited, &inherited)
      end
      define_method(:inherited, &inherited)

      def cmds
        registry.values
      end

      def parse(ctx, args)
        opts = Parser.new(self.opts, args).opts unless self == Help
        opts = merge(ctx.config[registry_key], opts) if ctx.config[registry_key]
        [args, opts || {}]
      end
    end

    opt '--help', 'Get help on this command'

    attr_reader :ctx, :args
    attr_accessor :deprecations

    def initialize(ctx, args)
      args, opts = self.class.parse(ctx, args)
      @ctx = ctx
      @opts = self.class.opts.apply(self, self.opts.merge(opts))
      @args = self.class.args.apply(self, args, opts)
    end

    def opts
      @opts ||= {}
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
cl-1.0.5 lib/cl/cmd.rb
cl-1.0.4 lib/cl/cmd.rb
cl-1.0.3 lib/cl/cmd.rb
cl-1.0.2 lib/cl/cmd.rb
cl-1.0.1 lib/cl/cmd.rb
cl-1.0.0 lib/cl/cmd.rb
cl-0.1.28 lib/cl/cmd.rb
cl-0.1.27 lib/cl/cmd.rb
cl-0.1.26 lib/cl/cmd.rb
cl-0.1.25 lib/cl/cmd.rb
cl-0.1.24 lib/cl/cmd.rb
cl-0.1.23 lib/cl/cmd.rb
cl-0.1.22 lib/cl/cmd.rb
cl-0.1.21 lib/cl/cmd.rb
cl-0.1.20 lib/cl/cmd.rb
cl-0.1.19 lib/cl/cmd.rb
cl-0.1.18 lib/cl/cmd.rb
cl-0.1.17 lib/cl/cmd.rb
cl-0.1.16 lib/cl/cmd.rb