Class: RunKit::Options::Flag
- Inherits:
-
Object
- Object
- RunKit::Options::Flag
- 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
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#required ⇒ Object
(also: #required?)
readonly
Returns the value of attribute required.
-
#switches ⇒ Object
readonly
Returns the value of attribute switches.
Instance Method Summary collapse
-
#bool? ⇒ Boolean
one-liners.
-
#initialize(kind, opts, default: nil, required: false, choices: nil) ⇒ Flag
constructor
ctor.
- #key ⇒ Object
-
#parse(switch, param) ⇒ Object
Parse a single cli param.
- #switch ⇒ Object
- #takes_param? ⇒ Boolean
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 = # default, with some special handling for bool @default = default if bool? && default.nil? && !required? @default = false end validate end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
17 18 19 |
# File 'lib/run_kit/options/flag.rb', line 17 def choices @choices end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
17 18 19 |
# File 'lib/run_kit/options/flag.rb', line 17 def default @default end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
17 18 19 |
# File 'lib/run_kit/options/flag.rb', line 17 def help @help end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
17 18 19 |
# File 'lib/run_kit/options/flag.rb', line 17 def kind @kind end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
17 18 19 |
# File 'lib/run_kit/options/flag.rb', line 17 def @meta end |
#required ⇒ Object (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 |
#switches ⇒ Object (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
72 |
# File 'lib/run_kit/options/flag.rb', line 72 def bool? = kind == :bool |
#key ⇒ Object
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
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 |
#switch ⇒ Object
74 |
# File 'lib/run_kit/options/flag.rb', line 74 def switch = switches.last |
#takes_param? ⇒ Boolean
75 |
# File 'lib/run_kit/options/flag.rb', line 75 def takes_param? = !bool? |