class One
Represents a single positional argument in a command.
A One parser extracts exactly one argument from the command line that matches the specified pattern.
Definitions
def initialize(key, description, pattern: //, default: nil, required: false, completions: nil)
Initialize a new positional argument parser.
Signature
-
parameter
keySymbol The name of the attribute to store the value in.
-
parameter
descriptionString A description of the argument for help output.
-
parameter
patternRegexp A pattern to match valid values.
-
parameter
defaultObject The default value if no argument is provided.
-
parameter
requiredBoolean Whether the argument is required.
-
parameter
completionsArray | Proc | Nil Completions for this argument.
Implementation
def initialize(key, description, pattern: //, default: nil, required: false, completions: nil)
@key = key
@description = description
@pattern = pattern
@default = default
@required = required
@completions = completions
end
attr :key
The name of the attribute to store the value in.
Signature
-
attribute
Symbol
attr :description
A description of the argument for help output.
Signature
-
attribute
String
attr :pattern
A pattern to match valid values.
Signature
-
attribute
Regexp
attr :default
The default value if no argument is provided.
Signature
-
attribute
Object
attr :required
Whether the argument is required.
Signature
-
attribute
Boolean
attr :completions
Completions for this argument.
Signature
-
attribute
Array | Proc | Nil
def to_s
Generate a string representation for usage output.
Signature
-
returns
String The usage string.
Implementation
def to_s
"<#{@key}>"
end
def to_a
Generate an array representation for usage output.
Signature
-
returns
Array The usage array.
Implementation
def to_a
usage = [to_s, @description]
if @default
usage << "(default: #{@default.inspect})"
elsif @required
usage << "(required)"
end
return usage
end
def parse(input, parent = nil, default = nil)
Parse a single argument from the input.
Signature
-
parameter
inputArray(String) The command-line arguments.
-
parameter
parentCommand | Nil The parent command.
-
parameter
defaultObject | Nil An override for the default value.
-
returns
String | Object | Nil The parsed value, or the default if no match.
Implementation
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
def complete(input, context, collected)
Complete this positional argument.
Signature
-
parameter
inputArray(String) Previously completed command-line arguments.
-
parameter
contextCompletion::Context The completion context.
-
parameter
collectedArray(Completion::Suggestion) Suggestions collected so far.
-
returns
Completion::Result | Nil A final completion result, or nil to continue.
Implementation
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