Class: Kwalify::Main

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

Overview

ex.

command = File.basename($0)
begin
  main = Kwalify::Main.new(command)
  s = main.execute
  print s if s
rescue Kwalify::CommandOptionError => ex
  $stderr.puts "ERROR: #{ex.message}"
  exit 1
rescue Kwalify::KwalifyError => ex
  $stderr.puts "ERROR: #{ex.message}"
  exit 1
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil) ⇒ Main

Returns a new instance of Main.



45
46
47
48
49
50
51
52
53
54
# File 'lib/kwalify/main.rb', line 45

def initialize(command=nil)
  @command = command || File.basename($0)
  @options = {}
  @properties   = {}
  @template_path  = []
  $:.each do |path|
    tpath = "#{path}/kwalify/templates"
    @template_path << tpath if test(?d, tpath)
  end
end

Class Method Details

.main(command, argv = ARGV) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kwalify/main.rb', line 121

def self.main(command, argv=ARGV)
  begin
    main = Kwalify::Main.new(command)
    s = main.execute(argv)
    print s if s
  rescue Kwalify::CommandOptionError => ex
    raise ex if main.debug?
    $stderr.puts ex.message
    exit 1
  rescue Kwalify::KwalifyError => ex
    raise ex if main.debug?
    $stderr.puts "ERROR: #{ex.to_s}"
    exit 1
  #rescue => ex
  #  if main.debug?
  #    raise ex
  #  else
  #    $stderr.puts ex.message
  #    exit 1
  #  end
  end
end

Instance Method Details

#_inspectObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kwalify/main.rb', line 62

def _inspect()
  sb = []
  sb <<   "command: #{@command}\n"
  sb <<   "options:\n"
  @options.keys.sort {|k1,k2| k1.to_s<=>k2.to_s }.each do |key|
    sb << "  - #{key}: #{@options[key]}\n"
  end
  sb <<   "properties:\n"
  @properties.keys.sort_by {|k| k.to_s}.each do |key|
    sb << "  - #{key}: #{@properties[key]}\n"
  end
  #sb <<   "template_path:\n"
  #@template_path.each do |path|
  #  sb << "  - #{path}\n"
  #end
  return sb.join
end

#debug?Boolean

Returns:



57
58
59
# File 'lib/kwalify/main.rb', line 57

def debug?
  @options[:debug]
end

#execute(argv = ARGV) ⇒ Object



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
# File 'lib/kwalify/main.rb', line 81

def execute(argv=ARGV)
  ## parse command-line options
  filenames = _parse_argv(argv)

  ## help or version
  if @options[:help] || @options[:version]
    action = @options[:action]
    s = ''
    s << _version() << "\n"           if @options[:version]
    s << _usage()                     if @options[:help] && !action
    s << _describe_properties(action) if @options[:help] && action
    puts s
    return
  end

  # validation
  if @options[:meta2]
    validate_schemafiles2(filenames)
  elsif @options[:meta]
    validate_schemafiles(filenames)
  elsif @options[:action]
    unless @options[:schema]
      #* key=:command_option_actionnoschema  msg="schema filename is not specified."
      raise option_error(:command_option_actionnoschema, @options[:action])
    end
    perform_action(@options[:action], @options[:schema])
  elsif @options[:schema]
    if @options[:debug]
      inspect_schema(@options[:schema])
    else
      validate_files(filenames, @options[:schema])
    end
  else
    #* key=:command_option_noaction  msg="command-line option '-f' or '-m' required."
    raise option_error(:command_option_noaction, @command)
  end
  return
end