Class: Samovar::One
- Inherits:
-
Object
- Object
- Samovar::One
- Defined in:
- lib/samovar/one.rb
Overview
Represents a single positional argument in a command.
A ‘One` parser extracts exactly one argument from the command line that matches the specified pattern.
Instance Attribute Summary collapse
-
#completions ⇒ Object
readonly
Completions for this argument.
-
#default ⇒ Object
The default value if no argument is provided.
-
#description ⇒ Object
readonly
A description of the argument for help output.
-
#key ⇒ Object
The name of the attribute to store the value in.
-
#pattern ⇒ Object
A pattern to match valid values.
-
#required ⇒ Object
Whether the argument is required.
Instance Method Summary collapse
-
#complete(input, context, collected) ⇒ Object
Complete this positional argument.
-
#initialize(key, description, pattern: //, default: nil, required: false, completions: nil) ⇒ One
constructor
Initialize a new positional argument parser.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse a single argument from the input.
-
#to_a ⇒ Object
Generate an array representation for usage output.
-
#to_s ⇒ Object
Generate a string representation for usage output.
Constructor Details
#initialize(key, description, pattern: //, default: nil, required: false, completions: nil) ⇒ One
Initialize a new positional argument parser.
21 22 23 24 25 26 27 28 |
# File 'lib/samovar/one.rb', line 21 def initialize(key, description, pattern: //, default: nil, required: false, completions: nil) @key = key @description = description @pattern = pattern @default = default @required = required @completions = completions end |
Instance Attribute Details
#completions ⇒ Object (readonly)
Completions for this argument.
58 59 60 |
# File 'lib/samovar/one.rb', line 58 def completions @completions end |
#default ⇒ Object
The default value if no argument is provided.
48 49 50 |
# File 'lib/samovar/one.rb', line 48 def default @default end |
#description ⇒ Object (readonly)
A description of the argument for help output.
38 39 40 |
# File 'lib/samovar/one.rb', line 38 def description @description end |
#key ⇒ Object
The name of the attribute to store the value in.
33 34 35 |
# File 'lib/samovar/one.rb', line 33 def key @key end |
#pattern ⇒ Object
A pattern to match valid values.
43 44 45 |
# File 'lib/samovar/one.rb', line 43 def pattern @pattern end |
#required ⇒ Object
Whether the argument is required.
53 54 55 |
# File 'lib/samovar/one.rb', line 53 def required @required end |
Instance Method Details
#complete(input, context, collected) ⇒ Object
Complete this positional argument.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/samovar/one.rb', line 104 def complete(input, context, collected) if input.empty? return Completion::Result.new(collected) + Completion::Provider.new(context.with_row(self), @completions).suggestions elsif @pattern =~ input.first input.shift return nil else return Completion::Result.new(collected) end end |
#parse(input, parent = nil, default = nil) ⇒ Object
Parse a single argument from the input.
88 89 90 91 92 93 94 95 96 |
# File 'lib/samovar/one.rb', line 88 def parse(input, parent = nil, default = nil) if input.first =~ @pattern input.shift elsif default ||= @default return default elsif @required raise MissingValueError.new(parent, @key) end end |
#to_a ⇒ Object
Generate an array representation for usage output.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/samovar/one.rb', line 70 def to_a usage = [to_s, @description] if @default usage << "(default: #{@default.inspect})" elsif @required usage << "(required)" end return usage end |
#to_s ⇒ Object
Generate a string representation for usage output.
63 64 65 |
# File 'lib/samovar/one.rb', line 63 def to_s "<#{@key}>" end |