Module: BaseChip::Cli::InstanceMethods

Defined in:
lib/base_chip/cli.rb

Instance Method Summary collapse

Instance Method Details

#cliObject



283
# File 'lib/base_chip/cli.rb', line 283

def cli     ; self.class.cli     end

#optionsObject



284
# File 'lib/base_chip/cli.rb', line 284

def options ; self.class.options end


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/base_chip/cli.rb', line 346

def print_table(table, delimeter="\t")
  column = 0
  table2 = []
  sizes  = []

  # determine size
  table.each do |row|
    column = 0
    table2 << row.map do |c|
      tmp           = c.to_s
      tmpsize       = tmp.size
      sizes[column] = tmpsize if sizes[column].nil? or tmpsize > sizes[column]
      column += 1
      tmp
    end
  end

  # map to sized columns
  table2.each do |row|
    column = 0
    puts (row.map! { |c| "%-#{sizes[(column += 1) - 1]}s" % c }).join(" => ").strip
  end
end

#run_cli(arguments = ARGV) ⇒ Object



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
345
# File 'lib/base_chip/cli.rb', line 290

def run_cli(arguments = ARGV)
  run_cli_no_command if arguments.size == 0
  pass_through              = nil
  options. append_arguments = nil
  options.replace_arguments = nil
  arguments.keep_if do |a|
    case a 
    when '--'  ; pass_through = (options. append_arguments ||= []); false
    when '---' ; pass_through = (options.replace_arguments ||= []); false
    else 
      if pass_through
        pass_through << a
        false
      else
        true
      end
    end
  end
  cli.options.each do |o,h|
    if h[:value]
      options.send("#{h[:name]}=",h[:value])
    end
  end
  cli.mine_options(options,arguments)
  arguments.size.times do |i|
    case a = arguments[i].to_sym
    when *(cli.sub_commands.keys) 
      arguments.delete_at i
      cli.sub_commands[a].new.run_cli(arguments)
      exit
    when *(cli.tasks.keys) 
      arguments.delete_at i
      task = cli.tasks[a]
      task.mine_options(options,arguments,true)
      if task.required_argument_size > arguments.size
        help task.name
        fault "\n#{task.name} is missing required arguments"
      end
      send a, *arguments
      break
    else
      if a[0] == '-'
        fault("Could not determine what to run from \"#{arguments.join(' ')}\".  Please consider placing dash options at the end of the command line.")
      end
      task = cli.default_task or fault("Could not determine what to run from \"#{arguments.join(' ')}\".")
      task.mine_options(options,arguments,true)
      if task.required_argument_size > arguments.size
        puts "\n#{task.name} is missing required arguments"
        help task.name
        exit 1
      end
      send task.name, *arguments
      break
    end
  end
end

#run_cli_no_commandObject



285
286
287
288
289
# File 'lib/base_chip/cli.rb', line 285

def run_cli_no_command
  puts "\nPlease specify a sub command" + ( cli.name ? " for #{cli.name}" : '' )
  help
  exit 1
end