Class: Toys::ArgParser
- Inherits:
-
Object
- Object
- Toys::ArgParser
- Defined in:
- core-docs/toys/arg_parser.rb
Overview
Defined in the toys-core gem
An internal class that parses command line arguments for a tool.
Generally, you should not need to use this class directly. It is called from CLI.
Defined Under Namespace
Classes: ArgMissingError, ArgValueUnacceptableError, ExtraArgumentsError, FlagAmbiguousError, FlagGroupConstraintError, FlagUnrecognizedError, FlagValueMissingError, FlagValueNotAllowedError, FlagValueUnacceptableError, ToolUnrecognizedError, UsageError
Instance Attribute Summary collapse
-
#active_flag_def ⇒ Toys::Flag?
readonly
The current flag definition whose value is still pending.
-
#data ⇒ Hash
readonly
The collected tool data from parsed arguments.
-
#errors ⇒ Array<Toys::ArgParser::UsageError>
readonly
An array of parse error messages.
-
#parsed_args ⇒ Array<String>
readonly
All command line arguments that have been parsed.
-
#tool ⇒ Toys::ToolDefinition
readonly
The tool definition governing this parser.
-
#unmatched_args ⇒ Array<String>
readonly
All args that were not matched.
-
#unmatched_flags ⇒ Array<String>
readonly
Flags that were not matched.
-
#unmatched_positional ⇒ Array<String>
readonly
Extra positional args that were not matched.
Instance Method Summary collapse
-
#finish ⇒ self
Complete parsing.
-
#finished? ⇒ boolean
Determine if this parser is finished.
-
#flags_allowed? ⇒ boolean
Whether flags are currently allowed.
-
#initialize(cli, tool, default_data: {}, require_exact_flag_match: false) ⇒ ArgParser
constructor
Create an argument parser for a particular tool.
-
#next_arg_def ⇒ Toys::PositionalArg?
The argument definition that will be applied to the next argument.
-
#parse(args) ⇒ self
Incrementally parse a single string or an array of strings.
Constructor Details
#initialize(cli, tool, default_data: {}, require_exact_flag_match: false) ⇒ ArgParser
Create an argument parser for a particular tool.
297 298 299 |
# File 'core-docs/toys/arg_parser.rb', line 297 def initialize(cli, tool, default_data: {}, require_exact_flag_match: false) # Source available in the toys-core gem end |
Instance Attribute Details
#active_flag_def ⇒ Toys::Flag? (readonly)
The current flag definition whose value is still pending
349 350 351 |
# File 'core-docs/toys/arg_parser.rb', line 349 def active_flag_def @active_flag_def end |
#data ⇒ Hash (readonly)
The collected tool data from parsed arguments.
335 336 337 |
# File 'core-docs/toys/arg_parser.rb', line 335 def data @data end |
#errors ⇒ Array<Toys::ArgParser::UsageError> (readonly)
An array of parse error messages.
341 342 343 |
# File 'core-docs/toys/arg_parser.rb', line 341 def errors @errors end |
#parsed_args ⇒ Array<String> (readonly)
All command line arguments that have been parsed.
311 312 313 |
# File 'core-docs/toys/arg_parser.rb', line 311 def parsed_args @parsed_args end |
#tool ⇒ Toys::ToolDefinition (readonly)
The tool definition governing this parser.
305 306 307 |
# File 'core-docs/toys/arg_parser.rb', line 305 def tool @tool end |
#unmatched_args ⇒ Array<String> (readonly)
All args that were not matched.
329 330 331 |
# File 'core-docs/toys/arg_parser.rb', line 329 def unmatched_args @unmatched_args end |
#unmatched_flags ⇒ Array<String> (readonly)
Flags that were not matched.
323 324 325 |
# File 'core-docs/toys/arg_parser.rb', line 323 def unmatched_flags @unmatched_flags end |
#unmatched_positional ⇒ Array<String> (readonly)
Extra positional args that were not matched.
317 318 319 |
# File 'core-docs/toys/arg_parser.rb', line 317 def unmatched_positional @unmatched_positional end |
Instance Method Details
#finish ⇒ self
Complete parsing. This should be called after all arguments have been processed. It does a final check for any errors, including:
- The arguments ended with a flag that was expecting a value but wasn't provided.
- One or more required arguments were never given a value.
- One or more extra arguments were provided.
- Restrictions defined in one or more flag groups were not fulfilled.
Any errors are added to the errors array. It also fills in final values
for Context::Key::USAGE_ERRORS and Context::Key::ARGS.
After this method is called, this object is locked down, and no additional arguments may be parsed.
405 406 407 |
# File 'core-docs/toys/arg_parser.rb', line 405 def finish # Source available in the toys-core gem end |
#finished? ⇒ boolean
Determine if this parser is finished
363 364 365 |
# File 'core-docs/toys/arg_parser.rb', line 363 def finished? # Source available in the toys-core gem end |
#flags_allowed? ⇒ boolean
Whether flags are currently allowed. Returns false after -- is received.
355 356 357 |
# File 'core-docs/toys/arg_parser.rb', line 355 def flags_allowed? # Source available in the toys-core gem end |
#next_arg_def ⇒ Toys::PositionalArg?
The argument definition that will be applied to the next argument.
373 374 375 |
# File 'core-docs/toys/arg_parser.rb', line 373 def next_arg_def # Source available in the toys-core gem end |
#parse(args) ⇒ self
Incrementally parse a single string or an array of strings
383 384 385 |
# File 'core-docs/toys/arg_parser.rb', line 383 def parse(args) # Source available in the toys-core gem end |