Class: Utils::CommandParser
- Inherits:
-
Object
- Object
- Utils::CommandParser
- Defined in:
- lib/ngi/utils/command_parser.rb
Overview
Similar to Ruby’s OptionParser However, this is customized for angular_init
Defined Under Namespace
Classes: Output
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#name ⇒ Object
Returns the value of attribute name.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize {|_self| ... } ⇒ CommandParser
constructor
A new instance of CommandParser.
-
#on(options, description = '', &block) ⇒ Object
register the listeners.
- #parse(args) ⇒ Object
- #register_help ⇒ Object
- #register_version ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ CommandParser
Returns a new instance of CommandParser.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ngi/utils/command_parser.rb', line 26 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
#args ⇒ Object (readonly)
Returns the value of attribute args.
11 12 13 |
# File 'lib/ngi/utils/command_parser.rb', line 11 def args @args end |
#banner ⇒ Object
Returns the value of attribute banner.
12 13 14 |
# File 'lib/ngi/utils/command_parser.rb', line 12 def @banner end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/ngi/utils/command_parser.rb', line 12 def name @name end |
#separator ⇒ Object
Returns the value of attribute separator.
12 13 14 |
# File 'lib/ngi/utils/command_parser.rb', line 12 def separator @separator end |
#version ⇒ Object
Returns the value of attribute version.
12 13 14 |
# File 'lib/ngi/utils/command_parser.rb', line 12 def version @version end |
Instance Method Details
#on(options, description = '', &block) ⇒ Object
register the listeners
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ngi/utils/command_parser.rb', line 50 def on(, description = '', &block) listener = {} # be able to pass in a single argument, or an array of arguments = * unless .is_a? Array listener[:options] = .map do |opt| opt.strip.split(' ') end listener[:block] = block listener[:description] = description @listeners << listener end |
#parse(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ngi/utils/command_parser.rb', line 66 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_help ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ngi/utils/command_parser.rb', line 94 def register_help # automaticaly register this listener on(['-h', '--help'], 'Show the help menu') do Output.new(@separator).to_s Output.new(@banner).to_s @listeners.each_with_index do |listener, i| desc = "\n" << "(#{i + 1}) #{listener[:description]}: " desc << "#{listener[:options].join(', ')}" Output.new(desc).to_s end Output.new(@separator).to_s end end |