Class: JSON::SchemaGeneratorCLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ SchemaGeneratorCLI

Returns a new instance of SchemaGeneratorCLI.



6
7
8
# File 'lib/json/schema_generator_cli.rb', line 6

def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)
  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end

Instance Method Details

#execute!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/json/schema_generator_cli.rb', line 10

def execute!
  default_version = 'draft4'
  supported_versions = ['draft4']

  options = {
    :schema_version => default_version,
    :defaults => false
  }



  OptionParser.new do |opts|
    opts.on("--defaults", "Record default values in the generated schema") { options[:defaults] = true }
    opts.on("--schema-version draft4", [:draft4],
      "Version of json-schema to generate (#{supported_versions.join ', '}).  Default: #{default_version}") do |schema_version|
        options[:schema_version] = schema_version
      end
    opts.on("--allow-null", "Includes 'null' as an allow type for all properties in the schema") { options[:allow_null] = true }
    opts.parse!
  end

  file = ARGV.shift
  schema = JSON.parse(JSON::SchemaGenerator.generate file, File.read(file), options)
  @stdout.puts JSON.pretty_generate schema
  @kernel.exit(0)
end