class SafariBookmarksParser::Commands::BaseCommand
Attributes
output_format[R]
output_path[R]
plist_path[R]
Public Class Methods
new(argv)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 12 def initialize(argv) @plist_path = File.expand_path('~/Library/Safari/Bookmarks.plist') @output_path = nil @output_format = :json parse_options(argv) handle_argv(argv) end
Private Instance Methods
do_parse(parser, argv)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 53 def do_parse(parser, argv) parser.parse!(argv) rescue OptionParser::ParseError => e raise Error, e.message end
format_to_text(obj)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 23 def format_to_text(obj) case @output_format when :json JSON.pretty_generate(obj) when :yaml YAML.dump(obj) end end
handle_argv(argv)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 59 def handle_argv(argv) @plist_path = argv.first if argv.any? end
on_output_format(parser)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 46 def on_output_format(parser) desc = %(Output format (default: "#{@output_format}"; one of "json" or "yaml")) parser.on('-f', '--output-format=FORMAT', %w[json yaml], desc) do |value| @output_format = value.to_sym end end
on_output_path(parser)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 40 def on_output_path(parser) parser.on('-o', '--output-path=PATH', 'Output path (default: output to $stdout)') do |value| @output_path = value end end
output_text(text)
click to toggle source
# File lib/safari_bookmarks_parser/commands/base_command.rb, line 32 def output_text(text) if @output_path File.write(@output_path, text) else puts text end end