Class: CLIApp::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/cliapp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc = nil, option_schema = {}, &block) ⇒ Action

Returns a new instance of Action.



26
27
28
29
30
31
# File 'lib/cliapp.rb', line 26

def initialize(name, desc=nil, option_schema={}, &block)
  @name = name
  @desc = desc
  @option_schema = option_schema
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



33
34
35
# File 'lib/cliapp.rb', line 33

def block
  @block
end

#descObject (readonly)

Returns the value of attribute desc.



33
34
35
# File 'lib/cliapp.rb', line 33

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/cliapp.rb', line 33

def name
  @name
end

#option_schemaObject (readonly)

Returns the value of attribute option_schema.



33
34
35
# File 'lib/cliapp.rb', line 33

def option_schema
  @option_schema
end

Instance Method Details

#call(*args, **opts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cliapp.rb', line 35

def call(*args, **opts)
  #; [!pc2hw] raises error when fewer arguments.
  n_min, n_max = Util.arity_of_proc(@block)
  args.length >= n_min  or
    raise ActionTooFewArgsError, "Too few arguments."
  #; [!6vdhh] raises error when too many arguments.
  n_max == nil || args.length <= n_max  or
    raise ActionTooManyArgsError, "Too many arguments."
  #; [!7n4hs] invokes action block with args and kwargs.
  if opts.empty?                 # for Ruby < 2.7
    @block.call(*args)           # for Ruby < 2.7
  else
    @block.call(*args, **opts)
  end
end