Class: Watcher::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kanseishitsu/watch/argument_parser.rb

Overview

Define the ArgumentsParser class

Constant Summary collapse

UNDERSCORE_PATTERN =
%r{_}
HYPHEN_STRING =
'-'.freeze
FLAGS =
i[banner show_all list remove verbose].freeze
POSITIONAL =
i[watch_path executable_path_with_args].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser = OptionParser.new) ⇒ ArgumentsParser

Returns a new instance of ArgumentsParser.



24
25
26
27
28
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 24

def initialize(parser = OptionParser.new)
  @parser = parser
  @options = {}
  FLAGS.each { |method_name| self.method(method_name).call }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 22

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



22
23
24
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 22

def parser
  @parser
end

Class Method Details

.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) ⇒ Object

rubocop: disable Metrics/MethodLength



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 119

def self.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new)
  arguments_parser.parser.parse!(args)
  if !arguments_parser.options? && ARGV.length != 2
    message = 'A directory and executable handler is required'
    raise OptionParser::MissingArgument, message
  elsif !args.empty?
    arguments_parser.positional!(args)
  end
  arguments_parser.options
rescue OptionParser::AmbiguousOption => e
  abort e.message
rescue OptionParser::ParseError => e
  puts e.message
  arguments_parser.usage!
end

Instance Method Details



30
31
32
33
34
35
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 30

def banner
  @parser.banner = "Usage: #{File.basename($PROGRAM_NAME)} " \
    '<watch_path> <executable_path_with_args>'
  @parser.separator ''
  @parser.separator 'Options:'
end

#demand(arg, positional: false) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


93
94
95
96
97
98
99
100
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 93

def demand(arg, positional: false)
  return @options[arg] unless @options[arg].nil?

  required_arg = if positional then "<#{arg}>"
  else "--#{arg.to_s.gsub(UNDERSCORE_PATTERN, HYPHEN_STRING)}"
  end
  raise OptionParser::MissingArgument, "Required argument: #{required_arg}"
end

#executable_path_with_args(args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 81

def executable_path_with_args(args)
  executable, *args = args
  return if executable.nil?

  executable_path = Object.new.extend(Which).which(executable)
  if executable_path.nil?
    message = "Executable not found: #{executable_path}"
    raise OptionParser::InvalidArgument, message
  end
  @options[:executable_path_with_args] = [executable_path] + args
end

#listObject



43
44
45
46
47
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 43

def list
  @parser.on('-l', '--list', 'List watcher labels') do
    @options[:list] = true
  end
end

#options?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 109

def options?
  ArgumentsParser::FLAGS.any? { |flag| @options.include?(flag) }
end

#positional!(args) ⇒ Object



102
103
104
105
106
107
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 102

def positional!(args)
  POSITIONAL.each do |opt|
    self.method(opt).call(args)
    self.demand(opt, positional: true)
  end
end

#removeObject



49
50
51
52
53
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 49

def remove
  @parser.on('-r', '--remove=<label>', 'Remove a watcher by label') do |label|
    @options[:remove] = label
  end
end

#show_allObject



37
38
39
40
41
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 37

def show_all
  @parser.on('--show-all', 'Show watchers') do
    @options[:show_all] = true
  end
end

#usage!Object



113
114
115
116
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 113

def usage!
  puts @parser
  exit
end

#verboseObject



55
56
57
58
59
60
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 55

def verbose
  @options[:log_level] ||= Logger::INFO
  @parser.on_tail('-v', '--verbose', 'Increase verbosity') do
    @options[:log_level] -= 1
  end
end

#versionObject



62
63
64
65
66
67
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 62

def version
  @parser.on_tail('--version', 'Show version') do
    puts "#{File.basename($PROGRAM_NAME)} version #{Kanseishitsu::VERSION}"
    exit
  end
end

#watch_path(args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kanseishitsu/watch/argument_parser.rb', line 69

def watch_path(args)
  return if (v = args.shift).nil?

  watch_path = File.expand_path(v)
  unless File.exist?(watch_path) &&
      File.directory?(watch_path)
    message = "Directory not found: #{watch_path}"
    raise OptionParser::InvalidArgument, message
  end
  @options[:watch_path] = [watch_path]
end