Class: CmdLineOptParser

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Return a structure describing the options



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/countloc.rb', line 426

def self.parse(args)
  # The options set on the command line will be collected in "options"
  # Setup the defaults here
  options = {}
  options[:recurse] = false
  options[:quiet] = false
  options[:mode] = :ruby
  options[:defaultMode] = true
  options[:csv] = false
  options[:csvFilename] = 'countloc.csv'
  options[:html] = false
  options[:htmlFilename] = 'countloc.html'
  options[:useUserFileTypes] = false
  
  begin
    OptionParser.new do |opts|
      opts.banner = usage

      opts.on('-r', '--recurse', 'Recurse into subdirectories') do
        options[:recurse] = true
      end

      opts.on('-v', '--version', 'Display version number') do 
        puts "#{File.basename($0)}, version: #{CountLOC::VERSION}"
        exit
      end

      opts.on('-q', '--quiet', "Don't write results to stdout") do 
        options[:quiet] = true
      end

      opts.on('-m', '--mode mode', 
        [:ruby, :python, :c, :cpp, :csharp, :java, :vb], 
        "Set the language mode.", 
        "All languages are used by default",
        "if the mode option is omitted.",
        "Supported modes: ",
        "  ruby, python,", 
        "  c, cpp, csharp,", 
        " java, vb") do |mode|
        options[:mode] = mode
        options[:defaultMode] = false
      end

      opts.on('--csv csvFilename', 'Generate csv file') do |csvFilename|
        options[:csvFilename] = csvFilename
        options[:csv] = true
      end

      opts.on('--html htmlFilename', 'Generate html file') do |htmlFilename|
        options[:htmlFilename] = htmlFilename
        options[:html] = true
      end

      opts.on('--file-types fileTypes', 
        "File types to be included.",
        "Default is dependant upon language mode:",
        "  ruby: *.rb",
        "  python: *.py",
        "  c: *.h, *.c",
        "  cpp: *.h, *.hpp, *.cpp, *.c, *.inl",
        "  csharp: *.cs",
        "  java: *.java",
        "  vb: *.vb",
        "Defaults are overridden using this option.") do |fileTypes|
        options[:fileTypes] = fileTypes.split()
        options[:useUserFileTypes] = true
      end

      opts.on_tail('-h', '--help', 'Display this help and exit') do
        puts opts
        exit
      end

    end.parse!(args)

  rescue
    puts "Error: " + $!.to_s
    exit
  end

  options
end

.usageObject



419
420
421
# File 'lib/countloc.rb', line 419

def self.usage 
  "Usage: #{File.basename($0)} [-h|--help] [options] <file> ... <file>"
end