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

Class Attribute Summary collapse

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.



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

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

Class Attribute Details

.auto_registerObject

Returns the value of attribute auto_register.



23
24
25
# File 'lib/cl/cmd.rb', line 23

def auto_register
  @auto_register
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#ctxObject (readonly)

Returns the value of attribute ctx.



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

def ctx
  @ctx
end

Class Method Details

.cmdsObject



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

def cmds
  registry.values.uniq
end

.parse(ctx, cmd, args) ⇒ Object



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

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



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

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

Instance Method Details

#deprecationsObject



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

def deprecations
  @deprecations ||= {}
end

#optsObject



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

def opts
  @opts ||= {}
end