Class: Shell::Parser

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

Class Method Summary collapse

Class Method Details

.get_arguments(argv) ⇒ Object

get arguments. arguments are anything written besides the command and options. in ‘push today –list’, ‘today’ is the argument



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/shell/shell.rb', line 115

def self.get_arguments argv
  @arguments = Array.new
  i = 0
  argv.each {
    |e|
    
    i+= 1
    next if i == 1
    
    e_length = e.length
    if e[0,2] != "--" and e[0,1] != "-"
      
      @arguments.push e[0,e_length]
    end
    
  }
  @arguments
end

.get_command(argv = []) ⇒ Object

defines the command of the application (e.g. ‘push’ in uplift push)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shell/shell.rb', line 59

def self.get_command argv = []
  command = String.new
  
  argv.each {
    |e|
    e_length = e.length
    if (e[0,2] != "--" and e[0,1] != "-") then
        command = e
        break
    end
  }
  
  if command.empty? then
    command = ""
  end
  
  command
  
end

.get_option_value(option, argv = []) ⇒ Object

get_options



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/shell/shell.rb', line 96

def self.get_option_value option, argv = []
  next_item, option_value = false
  argv.each { |e|
    if next_item
      option_value = e
      break
    end
    e_length = e.length
    if e[0,2] == "--" 
      next_item = true if e[2,e_length] == option
    elsif e[0,1] == "-"
      next_item = true if e[1,e_length] == option
    end
  }
  option_value || ''
end

.get_options(argv = []) ⇒ Object

get only options in ARGV (arguments starting with - or –)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shell/shell.rb', line 80

def self.get_options argv = []
  return [] if argv.empty?
  @options = []
  
  argv.each {
    |e|
    e_length = e.length
    if e[0,2] == "--" 
      @options.push e[2,e_length]
    elsif e[0,1] == "-"
        @options.push e[1,e_length]
    end
  }
  @options
end

.is_option(option, argv = Array.new) ⇒ Object

get_options



134
135
136
137
# File 'lib/shell/shell.rb', line 134

def self.is_option option, argv = Array.new
  argv_options = self.get_options argv
  argv_options.include?(option)
end