Class: Tilia::VObject::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/tilia/v_object/cli.rb

Overview

This is the CLI interface for sabre-vobject.

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



576
577
578
579
580
# File 'lib/tilia/v_object/cli.rb', line 576

def initialize
  @quiet = false
  @show_help = false
  @forgiving = false
end

Instance Method Details

#main(argv) ⇒ Fixnum

Main function.

Returns:

  • (Fixnum)


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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tilia/v_object/cli.rb', line 64

def main(argv)
  # @codeCoverageIgnoreStart
  # We cannot easily test this, so we'll skip it. Pretty basic anyway.

  @stderr = STDERR unless @stderr
  @stdout = STDOUT unless @stdout
  @stdin = STDIN unless @stdin

  begin
    (options, positional) = parse_arguments(argv)

    @quiet = true if options['q']
    log(colorize('green', 'tilia/vobject ') + colorize('yellow', Version::VERSION))

    options.each do |name, value|
      case name
      when 'q'
      when 'h', 'help'
        show_help
        return 0
      when 'format'
        formats = %w(jcard jcal vcard21 vcard30 vcard40 icalendar20 json mimedir icalendar vcard)
        fail ArgumentError, "Unkown format: #{value}" unless formats.include?(value)
        @format = value
      when 'pretty'
        @pretty = true
      when 'forgiving'
        @forgiving = true
      when 'inputformat'
        case value
        # json formats
        when 'jcard', 'jcal', 'json'
          @input_format = 'json'
        # mimedir formats
        when 'mimedir', 'icalendar', 'vcard', 'vcard21', 'vcard30', 'vcard40', 'icalendar20'
          @input_format = 'mimedir'
        else
          fail ArgumentError, "Unknown format: #{value}"
        end
      end
    end

    if positional.empty?
      show_help
      return 1
    end

    if positional.size == 1
      fail ArgumentError, 'Inputfile is a required argument'
    end

    fail ArgumentError, 'Too many arguments' if positional.size > 3

    unless %w(validate repair convert color).include?(positional[0])
      fail ArgumentError, "Uknown command: #{positional[0]}"
    end
  rescue ArgumentError => e
    show_help
    log("Error: #{e}", 'red')
    return 1
  end

  command = positional[0]

  @input_path = positional[1]
  @output_path = positional[2] ? positional[2] : '-'

  @stdout = File.open(@output_path, 'w') if @output_path != '-'

  unless @input_format
    if @input_path[-5..-1] == '.json'
      @input_format = 'json'
    else
      @input_format = 'mimedir'
    end
  end

  unless @format
    if @output_path[-5..-1] == '.json'
      @format = 'json'
    else
      @format = 'mimedir'
    end
  end

  real_code = 0

  begin
    loop do
      input = read_input
      break unless input

      return_code = send(command, input)
      real_code = return_code unless return_code == 0
    end
  rescue EofException
    # end of file
    return real_code
  rescue StandardError => e
    log("Error: #{e}", 'red')
    return 2
  end

  real_code
end