Class: Cmdlib::Handler
- Inherits:
-
Object
- Object
- Cmdlib::Handler
- Defined in:
- lib/cmdlib/handler.rb
Overview
Class with methods for handled commands from CLI application.
Constant Summary collapse
- OPTION_PREFIX_SHORT =
'-'
- OPTION_PREFIX_LONG =
'--'
Instance Attribute Summary collapse
-
#usage ⇒ Object
This array contain information to use application.
Instance Method Summary collapse
-
#addcmd(cmd) ⇒ Object
Add CLICommand object to CLIHandler.
-
#addopt(opt) ⇒ Object
Add CLICommand object to CLIHandler.
-
#getopt(opt) ⇒ Object
Check input arguments on equal option syntax.
-
#initialize ⇒ Handler
constructor
A new instance of Handler.
-
#optparser ⇒ Object
parse command line on the option exist.
-
#run ⇒ Object
Main handler method, execute command handler.
Constructor Details
#initialize ⇒ Handler
Returns a new instance of Handler.
11 12 13 14 15 |
# File 'lib/cmdlib/handler.rb', line 11 def initialize @usage = [] @cmdlist = [] @optlist = [] end |
Instance Attribute Details
#usage ⇒ Object
This array contain information to use application.
9 10 11 |
# File 'lib/cmdlib/handler.rb', line 9 def usage @usage end |
Instance Method Details
#addcmd(cmd) ⇒ Object
Add CLICommand object to CLIHandler.
18 19 20 21 22 23 24 25 |
# File 'lib/cmdlib/handler.rb', line 18 def addcmd ( cmd ) raise TypeError, 'Incorrectly types for command object.' unless cmd.respond_to? :describe and cmd.respond_to? :handler and cmd.describe.instance_of? Describe @cmdlist << cmd end |
#addopt(opt) ⇒ Object
Add CLICommand object to CLIHandler.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cmdlib/handler.rb', line 28 def addopt ( opt ) raise TypeError, 'Incorrectly types for option object.' unless opt.respond_to? :brief and opt.respond_to? :shortname and opt.respond_to? :longname and opt.respond_to? :value and opt.respond_to? :param and opt.brief.instance_of? String @optlist << opt end |
#getopt(opt) ⇒ Object
Check input arguments on equal option syntax. Return option name if success, else return ”.
135 136 137 138 139 140 141 142 143 |
# File 'lib/cmdlib/handler.rb', line 135 def getopt ( opt ) if opt.length > OPTION_PREFIX_LONG.length then return opt[OPTION_PREFIX_LONG.length, opt.length] if opt[0, OPTION_PREFIX_LONG.length] == OPTION_PREFIX_LONG end if opt.length > OPTION_PREFIX_SHORT.length then return opt[OPTION_PREFIX_SHORT.length, opt.length] if opt[0, OPTION_PREFIX_SHORT.length] == OPTION_PREFIX_SHORT end return '' end |
#optparser ⇒ Object
parse command line on the option exist.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/cmdlib/handler.rb', line 146 def optparser = [] # search option in option list. ARGV.each_with_index do |opt,i| r = getopt( opt ) << i if r != '' and r != 'h' and r != 'help' end # handling each option in list. .each do |oi| # find option in CLIHandler list. @optlist.each do |opt| argval = getopt( ARGV[oi] ) # if option has a parameter. if opt.param then # if option-value set in next argument. if argval == opt.longname or argval == opt.shortname then # argument is not set. if oi.next >= ARGV.size then puts "fatal error: unable to find argument for option '#{ARGV[oi]}'." exit end # if next argument option. if getopt( ARGV[oi.next] ) != '' then puts "fatal error: miss argument for option '#{ARGV[oi]}'." exit end opt.value = ARGV[oi.next] << oi.next next end # if option-value built-in option. oname = opt.shortname oname = opt.longname if ARGV[oi][0, OPTION_PREFIX_LONG.length] == OPTION_PREFIX_LONG if argval[0, oname.length] == oname then opt.value = argval[oname.length, argval.length - oname.length] # delete assign symbols. opt.value = opt.value[1, opt.value.length - 1] if opt.value[0] == '=' end # single (toggle) option. else if argval == opt.longname or argval == opt.shortname then opt.value = true end end end end # delete option from ARGV. onamelist = [] .each do |oi| onamelist << ARGV[oi] end onamelist.each do |oname| ARGV.delete( oname ) end end |
#run ⇒ Object
Main handler method, execute command handler.
41 42 43 44 45 46 47 48 49 50 51 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cmdlib/handler.rb', line 41 def run # parsing options. optparser # handled position arguments. if ARGV.size > 0 then if ARGV[0] == 'help' or ARGV[0] == '--help' or ARGV[0] == '-h' then # display help specificly command. if ARGV.size == 2 then @cmdlist.each do |e| if ARGV[1] == e.describe.oname then e.describe.display exit end end puts "fatal error: unknown command with name - #{ARGV[1]}" # display usage help. else puts # dislpay usage information. @usage.each do |e| puts e end if @cmdlist.size > 0 then puts puts '** COMMANDS:' maxlen = 0 # dislpay command list. @cmdlist.each do |e| maxlen = e.describe.oname.length if e.describe.oname.length > maxlen end @cmdlist.each do |e| print ' ' + e.describe.oname print "#{' ' * (maxlen - e.describe.oname.length)} -- " puts e.describe.brief end puts puts ' For details type: help <command>' end puts # display options list. if @optlist.size > 0 then puts '** OPTIONS:' maxlen = 0 # make shortname to string with option names. listout = [] @optlist.each do |e| optnames = '' if e.shortname.length == 0 optnames += ' ' else optnames += OPTION_PREFIX_SHORT + e.shortname end optnames += ',' if e.longname.length != 0 optnames += OPTION_PREFIX_LONG + e.longname end listout << optnames maxlen = optnames.length if optnames.length > maxlen end # make longname to string with option names. @optlist.each_with_index do |e,i| print ' ' + listout[i] print "#{' ' * (maxlen - listout[i].length)} -- " puts e.brief end puts end end else # handling input command. @cmdlist.each do |e| if ARGV[0] == e.describe.oname then # check arguments numbers. if e.argnum != nil then if (ARGV.size - 1) != e.argnum then puts 'fatal error: wrong arguments for program.' exit end end # run handler of command. e.handler exit end end puts "fatal error: unknown command with name - #{ARGV[0]}" end # exit, after handled all arguments. exit end end |