Class: Command::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/command-set/arguments.rb

Overview

An Argument has a name and a value. They’re used to to validate input, provide input prompts (like tab-completion or pop-up menus.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Argument



14
15
16
17
# File 'lib/command-set/arguments.rb', line 14

def initialize(name)
  @name = name.to_s
  @value = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/command-set/arguments.rb', line 19

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



19
20
21
# File 'lib/command-set/arguments.rb', line 19

def value
  @value
end

Class Method Details

.register(shorthand, type = nil) ⇒ Object

Used for new Argument subclasses to register the types they can be based on, and the explicit names of the arguments



10
11
12
# File 'lib/command-set/arguments.rb', line 10

def self.register(shorthand, type=nil)
  DSL::Argument::register_argument(self, shorthand, type)
end

Instance Method Details

#check_present(keys) ⇒ Object



56
57
58
59
60
# File 'lib/command-set/arguments.rb', line 56

def check_present(keys)
  unless keys.include?(@name)
    raise OutOfArgumentsException, "Missing argument: #@name!"
  end
end

#complete(prefix, subject) ⇒ Object

Provides a list of completion options based on a string prefix and the subject The completion should be an array of completion options. If the completions have a common prefix, completion will enter it for the user. As a clever trick for providing hints:

[ "This is a hint", "" ]

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/command-set/arguments.rb', line 25

def complete(prefix, subject)
  raise NotImplementedError, "complete not implemented in #{self.class.name}"
end

#consume(subject, arguments) ⇒ Object

Pulls strings from an ordered list of inputs and provides the parsed data to the host command Returns the parsed data or raises ArgumentInvalidException



37
38
39
40
41
42
43
# File 'lib/command-set/arguments.rb', line 37

def consume(subject, arguments)
  term = arguments.shift
  unless validate(term, subject)
  raise ArgumentInvalidException, {@name => term}
  end
  return {@name => parse(subject, term)}
end

#consume_hash(subject, hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/command-set/arguments.rb', line 45

def consume_hash(subject, hash)
  unless((term = hash[@name]).nil?)
    if validate(term, subject)
      return {@name => parse(subject, term)}
    else
      raise ArgumentInvalidException, {@name => term}
    end
  end
  return {}
end

#match_terms(subject, terms, arguments) ⇒ Object

Used for completion, to skip along terms and arguments until we’re at a place where completion begins. Both the terms and arguments arrays are modified by this method, so a number of clever tricks can be played to make completion work.



65
66
67
68
# File 'lib/command-set/arguments.rb', line 65

def match_terms(subject, terms, arguments)
  arguments.shift
  validate(terms.shift, subject)
end

#omittable?Boolean

Used in completion to recognize that some arguments can be skipped



71
72
73
# File 'lib/command-set/arguments.rb', line 71

def omittable?
  false
end

#parse(subject, term) ⇒ Object

Returns the parsed data equivalent of the string input



81
82
83
# File 'lib/command-set/arguments.rb', line 81

def parse(subject, term)
  return term
end

#required?Boolean

Used in validation to require some arguments, and allow others to be optional



76
77
78
# File 'lib/command-set/arguments.rb', line 76

def required?
  true
end

#validate(term, subject) ⇒ Object

Validates the input string against the type of the argument. Returns true if the input is valid, or else false

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/command-set/arguments.rb', line 31

def validate(term, subject)
  raise NotImplementedError, "validate not implemented in #{self.class.name}"
end