Class: Nicolus::OptParse

Inherits:
Object show all
Defined in:
lib/opt_parse.rb

Overview

Usage:

options = Nicolus::OptParse.parse(ARGV)
puts options[:input]
puts options[:output]
puts options[:inverse]
puts options[:unicode]

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Return a structure describing the options.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opt_parse.rb', line 14

def self.parse(args)
  options = {}
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: \n\tnicolus --input listes.csv --output combinaison.csv\n\tnicolus -i listes.csv -o combinaison.csv --inverse\n"

    opts.separator ""
    opts.separator "Mandatory options:"
    
    opts.on("-i", "--input [FILE]", :REQUIRED, String, "Csv File with lists of words") do |i|
      options[:input] = i
    end
    opts.on("-o", "--output [FILE]", :REQUIRED, String, "Csv File to store the result") do |o|
      options[:output] = o
    end
    
    opts.separator ""
    opts.separator "Optional options:"
    
    opts.on("-I", "--inverse", "Generate inverse combinaison") do |inv|
      options[:inverse] = inv
    end
    
    opts.on("-U", "--unicode", "Input is Unicode") do |unicode|
      options[:unicode] = unicode
    end
    
    opts.separator ""
    opts.separator "Common options:"
    
    # No argument, shows at tail.  This will print an options summary.
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  if (args.size == 0)
    # No args specify > running help
    opts.parse!(["--help"])
  else
    opts.parse!(args)
  end
  
  options
end