picandocodigo/ricojson

View on GitHub
bin/ricojson

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'ricojson'
require 'clap'

input_file    = nil
open_result   = false
help_message  = <<EOS
Usage: ricojson [-oh] [-f file]

Synopsis:

  ricojson is a command line utility that will format a given JSON input
  so that it can be read by humans.

  The default action for ricojson consists on reading from STDIN and is
  meant to be used by piping other commnands results into it, however you
  can also specify a file to read from.

Flags:

  -f file       Read JSON from specified file.
  -o            Opens the resulting formatted JSON in your default
                text editor.
  -h            Prints this message.
EOS

Clap.run ARGV,
  '-f' => lambda { |file| input_file = file },
  '-o' => lambda { open_result = true },
  '-h' => lambda { abort(help_message) }

if input_file.nil?
  RicoJSON.read_string(STDIN.read, open_result)
else
  RicoJSON.read_file(input_file, open_result)
end