Class: Cl::Cmd

Inherits:
Object
  • Object
show all
Extended by:
Merge
Includes:
Registry
Defined in:
lib/cl/cmd.rb

Direct Known Subclasses

Help

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

Constructor Details

#initialize(ctx, args) ⇒ Cmd

Returns a new instance of Cmd.


85
86
87
88
89
90
# File 'lib/cl/cmd.rb', line 85

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args


83
84
85
# File 'lib/cl/cmd.rb', line 83

def args
  @args
end

#ctxObject (readonly)

Returns the value of attribute ctx


83
84
85
# File 'lib/cl/cmd.rb', line 83

def ctx
  @ctx
end

Class Method Details

.abstractObject


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

def abstract
  unregister
end

.arg(*args) ⇒ Object


27
28
29
# File 'lib/cl/cmd.rb', line 27

def arg(*args)
  self.args.define(self, *args)
end

.args(*args) ⇒ Object


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

def args(*args)
  return @args ||= Args.new unless args.any?
  opts = args.last.is_a?(Hash) ? args.pop : {}
  args.each { |arg| arg(arg, opts) }
end

.cmdsObject


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

def cmds
  registry.values
end

.description(description = nil) ⇒ Object


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

def description(description = nil)
  description ? @description = description : @description
end

.examples(examples = nil) ⇒ Object


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

def examples(examples = nil)
  examples ? @examples = examples : @examples
end

.opt(*args, &block) ⇒ Object


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

def opt(*args, &block)
  self.opts.define(self, *args, &block)
end

.optsObject


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

def opts
  @opts ||= self == Cmd ? Opts.new : superclass.opts.dup
end

.parse(ctx, args) ⇒ Object


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

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

.required(*required) ⇒ Object


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

def required(*required)
  required.any? ? self.required << required : @required ||= []
end

.required?Boolean

Returns:

  • (Boolean)

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

def required?
  !!@required
end

.summary(summary = nil) ⇒ Object Also known as: purpose


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

def summary(summary = nil)
  summary ? @summary = summary : @summary
end

.underscore(string) ⇒ Object


74
75
76
77
78
# File 'lib/cl/cmd.rb', line 74

def underscore(string)
  string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  downcase
end

Instance Method Details

#deprecated_optsObject


96
97
98
99
100
# File 'lib/cl/cmd.rb', line 96

def deprecated_opts
  opts = self.class.opts.select(&:deprecated?)
  opts = opts.select { |opt| self.opts.key?(opt.deprecated[0]) }
  opts.map(&:deprecated).to_h
end

#optsObject


92
93
94
# File 'lib/cl/cmd.rb', line 92

def opts
  @opts ||= {}
end