Class: Clin::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/clin/argument.rb

Overview

Command line positional argument(not option)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argument) ⇒ Argument

Returns a new instance of Argument.



11
12
13
14
15
16
17
18
19
# File 'lib/clin/argument.rb', line 11

def initialize(argument)
  @original = argument
  @optional = false
  @multiple = false
  @variable = false
  argument = check_optional(argument)
  argument = check_multiple(argument)
  @name = check_variable(argument)
end

Instance Attribute Details

#multipleObject

Returns the value of attribute multiple.



7
8
9
# File 'lib/clin/argument.rb', line 7

def multiple
  @multiple
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/clin/argument.rb', line 9

def name
  @name
end

#optionalObject

Returns the value of attribute optional.



6
7
8
# File 'lib/clin/argument.rb', line 6

def optional
  @optional
end

#originalObject

Returns the value of attribute original.



5
6
7
# File 'lib/clin/argument.rb', line 5

def original
  @original
end

#variableObject

Returns the value of attribute variable.



8
9
10
# File 'lib/clin/argument.rb', line 8

def variable
  @variable
end

Instance Method Details

#check_multiple(argument) ⇒ Object

Check if the argument is multiple(i.e arg…)



31
32
33
34
35
36
37
# File 'lib/clin/argument.rb', line 31

def check_multiple(argument)
  if argument.end_with? '...'
    @multiple = true
    return argument[0...-3]
  end
  argument
end

#check_optional(argument) ⇒ Object

Check if the argument is optional(i.e [arg])



22
23
24
25
26
27
28
# File 'lib/clin/argument.rb', line 22

def check_optional(argument)
  if beck_between(argument, '[', ']')
    @optional = true
    return argument[1...-1]
  end
  argument
end

#check_variable(argument) ⇒ Object

Check if the argument is variable(i.e <arg>)



40
41
42
43
44
45
46
# File 'lib/clin/argument.rb', line 40

def check_variable(argument)
  if beck_between(argument, '<', '>')
    @variable = true
    return argument[1...-1]
  end
  argument
end

#parse(argv) ⇒ Object

Given a list of arguments extract the list of arguments that are matched



49
50
51
52
53
54
55
56
57
58
# File 'lib/clin/argument.rb', line 49

def parse(argv)
  return handle_empty if argv.empty?
  if @multiple
    ensure_fixed(argv) unless @variable
    [argv, []]
  else
    ensure_fixed(argv[0]) unless @variable
    [argv[0], argv[1..-1]]
  end
end