Class: Tabry::Result

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, state) ⇒ Result

Returns a new instance of Result.



11
12
13
14
# File 'lib/tabry/result.rb', line 11

def initialize(config, state)
  @config = config
  @state = state
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/tabry/result.rb', line 9

def config
  @config
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/tabry/result.rb', line 9

def state
  @state
end

Instance Method Details

#current_flags_for_flagargsObject



115
116
117
118
119
120
# File 'lib/tabry/result.rb', line 115

def current_flags_for_flagargs
  sub_stack.map do |sub|
    flag = sub.flags[state.current_flag]
    flag if flag&.arg
  end.compact
end

#current_subObject



16
17
18
# File 'lib/tabry/result.rb', line 16

def current_sub
  @current_sub ||= sub_stack.last
end

#expected_argObject



122
123
124
125
126
127
128
# File 'lib/tabry/result.rb', line 122

def expected_arg
  if current_sub.args.n_passed_in_varargs(state.args.length) > 0
    current_sub.args.varargs_arg
  else
    current_sub.args[state.args.length]
  end
end

#help?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/tabry/result.rb', line 97

def help?
  state.help
end

#invalid_usage_reasonObject



24
25
26
27
28
29
# File 'lib/tabry/result.rb', line 24

def invalid_usage_reason
  waiting_on_flag_arg? ||
    wrong_number_of_args? ||
    missing_required_flags? ||
    nil
end

#missing_required_flags?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/tabry/result.rb', line 79

def missing_required_flags?
  current_sub.flags.each do |flag|
    if flag.required && !state.flags[flag.name]
      return "missing required flag #{flag.name}"
    end
  end
  false
end

#n_passed_in_varargsObject



75
76
77
# File 'lib/tabry/result.rb', line 75

def n_passed_in_varargs
  current_sub.args.n_passed_in_varargs(state.args.count)
end

#named_argsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tabry/result.rb', line 101

def named_args
  @named_args ||= {}.tap do |res|
    if (varargs_name = current_sub.args.varargs_arg&.name)
      res[varargs_name] = varargs
    end

    non_varargs.each_with_index do |arg_val, i|
      if (arg_name = current_sub.args[i]&.name)
        res[arg_name] = arg_val
      end
    end
  end
end

#non_varargsObject



67
68
69
# File 'lib/tabry/result.rb', line 67

def non_varargs
  @non_varargs ||= state.args.first(state.args.length - n_passed_in_varargs)
end

#sub_stackObject



20
21
22
# File 'lib/tabry/result.rb', line 20

def sub_stack
  @sub_stack ||= config.dig_sub_array(state.subcommand_stack)
end

#top_level?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/tabry/result.rb', line 93

def top_level?
  state.subcommand_stack.empty?
end

#usage(cmd_name = nil) ⇒ Object



88
89
90
91
# File 'lib/tabry/result.rb', line 88

def usage(cmd_name = nil)
  cmd_name ||= config.cmd
  Tabry::UsageGenerator.new(sub_stack, cmd_name).usage
end

#varargsObject



71
72
73
# File 'lib/tabry/result.rb', line 71

def varargs
  @varargs ||= state.args.last(n_passed_in_varargs)
end

#waiting_on_flag_arg?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/tabry/result.rb', line 31

def waiting_on_flag_arg?
  if state.mode != :subcommand
    "arg required for flag #{state.current_flag}"
  end
end

#wrong_number_of_args?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tabry/result.rb', line 37

def wrong_number_of_args?
  n_args = state.args.length
  # This could use some tweaking. I'm not at which point a subcommand
  # should be considered to be invalid with no arguments... if it has a
  # subcommand it's a good sign, but if it also has an optional argument
  # that means it's OK. Probably need another "no_args" construct.
  if n_args == 0 && current_sub.subs.any? && !(current_sub.args.any? && current_sub.min_args == 0)
    if current_sub.args.any?
      "missing subcommand or arg(s)"
    else
      "missing subcommand"
    end
  elsif !(current_sub.min_args..current_sub.max_args).include?(n_args)
    if n_args == 0
      if current_sub.max_args == 1
        msg = "missing argument"
        msg += " #{current_sub.args[0].name.inspect}" if current_sub.args[0].name
        msg
      elsif current_sub.min_args == 1
        "missing one or more args"
      else
        "missing args"
      end
    else
      "got #{state.args.count} args, " \
        "must be between #{current_sub.min_args} and #{current_sub.max_args}"
    end
  end
end