Class: Executable::Parser

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

Overview

The Parser class does all the heavy lifting for Executable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_class) ⇒ Parser

Returns a new instance of Parser.

Parameters:



11
12
13
# File 'lib/executable/parser.rb', line 11

def initialize(cli_class)
  @cli_class = cli_class
end

Instance Attribute Details

#cli_classObject (readonly)

Returns the value of attribute cli_class.



15
16
17
# File 'lib/executable/parser.rb', line 15

def cli_class
  @cli_class
end

Instance Method Details

#find_long_option(obj, char) ⇒ Object

TODO:

Sort alphabetically?



182
183
184
185
186
187
188
# File 'lib/executable/parser.rb', line 182

def find_long_option(obj, char)
  meths = obj.methods.map{ |m| m.to_s }
  meths = meths.select do |m|
    m.start_with?(char) and (m.end_with?('=') or m.end_with?('!'))
  end
  meths.first
end

#invoke(obj, meth, argv) ⇒ Object



192
193
194
195
196
197
# File 'lib/executable/parser.rb', line 192

def invoke(obj, meth, argv)
  m = (Method === meth ? meth : obj.method(meth))
  a = []
  m.arity.abs.times{ a << argv.shift }
  m.call(*a)
end

#parse(argv = ARGV) ⇒ Object

Parse command-line.

Parameters:

  • argv (Array, String) (defaults to: ARGV)

    command-line arguments



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/executable/parser.rb', line 21

def parse(argv=ARGV)
  # duplicate to make sure ARGV stay intact.
  argv = argv.dup
  argv = parse_shellwords(argv)

  cmd, argv = parse_subcommand(argv)
  cli  = cmd.new
  args = parse_arguments(cli, argv)

  return cli, args
end

#parse_arguments(obj, argv, args = []) ⇒ Array

Parse command line options based on given object.

Parameters:

  • obj (Object)

    basis for command-line parsing

  • argv (Array, String)

    command-line arguments

  • args (Array) (defaults to: [])

    pre-seeded arguments to add to

Returns:

  • (Array)

    parsed arguments



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/executable/parser.rb', line 70

def parse_arguments(obj, argv, args=[])
  case argv
  when String
    require 'shellwords'
    argv = Shellwords.shellwords(argv)
  #else
  #  argv = argv.dup
  end

  #subc = nil
  #@args = []  #opts, i = {}, 0

  while argv.size > 0
    case arg = argv.shift
    when /=/
      parse_equal(obj, arg, argv, args)
    when /^--/
      parse_long(obj, arg, argv, args)
    when /^-/
      parse_flags(obj, arg, argv, args)
    else
      #if Executable === obj
      #  if cmd_class = obj.class.subcommands[arg]
      #    cmd  = cmd_class.new(obj)
      #    subc = cmd
      #    parse(cmd, argv, args)
      #  else
          args << arg
      #  end
      #end
    end
  end
 
  return args
end

#parse_equal(obj, opt, argv, args) ⇒ Object

Parse equal setting comman-line option.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/executable/parser.rb', line 109

def parse_equal(obj, opt, argv, args)
  if md = /^[-]*(.*?)=(.*?)$/.match(opt)
    x, v = md[1], md[2]
  else
    raise ArgumentError, "#{x}"
  end
  if obj.respond_to?("#{x}=")
    v = true  if v == 'true'   # yes or on  ?
    v = false if v == 'false'  # no  or off ?
    obj.send("#{x}=", v)
  else
    obj.__send__(:option_missing, x, v) # argv?
  end
end

#parse_flags(obj, opt, argv, args) ⇒ Object

Parse single-dash command-line option.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/executable/parser.rb', line 150

def parse_flags(obj, opt, argv, args)
  x = opt[1..-1]
  c = 0
  x.split(//).each do |k|
    if obj.respond_to?("#{k}=")
      m = obj.method("#{k}=")
      if obj.respond_to?("#{x}?")
        m.call(true)
      else
        invoke(obj, m, argv) #m.call(argv.shift)
      end
    elsif obj.respond_to?("#{k}!")
      invoke(obj, "#{k}!", argv)
    else
      long = find_long_option(obj, k)
      if long
        if long.end_with?('=') && obj.respond_to?(long.chomp('=')+'?')
          invoke(obj, long, [true])
        else
          invoke(obj, long, argv)
        end
      else
        obj.__send__(:option_missing, x, argv)
      end
    end
  end
end

#parse_long(obj, opt, argv, args) ⇒ Object

Parse double-dash command-line option.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/executable/parser.rb', line 127

def parse_long(obj, opt, argv, args)
  x = opt.sub(/^\-+/, '') # remove '--'
  if obj.respond_to?("#{x}=")
    m = obj.method("#{x}=")
    if obj.respond_to?("#{x}?")
      m.call(true)
    else
      invoke(obj, m, argv)
    end
  elsif obj.respond_to?("#{x}!")
    invoke(obj, "#{x}!", argv)
  else
    # call even if private method
    obj.__send__(:option_missing, x, argv)
  end
end

#parse_shellwords(argv) ⇒ Object

Make sure arguments are an array. If argv is a String, then parse using Shellwords module.

Parameters:

  • argv (Array, String)

    commmand-line arguments



37
38
39
40
41
42
43
# File 'lib/executable/parser.rb', line 37

def parse_shellwords(argv)
  if String === argv
    require 'shellwords'
    argv = Shellwords.shellwords(argv)
  end
  argv.to_a
end

#parse_subcommand(argv) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/executable/parser.rb', line 48

def parse_subcommand(argv)
  cmd = cli_class
  arg = argv.first

  while c = cmd.subcommands[arg]
    cmd = c
    argv.shift
    arg = argv.first
  end

  return cmd, argv
end

#subcommandsHash

Index of subcommands.

Returns:

  • (Hash)

    name mapped to subcommnd class



202
203
204
# File 'lib/executable/parser.rb', line 202

def subcommands
  @cli_class.subcommands
end