Class: Watnow::OptParser

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

Overview

Home made Option parser built on top of Ruby’s optparse

Class Method Summary collapse

Class Method Details

.get_option_value(option, args) ⇒ Object



48
49
50
# File 'lib/watnow/option_parser.rb', line 48

def self.get_option_value(option, args)
  args.include?(option) ? args[args.index(option) + 1] : nil
end

.parse(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/watnow/option_parser.rb', line 7

def self.parse(args)
  # Defaults
  options = {}
  options[:directory] = '.'

  opts = OptionParser.new('', 24, '  ') do |opts|
    opts.separator '  Usage: watnow [options]'
    opts.separator ''

    # Help
    opts.on('-h', '--help', 'show this message') do
      puts opts
      exit
    end

    # Version
    opts.on('-v', '--version', 'display version') do
      puts VERSION
      exit
    end

    # Directory
    opts.on('-d', '--directory DIR', 'directory DIR to scan (defaults: ./)') do |d|
      options[:directory] = d
    end

    # Commands
    opts.separator ''
    opts.separator '  watnow commands:'
    opts.separator '  open <ID>    open annotation ID in your editor'
    opts.separator '  remove <ID>  remove annotation ID'

    options[:open] = get_option_value('open', args)
    options[:remove] = get_option_value('remove', args)

  end

  opts.parse!(args)
  options
end