Class: PrettifyJson::CLI

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

Class Method Summary collapse

Class Method Details

.add_file_option(opts, options) ⇒ Object



59
60
61
62
63
# File 'lib/prettify_json.rb', line 59

def self.add_file_option(opts, options)
  opts.on('-f', '--file FILE', 'Input JSON file') do |file|
    options[:file] = file
  end
end

.add_help_option(opts) ⇒ Object



71
72
73
74
75
76
# File 'lib/prettify_json.rb', line 71

def self.add_help_option(opts)
  opts.on('-h', '--help', 'Show this help message') do
    puts opts
    exit
  end
end

.add_string_option(opts, options) ⇒ Object



65
66
67
68
69
# File 'lib/prettify_json.rb', line 65

def self.add_string_option(opts, options)
  opts.on('-s', '--string JSON', 'Input JSON string') do |json_string|
    options[:string] = json_string
  end
end

.display_helpObject



96
97
98
99
# File 'lib/prettify_json.rb', line 96

def self.display_help
  puts help_message
  exit 0
end

.help_messageObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/prettify_json.rb', line 101

def self.help_message
  "    Usage:\n      prettify_json [options]\n\n    [Options]\n      -f, --file FILE          Input JSON file\n      -s, --string JSON        Input JSON string\n      -                        Read multiple lines from standard input (no single quotes needed)\n\n    You may specify a file path or a JSON string to be parsed and formatted.\n    IMPORTANT: make sure to surround your JSON string with single quotes to prevent the shell from splitting it by its own.\n  HELP\nend\n"

.main(argv = ARGV) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/prettify_json.rb', line 116

def self.main(argv = ARGV)
  options = parse_options(argv)

  display_help if options.empty? && argv.empty?

  input = read_input(options)
  pretty_print_json(input)
end

.option_parser(options) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/prettify_json.rb', line 50

def self.option_parser(options)
  OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options]"
    add_file_option(opts, options)
    add_string_option(opts, options)
    add_help_option(opts)
  end
end

.parse_options(argv = ARGV) ⇒ Object



43
44
45
46
47
48
# File 'lib/prettify_json.rb', line 43

def self.parse_options(argv = ARGV)
  options = {}
  opts = option_parser(options)
  opts.parse!(argv)
  options
end

.pretty_print_json(input) ⇒ Object



90
91
92
93
94
# File 'lib/prettify_json.rb', line 90

def self.pretty_print_json(input)
  puts PrettifyJson::JsonPrettifier.new(input).pretty_print
rescue JSON::ParserError => e
  puts "Invalid JSON: #{e.message}"
end

.read_input(options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/prettify_json.rb', line 78

def self.read_input(options)
  if options[:file]
    File.read(options[:file])
  elsif options[:string]
    options[:string]
  elsif ARGV.include?('-')
    $stdin.read
  else
    display_help
  end
end