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, basis = nil) ⇒ Argument

Returns a new instance of Argument.



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

def initialize(name, basis=nil)
  @name = name.to_s
  @basis = basis
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

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

#basis(subject = nil) ⇒ Object



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

def basis(subject = nil)
  return @basis unless DSL::Argument::SubjectDeferral === @basis
  return @basis.realize(subject)
end

#check_present(keys) ⇒ Object



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

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

#complete(original_terms, 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)


42
43
44
# File 'lib/command-set/arguments.rb', line 42

def complete(original_terms, 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



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

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

#consume_hash(subject, hash) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/command-set/arguments.rb', line 63

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.



84
85
86
87
# File 'lib/command-set/arguments.rb', line 84

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

#namesObject



21
22
23
# File 'lib/command-set/arguments.rb', line 21

def names
  return name()
end

#omittable?Boolean

Used in completion to recognize that some arguments can be skipped

Returns:

  • (Boolean)


90
91
92
# File 'lib/command-set/arguments.rb', line 90

def omittable?
  false
end

#parse(subject, term) ⇒ Object

Returns the parsed data equivalent of the string input



101
102
103
# File 'lib/command-set/arguments.rb', line 101

def parse(subject, term)
  return term
end

#required?Boolean

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

Returns:

  • (Boolean)


96
97
98
# File 'lib/command-set/arguments.rb', line 96

def required?
  true
end

#subject_requirementsObject



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

def subject_requirements
  if DSL::Argument::SubjectDeferral === @basis
    return @basis.subject_requirements
  else
    return []
  end
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)


48
49
50
# File 'lib/command-set/arguments.rb', line 48

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