Class: Tabry::CLI::ArgProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tabry/cli/arg_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, named_args, reqd: false) ⇒ ArgProxy

Returns a new instance of ArgProxy.



10
11
12
13
14
15
16
# File 'lib/tabry/cli/arg_proxy.rb', line 10

def initialize(args, named_args, reqd: false)
  @args = args
  @named_args = FlagProxy.new(named_args)
  @raw_named_args = named_args
  @reqd = reqd
  @reqd_arg_proxy = ArgProxy.new(args, named_args, reqd: true) unless @reqd
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(met) ⇒ Object



50
51
52
# File 'lib/tabry/cli/arg_proxy.rb', line 50

def method_missing(met)
  self[met]
end

Instance Method Details

#<=>(*args) ⇒ Object



46
47
48
# File 'lib/tabry/cli/arg_proxy.rb', line 46

def <=>(*args)
  @args.send(:<=>, *args)
end

#[](key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tabry/cli/arg_proxy.rb', line 22

def [](key)
  if key.is_a?(Integer)
    res = @args[key]
    if @reqd && !res
      warn "FATAL: Missing required argument number #{key + 1}"
      Kernel.exit 1
    end
  else
    key = key.to_s
    res = @named_args[key]
    if @reqd && !res
      warn "FATAL: Missing required argument #{key}"
      Kernel.exit 1
    end
  end
  res
end

#each(&blk) ⇒ Object



18
19
20
# File 'lib/tabry/cli/arg_proxy.rb', line 18

def each(&blk)
  @args.each(&blk)
end

#inspectObject



58
59
60
# File 'lib/tabry/cli/arg_proxy.rb', line 58

def inspect
  "ArgProxy: #{@args.inspect}, #{@raw_named_args.inspect}"
end

#reqdObject



66
67
68
# File 'lib/tabry/cli/arg_proxy.rb', line 66

def reqd
  @reqd_arg_proxy or raise "no reqd.reqd"
end

#respond_to_missing?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/tabry/cli/arg_proxy.rb', line 54

def respond_to_missing?(*_args)
  true # Anything could be an arg name that we haven't set
end

#slice(*keys) ⇒ Object



40
41
42
43
44
# File 'lib/tabry/cli/arg_proxy.rb', line 40

def slice(*keys)
  [keys].flatten.each_with_object({}) do |key, result_hash|
    result_hash[key] = self[key]
  end
end

#to_sObject



62
63
64
# File 'lib/tabry/cli/arg_proxy.rb', line 62

def to_s
  "ArgProxy: #{@args}, #{@raw_named_args.inspect}"
end