Module: Bkmrq::Cli::ArgParser

Defined in:
lib/bkmrq/cli/arg_parser.rb

Overview

Argument Parser

Class Method Summary collapse

Class Method Details

.assign_or_raise_arg(options, key, value) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


11
12
13
14
15
# File 'lib/bkmrq/cli/arg_parser.rb', line 11

def self.assign_or_raise_arg(options, key, value)
  raise OptionParser::MissingArgument if value.blank?

  options[key] = value
end


51
52
53
54
55
# File 'lib/bkmrq/cli/arg_parser.rb', line 51

def self.banner(parser)
  parser.banner = Bkmrq::Ui::BANNER
  parser.separator Bkmrq::Ui::USAGE
  parser.separator "\n\s\s\s\s# OPTIONS:\n"
end

.browser_opt(parser) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/bkmrq/cli/arg_parser.rb', line 25

def self.browser_opt(parser)
  parser.on(
    '-b',
    '--browser [BROWSER]',
    %i[chrome chromium brave firefox safari opera],
    'export bookmarks from <BROWSER>'
  )
end

.exclude_patterns_opt(parser) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/bkmrq/cli/arg_parser.rb', line 43

def self.exclude_patterns_opt(parser)
  parser.on(
    '-x',
    '--exclude [XPATTERNS]',
    'exclude bookmarks or folders containing <KEYWORDS>'
  )
end

.filepath_opt(parser) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/bkmrq/cli/arg_parser.rb', line 17

def self.filepath_opt(parser)
  parser.on(
    '-o',
    '--to [FILEPATH]',
    'save the export file to <FILEPATH>'
  )
end

.format_opt(parser) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/bkmrq/cli/arg_parser.rb', line 34

def self.format_opt(parser)
  parser.on(
    '-f',
    '--format [FILEFORMAT]',
    %i[markdown json csv html],
    'export bookmarks to <FORMAT> File.'
  )
end

.generate_opt_parserObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bkmrq/cli/arg_parser.rb', line 71

def self.generate_opt_parser
  OptionParser.new do |parser|
    banner(parser)
    filepath_opt(parser)
    browser_opt(parser)
    format_opt(parser)
    exclude_patterns_opt(parser)
    version(parser)
    help(parser)
  end
end

.help(parser) ⇒ Object



64
65
66
67
68
69
# File 'lib/bkmrq/cli/arg_parser.rb', line 64

def self.help(parser)
  parser.on('-h', '--help', 'print bkmrq help docs') do
    puts parser
    exit
  end
end

.parse!(parsed = {}) ⇒ Object



83
84
85
86
87
# File 'lib/bkmrq/cli/arg_parser.rb', line 83

def self.parse!(parsed = {})
  parsed.tap do |options|
    generate_opt_parser.parse!(ARGV, into: options)
  end
end

.version(parser) ⇒ Object



57
58
59
60
61
62
# File 'lib/bkmrq/cli/arg_parser.rb', line 57

def self.version(parser)
  parser.on('-v', '--version', 'print bkmrq version') do
    puts "bkmrq v#{Bkmrq::VERSION}"
    exit
  end
end