class Samovar::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.
Attributes
Completions for this argument.
@attribute [Array | Proc | Nil]
The default value if no argument is provided.
@attribute [Object]
A description of the argument for help output.
@attribute [String]
The name of the attribute to store the value in.
@attribute [Symbol]
A pattern to match valid values.
@attribute [Regexp]
Whether the argument is required.
@attribute [Boolean]
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/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
Initialize a new positional argument parser.
@parameter key [Symbol] The name of the attribute to store the value in. @parameter description [String] A description of the argument for help output. @parameter pattern [Regexp] A pattern to match valid values. @parameter default [Object] The default value if no argument is provided. @parameter required [Boolean] Whether the argument is required. @parameter completions [Array | Proc | Nil] Completions for this argument.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/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
Complete this positional argument.
@parameter input [Array(String)] Previously completed command-line arguments. @parameter context [Completion::Context] The completion context. @parameter collected [Array(Completion::Suggestion)] Suggestions collected so far. @returns [Completion::Result | Nil] A final completion result, or nil to continue.
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/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
Parse a single argument from the input.
@parameter input [Array(String)] The command-line arguments. @parameter parent [Command | Nil] The parent command. @parameter default [Object | Nil] An override for the default value. @returns [String | Object | Nil] The parsed value, or the default if no match.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/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
Generate an array representation for usage output.
@returns [Array] The usage array.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/one.rb, line 63 def to_s "<#{@key}>" end
Generate a string representation for usage output.
@returns [String] The usage string.