Class: Cmdline::CmdLine

Inherits:
Struct
  • Object
show all
Defined in:
lib/cmdline.rb,
lib/cmdline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCmdLine

Returns a new instance of CmdLine.



91
92
93
94
95
96
97
98
99
# File 'lib/cmdline.rb', line 91

def initialize(*)
  super
  self.options ||= {}
  self.commands ||= {}
  self.command ||= ""
  self.arguments ||= []
  self.command_arguments ||= []
  self.program_name ||= ""
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



80
81
82
# File 'lib/cmdline.rb', line 80

def arguments
  @arguments
end

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



80
81
82
# File 'lib/cmdline.rb', line 80

def command
  @command
end

#command_argumentsObject

Returns the value of attribute command_arguments

Returns:

  • (Object)

    the current value of command_arguments



80
81
82
# File 'lib/cmdline.rb', line 80

def command_arguments
  @command_arguments
end

#commandsObject

Returns the value of attribute commands

Returns:

  • (Object)

    the current value of commands



80
81
82
# File 'lib/cmdline.rb', line 80

def commands
  @commands
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



80
81
82
# File 'lib/cmdline.rb', line 80

def options
  @options
end

#program_nameObject

Returns the value of attribute program_name

Returns:

  • (Object)

    the current value of program_name



80
81
82
# File 'lib/cmdline.rb', line 80

def program_name
  @program_name
end

Instance Method Details

#add_argument(name, description) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/cmdline.rb', line 131

def add_argument(name, description)
  argument = Argument.new(
    name: name.to_s,
    description: description.to_s
  )

  addarg(argument)
end

#add_command(name, description) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cmdline.rb', line 150

def add_command(name, description)
  if self.arguments.size.zero?
    add_argument("command", "the command to execute")
  elsif self.arguments.first.name != "command"
    raise(ArgumentError, "cannot have both arguments and commands")
  end

  cmd = Command.new(
    name: name.to_s,
    description: description.to_s
  )

  self.commands[cmd.name] = cmd
end

#add_flag(short, long, description) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/cmdline.rb', line 101

def add_flag(short, long, description)
  option = Option.new(
    short_name: short.to_s,
    long_name: long.to_s,
    value_string: "",
    description: description.to_s
  )

  addopt(option)
end

#add_option(short, long, value, description) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/cmdline.rb', line 112

def add_option(short, long, value, description)
  option = Option.new(
    short_name: short.to_s,
    long_name: long.to_s,
    value_string: value.to_s,
    description: description.to_s
  )

  addopt(option)
end

#add_trailing_arguments(name, description) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/cmdline.rb', line 140

def add_trailing_arguments(name, description)
  argument = Argument.new(
    name: name.to_s,
    description: description.to_s,
    trailing: true
  )

  addarg(argument)
end

#argument_value(name) ⇒ Object

Raises:

  • (ArgumentError)


360
361
362
363
364
365
366
367
# File 'lib/cmdline.rb', line 360

def argument_value(name)
  self.arguments.each do |arg|
    if arg.name == name
      return arg.value
    end
  end
  raise(ArgumentError, "unknown argument")
end

#command_arguments_valuesObject

Raises:

  • (RuntimeError)


381
382
383
384
# File 'lib/cmdline.rb', line 381

def command_arguments_values
  raise(RuntimeError, "no command defined") if self.commands.empty?
  self.command_arguments
end

#command_nameObject

Raises:

  • (RuntimeError)


376
377
378
379
# File 'lib/cmdline.rb', line 376

def command_name
  raise(RuntimeError, "no command defined") if self.commands.empty?
  self.command
end

#command_name_and_argumentsObject

Raises:

  • (RuntimeError)


386
387
388
389
# File 'lib/cmdline.rb', line 386

def command_name_and_arguments
  raise(RuntimeError, "no command defined") if self.commands.empty?
  [self.command, *self.command_arguments]
end

#die(format, *args) ⇒ Object



165
166
167
168
169
# File 'lib/cmdline.rb', line 165

def die(format, *args)
  msg = sprintf(format, *args)
  STDERR.puts("error: #{msg}")
  exit(1)
end

#is_option_set(name) ⇒ Object

Raises:

  • (ArgumentError)


346
347
348
349
350
# File 'lib/cmdline.rb', line 346

def is_option_set(name)
  opt = self.options[name]
  raise(ArgumentError, "unknown option") if opt.nil?
  opt.set
end

#option_value(name) ⇒ Object

Raises:

  • (ArgumentError)


352
353
354
355
356
357
358
# File 'lib/cmdline.rb', line 352

def option_value(name)
  opt = self.options[name]
  raise(ArgumentError, "unknown option") if opt.nil?

  return opt.value if opt.set
  opt.default
end

#parse(args) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cmdline.rb', line 171

def parse(args)
  die("empty argument array") if args.size == 0

  self.program_name = args.shift

  while args.size > 0
    arg = args.first

    if arg == "--"
      args.shift
      break
    end

    is_short = arg.size == 2 && arg[0] == "-" && arg[1] != "-"
    is_long = arg.size > 2 && arg[0,2] == "--"

    if is_short || is_long
      key = if is_short
              arg[1,2]
            else
              arg[2..]
            end

      opt = self.options[key]
      die("unknown option \"%s\"", key) if opt.nil?

      opt.set = true

      if opt.value_string.empty?
        args = args[1..]
      else
        die("missing value for option \"%s\"", key) if args.size < 2
        opt.value = args[1]
        args = args[2..]
      end
    else
      break
    end
  end

  if self.arguments.size > 0 && !is_option_set("help")
    last = self.arguments.last

    min = self.arguments.size
    min -= 1 if last.trailing

    die("missing argument(s)") if args.size < min

    min.times do |i|
      self.arguments[i].value = args[i]
    end
    args = args[min..]

    if last.trailing
      last.trailing_values = args
      args = args[args.size..]
    end
  end

  if self.commands.size > 0
    self.command = self.arguments.first.value
    self.command_arguments = args
  end

  if !is_option_set("help")
    if self.commands.size > 0
      cmd = self.commands[self.command]
      if cmd.nil?
        die("unknown command \"%s\"", self.command)
      end
    elsif args.size > 0
      die("invalid extra argument(s)")
    end
  end

  if is_option_set("help")
    print_usage
    exit(0)
  end
end


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/cmdline.rb', line 252

def print_usage
  usage = sprintf("Usage: %s OPTIONS", self.program_name)
  if self.arguments.size > 0
    self.arguments.each do |arg|
      if arg.trailing
        usage << sprintf(" [<%s> ...]", arg.name)
      else
        usage << sprintf(" <%s>", arg.name)
      end
    end
  end

  usage << "\n\n"

  opt_strs = {}
  max_width = 0

  self.options.each do |_, opt|
    next if opt_strs[opt]

    buf = ""

    if opt.short_name != ""
      buf << sprintf("-%s", opt.short_name)
    end

    if opt.long_name != ""
      if opt.short_name != ""
        buf << ", "
      end

      buf << sprintf("--%s", opt.long_name)
    end

    if opt.value_string != ""
      buf << sprintf(" <%s>", opt.value_string)
    end

    opt_strs[opt] = buf

    if buf.size > max_width
      max_width = buf.size
    end
  end

  if self.commands.size > 0
    self.commands.each do |name, _|
      max_width = name.size if name.size > max_width
    end
  elsif self.arguments.size > 0
    self.arguments.each do |arg|
      max_width = arg.name.size if arg.name.size > max_width
    end
  end

  # Print options
  usage << "OPTIONS\n\n"

  opts = []
  opt_strs.each do |opt, _|
    opts << opt
  end

  # TODO: sort options

  opts.each do |opt|
    usage << sprintf("%-*s  %s", max_width, opt_strs[opt], opt.description)
    usage << sprintf(" (default: %s)", opt.default) unless opt.default.empty?
    usage << "\n"
  end

  if self.commands.size > 0
    usage << "\nCOMMANDS\n\n"
    names = []
    self.commands.each do |name, _|
      names << name
    end
    names.sort!

    names.each do |name|
      cmd = self.commands[name]
      usage << sprintf("%-*s  %s\n", max_width, cmd.name, cmd.description)
    end
  elsif self.arguments.size > 0
    usage << "\nARGUMENTS\n\n"

    self.arguments.each do |arg|
      usage << sprintf("%-*s  %s\n", max_width, arg.name, arg.description)
    end
  end

  printf(usage)
end

#set_option_default(name, value) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
# File 'lib/cmdline.rb', line 123

def set_option_default(name, value)
  option = self.options[name]
  raise(ArgumentError, "unknown option") if option.nil?
  raise(ArgumentError, "flags cannot have a default value") if option.value_string.empty?

  option.default = value
end

#trailing_arguments_values(name) ⇒ Object

Raises:

  • (ArgumentError)


369
370
371
372
373
374
# File 'lib/cmdline.rb', line 369

def trailing_arguments_values(name)
  raise(ArgumentError, "empty argument array") if self.arguments.empty?
  last = self.arguments.last
  raise(ArgumentError, "no trailing arguments") unless last.trailing
  last.trailing_values
end