Class: Cl::Cmd

Inherits:
Object
  • Object
show all
Extended by:
Dsl, Merge, Suggest, Underscore
Includes:
Registry
Defined in:
lib/cl/cmd.rb,
lib/cl/dsl.rb

Overview

Base class for all command classes that can be run.

Inherit your command classes from this class, use the Dsl to declare arguments, options, summary, description, examples etc., and implement the method #run.

See Dsl for details on the DSL methods.

Direct Known Subclasses

Help

Defined Under Namespace

Modules: Dsl

Constant Summary

Constants included from Merge

Merge::MERGE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Merge

merge

Methods included from Suggest

suggest

Methods included from Underscore

underscore

Methods included from Dsl

abstract, abstract?, arg, description, examples, opt, required, required?, summary

Constructor Details

#initialize(ctx, args) ⇒ Cmd

Returns a new instance of Cmd.



58
59
60
61
62
63
# File 'lib/cl/cmd.rb', line 58

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



56
57
58
# File 'lib/cl/cmd.rb', line 56

def args
  @args
end

#ctxObject (readonly)

Returns the value of attribute ctx.



56
57
58
# File 'lib/cl/cmd.rb', line 56

def ctx
  @ctx
end

Class Method Details

.cmdsObject



36
37
38
# File 'lib/cl/cmd.rb', line 36

def cmds
  registry.values.uniq
end

.parse(ctx, cmd, args) ⇒ Object



40
41
42
43
44
45
# File 'lib/cl/cmd.rb', line 40

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

.suggestions(opt) ⇒ Object



47
48
49
# File 'lib/cl/cmd.rb', line 47

def suggestions(opt)
  suggest(opts.map(&:name), opt.sub(/^--/, ''))
end

Instance Method Details

#deprecationsObject



69
70
71
# File 'lib/cl/cmd.rb', line 69

def deprecations
  @deprecations ||= {}
end

#optsObject



65
66
67
# File 'lib/cl/cmd.rb', line 65

def opts
  @opts ||= {}
end