Class: TermUtils::AP::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/ap/parser.rb

Overview

Represents the argument list parser.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Constructs a new Parser.



24
25
# File 'lib/term_utils/ap/parser.rb', line 24

def initialize
end

Class Method Details

.eval_article_min_occurs(articles) ⇒ Integer

Evaluates the added number of min occurs of a given array of articles.

Parameters:

Returns:

  • (Integer)


64
65
66
# File 'lib/term_utils/ap/parser.rb', line 64

def self.eval_article_min_occurs(articles)
  articles.inject(0) { |acc, a| acc + a.min_occurs }
end

.match_shortcut_flag(shortcut_flags, arg) ⇒ Array<String>?

Tests whether a given sample matches a shortcut flag.

Parameters:

  • shortcut_flags (Hash<String, Flag>)
  • arg (String)

Returns:

  • (Array<String>, nil)

    Replacements on success, nil otherwise.



51
52
53
54
55
56
57
58
59
# File 'lib/term_utils/ap/parser.rb', line 51

def self.match_shortcut_flag(shortcut_flags, arg)
  shortcut_flags.each do |label, flag|
    next unless arg.start_with? label

    return [flag.label, arg[label.length..-1]]
  end

  nil
end

Instance Method Details

#parse_arguments(syntax, arguments, opts = {}, &block) ⇒ Result

Parses a given list of arguments.

Parameters:

  • syntax (Syntax)
  • arguments (Array<String>)
  • opts (Hash) (defaults to: {})

Returns:

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/term_utils/ap/parser.rb', line 34

def parse_arguments(syntax, arguments, opts = {}, &block)
  syntax = syntax.dup
  syntax.finalize!
  arguments = arguments.dup
  res = TermUtils::AP::Result.new(syntax)
  catch :done do
    parse0(res, syntax, arguments, opts)
  end
  res.remaining_arguments = arguments
  res.walk(&block) if block
  res
end