Class: RunKit::Options::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/run_kit/options/flag.rb

Constant Summary collapse

SWITCH_RE =

--foo or -f

/\A-(\w|-\w[\w-]*)\z/
INLINE_RE =

--foo=bar

/\A-(\w|-\w[\w-]*)=(.*)\z/m
NEGATE_RE =

--no-foo

/\A--no-(\w[\w-]*)\z/
KINDS =
%i[bool float int path str sym]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, opts, default: nil, required: false, choices: nil) ⇒ Flag

ctor



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/run_kit/options/flag.rb', line 20

def initialize(kind, opts, default: nil, required: false, choices: nil)
  @choices, @kind, @required = choices, kind, required

  # extract @help from last string
  @switches = opts.dup
  @help = if switch && !switch.start_with?("-")
    switches.pop
  end

  # Extract meta from the final switch, if written as `--port <int>`.
  @meta = build_meta

  # default, with some special handling for bool
  @default = default
  if bool? && default.nil? && !required?
    @default = false
  end

  validate
end

Instance Attribute Details

#choicesObject (readonly)

Returns the value of attribute choices.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def choices
  @choices
end

#defaultObject (readonly)

Returns the value of attribute default.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def default
  @default
end

#helpObject (readonly)

Returns the value of attribute help.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def help
  @help
end

#kindObject (readonly)

Returns the value of attribute kind.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def kind
  @kind
end

#metaObject (readonly)

Returns the value of attribute meta.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def meta
  @meta
end

#requiredObject (readonly) Also known as: required?

Returns the value of attribute required.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def required
  @required
end

#switchesObject (readonly)

Returns the value of attribute switches.



17
18
19
# File 'lib/run_kit/options/flag.rb', line 17

def switches
  @switches
end

Instance Method Details

#bool?Boolean

one-liners

Returns:

  • (Boolean)


72
# File 'lib/run_kit/options/flag.rb', line 72

def bool? = kind == :bool

#keyObject



73
# File 'lib/run_kit/options/flag.rb', line 73

def key = @key ||= switch.sub(/^-+/, "").tr("-", "_").to_sym

#parse(switch, param) ⇒ Object

Parse a single cli param

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/run_kit/options/flag.rb', line 46

def parse(switch, param)
  if bool?
    raise Error, "option '#{switch}=#{param}' does not take a value" if param
    return true
  end

  raise Error, "option '#{switch}' requires a value" if !param
  parsed = begin
    case kind
    when :float then Float(param)
    when :int then Integer(param, 10)
    when :path then Pathname.new(param)
    when :str then param
    when :sym then param.to_sym
    end
  rescue ArgumentError
    raise Error, "invalid value '#{param}' for option '#{switch}'"
  end

  if choices && !choices.include?(parsed)
    raise Error, "invalid value '#{parsed}' for option '#{switch}', must be one of #{choices.join(", ")}"
  end
  parsed
end

#switchObject



74
# File 'lib/run_kit/options/flag.rb', line 74

def switch = switches.last

#takes_param?Boolean

Returns:

  • (Boolean)


75
# File 'lib/run_kit/options/flag.rb', line 75

def takes_param? = !bool?