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.



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

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)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



52
53
54
# File 'lib/cl/cmd.rb', line 52

def args
  @args
end

#ctxObject (readonly)

Returns the value of attribute ctx.



52
53
54
# File 'lib/cl/cmd.rb', line 52

def ctx
  @ctx
end

Class Method Details

.cmdsObject



32
33
34
# File 'lib/cl/cmd.rb', line 32

def cmds
  registry.values
end

.parse(ctx, cmd, args) ⇒ Object



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

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



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

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

Instance Method Details

#deprecationsObject



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

def deprecations
  @deprecations ||= {}
end

#optsObject



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

def opts
  @opts ||= {}
end