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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/shell/shell.rb', line 101

def self.get_arguments argv
  @arguments = []
  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

ARGV has a command, options and arguments. The design is:

$ bin_file command argument1 argument2 -option1 -option2

get_options() and get_arguments() return Array. get_command() returns String.



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

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
  }
  return false if command.empty?
  command
end

.get_options(argv) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/shell/shell.rb', line 77

def self.get_options argv
  return [] if argv.nil?
  @options = []
  @sanitized_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
  }
  unless @options.empty?
    @options.each { |e|
      next if @sanitized_options.include?(e)
      @sanitized_options << e
    }
  end
  
  @sanitized_options
end

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



118
119
120
121
# File 'lib/shell/shell.rb', line 118

def self.is_option option, argv = []
  argv_options = self.get_options argv
  argv_options.include?(option)
end