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



186
187
188
189
190
191
192
# File 'lib/executable/parser.rb', line 186

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



196
197
198
199
200
201
# File 'lib/executable/parser.rb', line 196

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
32
33
34
35
36
# 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

  if argv.first == "_"
    cli = Completion.new(cli.class)
  else
    args = parse_arguments(cli, argv)
  end

  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



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
105
106
107
108
109
# File 'lib/executable/parser.rb', line 75

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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/executable/parser.rb', line 114

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.



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

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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/executable/parser.rb', line 132

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



42
43
44
45
46
47
48
# File 'lib/executable/parser.rb', line 42

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

#parse_subcommand(argv) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/executable/parser.rb', line 53

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



206
207
208
# File 'lib/executable/parser.rb', line 206

def subcommands
  @cli_class.subcommands
end