Class: PrettifyJson::CLI
- Inherits:
-
Object
- Object
- PrettifyJson::CLI
- Defined in:
- lib/prettify_json.rb
Class Method Summary collapse
- .add_file_option(opts, options) ⇒ Object
- .add_help_option(opts) ⇒ Object
- .add_string_option(opts, options) ⇒ Object
- .display_help ⇒ Object
- .help_message ⇒ Object
- .main(argv = ARGV) ⇒ Object
- .option_parser(options) ⇒ Object
- .parse_options(argv = ARGV) ⇒ Object
- .pretty_print_json(input) ⇒ Object
- .read_input(options) ⇒ Object
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, ) opts.on('-f', '--file FILE', 'Input JSON file') do |file| [: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, ) opts.on('-s', '--string JSON', 'Input JSON string') do |json_string| [:string] = json_string end end |
.display_help ⇒ Object
96 97 98 99 |
# File 'lib/prettify_json.rb', line 96 def self.display_help puts exit 0 end |
.help_message ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/prettify_json.rb', line 101 def self. " 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) = (argv) display_help if .empty? && argv.empty? input = read_input() 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() OptionParser.new do |opts| opts. = "Usage: #{File.basename($PROGRAM_NAME)} [options]" add_file_option(opts, ) add_string_option(opts, ) 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.(argv = ARGV) = {} opts = option_parser() opts.parse!(argv) 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() if [:file] File.read([:file]) elsif [:string] [:string] elsif ARGV.include?('-') $stdin.read else display_help end end |