Class: Ss2Json::Options

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/ss2json/options.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    :first_row => 1,
    :sheet => nil,
    :file => nil,
    :check_column => nil,
    :action => :convert,
    :orientation => :horizontal,
    :key_column => 1,
    :value_column => 2,
    :converter => {
        :show_null => false,
        :dont_convert => false,
        :ignored_values => [],
        :downcase_first_letter => true,
        :key_pattern => nil
    }

}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ss2json/options.rb', line 30

def initialize
  @options = DEFAULT_OPTIONS
  @help = nil
  super do |opts|
    @help = opts

    opts.banner =  "Usage: #{File.basename $0} -f FILENAME [options]"

    opts.separator "\nMode options:"

    opts.on("-h", "--horizontal", "Normal processing mode (DEFAULT). Can be ommited") do
      @options[:orientation] = :horizontal
    end

    opts.on("-v", "--vertical", "Process the spreadhsheet on vertical") do
      @options[:orientation] = :vertical
    end

    opts.on("-l", "--list-sheets", "Return the list of sheets") do |file|
      @options[:action] = :list
    end

    opts.separator "Common options:"

    opts.on("-f", "--file FILENAME", "Use the filename") do |file|
      File.file?(file) or die("Can't find the file #{file}.")
      @options[:file] = File.expand_path(file)
    end

    opts.on("-s", "--sheet SHEET_NAME", "Use other that the first table") do |sheet|
      @options[:sheet] = sheet
    end

    opts.separator "\nHorizontal and Vertical mode options:"

    opts.on("-r", "--first-row ROW_NUMBER", "Set the first row") do |row|
      die("Can't understand the row #{row}. Use a number") unless row =~ /\A\d*\z/
      @options[:first_row] = row.to_i
    end

    opts.separator "\nHorizontal mode options:"

    opts.on("-u", "--use-key KEY", "Generate a hash instead of an array using KEY as the hash index key") do |key|
      @options[:hash_key] = key
    end

    opts.on("--dont-remove-key", "Don't remove key from the hash") do
      @options[:dont_remove_key] = true
    end

    opts.separator "\nVertical mode options:"

    opts.on("-k", "--key-column COLUMN_NUMBER", "Column where the keys are (Default to 1)") do |column|
      die("Can't understand the column #{column}. Use a number") unless column =~ /\A\d*\z/
      @options[:key_column] = column.to_i
    end

    opts.on("-a", "--value-column COLUMN_NUMBER", "Column where the values are (Default to 2)") do |column|
      die("Can't understand the column #{column}. Use a number") unless column =~ /\A\d*\z/
      @options[:value_column] = column.to_i
    end

    opts.separator "\nData options:"

    opts.on("-i", "--ignore-value VALUE", "Ignore the fields with that value. Could be use several times") do |t|
      @options[:converter][:ignored_values] << t
    end

    opts.on("-b", "--include-blank", "Generate a json with the values included in the ignore list") do |t|
      @options[:converter][:show_null] = true
    end

    opts.on("-d", "--disable-conversion", "Disable the conversion from floats to integers") do
      @options[:converter][:dont_convert] = true
    end

    opts.on("-l", "--disable-first-letter", "Will disable the downcase of the first letter of the key") do
      @options[:converter][:downcase_first_letter] = false
    end

    opts.on("--key-pattern PATTERN", "keys must match against PATTERN defaults to '\\A[\\.\\w]+\\z'") do |pattern|
      @options[:converter][:key_pattern] = pattern
    end

    opts.separator "\nFilter options:"

    opts.on("-c", "--check-column NAME", "Only output objects wich his property NAME is not in IGNORED VALUES") do |t|
      @options[:check_column] = t
    end



    opts.separator "\n"

    opts.on_tail("-h","--help", "Show this help") do
      puts opts
      exit 0
    end

    opts.on_tail("--version", "Show the version") do
      puts "#{File.basename($0)} Version: #{Ss2Json::VERSION}"
      exit 0
    end

  end.parse!

  die("You need to at least specify a file") if @options[:file].nil?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/ss2json/options.rb', line 6

def options
  @options
end

Class Method Details

.parse!Object



26
27
28
# File 'lib/ss2json/options.rb', line 26

def self.parse!
  new.options
end

Instance Method Details

#die(msg) ⇒ Object



139
140
141
142
143
# File 'lib/ss2json/options.rb', line 139

def die(msg)
  $stderr.puts msg
  $stderr.puts @help
  exit -1
end