Class: CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ngi/utils/command_parser.rb

Overview

Similar to Ruby’s OptionParser However, this is customized for angular_init

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ CommandParser

Returns a new instance of CommandParser.

Yields:

  • (_self)

Yield Parameters:

  • _self (CommandParser)

    the object that the method was called on



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ngi/utils/command_parser.rb', line 12

def initialize
  # TODO: to "Usage #{file_name}", etc.
  @name = '<CLI>'
  @banner = "Usage: #{@name} <command>"
  @version = '0.0.0'
  @separator = '===================='
  @listeners = []

  # automatically register some listeners
  register_help
  register_version

  # be able to pass in a block for setup
  yield(self) if block_given?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/ngi/utils/command_parser.rb', line 9

def args
  @args
end

Returns the value of attribute banner.



10
11
12
# File 'lib/ngi/utils/command_parser.rb', line 10

def banner
  @banner
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/ngi/utils/command_parser.rb', line 10

def name
  @name
end

#separatorObject

Returns the value of attribute separator.



10
11
12
# File 'lib/ngi/utils/command_parser.rb', line 10

def separator
  @separator
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/ngi/utils/command_parser.rb', line 10

def version
  @version
end

Instance Method Details

#on(options, description = '', &block) ⇒ Object

register the listeners



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ngi/utils/command_parser.rb', line 36

def on(options, description = '', &block)
  listener = {}

  # be able to pass in a single argument, or an array of arguments
  options = *options unless options.is_a? Array

  listener[:options] = options.map do |opt|
    opt.strip.split(' ')
  end

  listener[:block] = block
  listener[:description] = description

  @listeners << listener
end

#parse(args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ngi/utils/command_parser.rb', line 52

def parse(args)
  # puts @listeners
  matched_listener = {
    options: nil,
    block: nil
  }

  @listeners.each do |listener|
    listener[:options].each do |opt_arr|
      if opt_arr == args
        matched_listener[:options] = opt_arr
        matched_listener[:block] = listener[:block]
        break
      end
    end
  end

  if !matched_listener[:options].nil?
    # matched_listener[:options] should always be an array
    # when we call, we can each member of that array to be
    # passed separately
    matched_listener[:block].call(*matched_listener[:options])
  else
    # if there was no match, show the help menu
    parse(['-h'])
  end
end

#register_helpObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ngi/utils/command_parser.rb', line 80

def register_help
  # automaticaly register this listener
  on(['-h', '--help'], 'Show the help menu') do
    puts @separator
    puts @banner

    @listeners.each_with_index do |listener, i|
      desc = "\n" << "(#{i + 1}) #{listener[:description]}: "
      desc << "#{listener[:options].join(', ')}"
      puts desc
    end
    puts @separator
  end
end

#register_versionObject



95
96
97
98
99
100
# File 'lib/ngi/utils/command_parser.rb', line 95

def register_version
  # automaticaly register this listener
  on(['-v', '--version'], 'Show the version') do
    puts "#{@name} #{@version}"
  end
end