Class: Cmdlib::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdlib/application.rb

Overview

Class edscribe CLI application (TOP class).

Constant Summary collapse

OPTION_PREFIX_SHORT =
'-'
OPTION_PREFIX_LONG =
'--'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appname) ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cmdlib/application.rb', line 13

def initialize ( appname )
  @name  = appname
  @about = []
  @usage = []
  @commands = []
  @options = {}
  @default = nil
  @version = '0.1.1'
  
  addopt Option.new( 'V', 'version', 'Display application version.' )
end

Instance Attribute Details

#aboutObject

This array contain information to use application.



8
9
10
# File 'lib/cmdlib/application.rb', line 8

def about
  @about
end

#commandsObject

Version string (will be display to -v or –version).



11
12
13
# File 'lib/cmdlib/application.rb', line 11

def commands
  @commands
end

#nameObject

This array contain information to use application.



8
9
10
# File 'lib/cmdlib/application.rb', line 8

def name
  @name
end

#optionsObject

Version string (will be display to -v or –version).



11
12
13
# File 'lib/cmdlib/application.rb', line 11

def options
  @options
end

#usageObject

This array contain information to use application.



8
9
10
# File 'lib/cmdlib/application.rb', line 8

def usage
  @usage
end

#versionObject

Version string (will be display to -v or –version).



11
12
13
# File 'lib/cmdlib/application.rb', line 11

def version
  @version
end

Instance Method Details

#addcmd(cmd) ⇒ Object

Add CLICommand object to CLIHandler.

Raises:

  • (TypeError)


27
28
29
30
31
32
# File 'lib/cmdlib/application.rb', line 27

def addcmd ( cmd )
  raise TypeError, 'Incorrectly types for command object.' unless
	cmd.is_a? Cmdlib::Command

  @commands << cmd
end

#addopt(opt) ⇒ Object

Add CLICommand object to CLIHandler.

Raises:

  • (TypeError)


36
37
38
39
40
41
# File 'lib/cmdlib/application.rb', line 36

def addopt ( opt )
  raise TypeError, 'Incorrectly types for option object.' unless
	opt.instance_of? Cmdlib::Option

  @options[opt.longname.to_sym] = opt
end

#command_search(cmdlist, cmd) ⇒ Object

Search command in command list. Return command object if search success, else return nil. cmdlist – array with command objects. cmd – string with command name from ARGV.



218
219
220
221
222
223
224
# File 'lib/cmdlib/application.rb', line 218

def command_search( cmdlist, cmd )
  cmdlist.each do |c|
	c.init
	return c if c.name == cmd
  end
  return nil
end

#command_selectObject

Select and return command object in application.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/cmdlib/application.rb', line 228

def command_select
  command = command_search( @commands, ARGV[0] )
  if command != nil then
    # remove command name from ARGV and search next.
    ARGV.delete_at( 0 )
    ARGV.each do |arg|
	  cmd = command_search( command.subcmd, arg )
	  break if cmd == nil
	  ARGV.delete_at( 0 )
	  command = cmd
    end
  end
  return command
end

#default(cmd) ⇒ Object

Default command for run without arguments.

Raises:

  • (TypeError)


45
46
47
48
49
50
# File 'lib/cmdlib/application.rb', line 45

def default ( cmd )
  raise TypeError, 'Incorrectly types for command object.' unless
	cmd.is_a? Cmdlib::Command
	
  @default = cmd
end

#display_commands(cmdlist) ⇒ Object

Display commands info



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cmdlib/application.rb', line 54

def display_commands( cmdlist )
  maxlen = 0
  cmdlist.each do |cmd|
	maxlen = cmd.name.length if cmd.name.length > maxlen
  end
  cmdlist.each do |cmd|
	print '  ' + cmd.name
	print "#{' ' * (maxlen - cmd.name.length)}  # "
	puts cmd.brief
  end
end

#display_options(optlist) ⇒ Object

Display options info



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cmdlib/application.rb', line 68

def display_options( optlist )
  maxlen = 0
  listout = []
  optlist.each_value do |opt|
	optnames = ''
	if opt.shortname.length == 0
      optnames += '  ' 
	else
	  optnames += OPTION_PREFIX_SHORT + opt.shortname
	end
	optnames += ','
	optnames += OPTION_PREFIX_LONG + opt.longname if opt.longname.length != 0
	optnames += '=[...]' if opt.param == true
	listout << { :n => optnames, :b => opt.brief }
	maxlen = optnames.length if optnames.length > maxlen
  end
  listout.each do |opt|
	print '  ' + opt[:n]
	print "#{' ' * (maxlen - opt[:n].length)}  # "
	puts opt[:b]
  end
end

#getopt(opt) ⇒ Object

Check input arguments on equal option syntax. if success return option { :n => <argument name>, :t => <option type> }, else return ” in all fields.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cmdlib/application.rb', line 247

def getopt ( opt )
  result = { :n => '', :t => '', }
  if opt.length > OPTION_PREFIX_LONG.length then
    if opt[0, OPTION_PREFIX_LONG.length] == OPTION_PREFIX_LONG then
      result[:n] = opt[OPTION_PREFIX_LONG.length, opt.length]
      result[:t] = OPTION_PREFIX_LONG
      return result
    end
  end
  if opt.length > OPTION_PREFIX_SHORT.length then
    if opt[0, OPTION_PREFIX_SHORT.length] == OPTION_PREFIX_SHORT then
      result[:n] = opt[OPTION_PREFIX_SHORT.length, opt.length]
      result[:t] = OPTION_PREFIX_SHORT
      return result 
    end
  end
  return result
end

#option_compare(opt_form, opt_app) ⇒ Object

Compare parsing option (form by getopt) with application option. Return true if options is equal, else return false. opt_form – object formed by getopt. opt_app – application option object.



283
284
285
286
287
288
289
290
291
# File 'lib/cmdlib/application.rb', line 283

def option_compare ( opt_form, opt_app )
  case opt_form[:t]
  when OPTION_PREFIX_SHORT
    return true if opt_form[:n][0, opt_app.shortname.length] == opt_app.shortname
  when OPTION_PREFIX_LONG
	return true if opt_form[:n].split('=')[0] == opt_app.longname
  end
  return false
end

#option_excessObject

Check ARGV to exess options.



268
269
270
271
272
273
274
275
276
# File 'lib/cmdlib/application.rb', line 268

def option_excess
  ARGV.each do |opt|
	o = getopt( opt )
	if o[:n] != '' then
	  puts "fatal error: unknown option '#{o[:t]}#{o[:n]}'"
	  exit
	end
  end
end

#option_parser(optlist) ⇒ Object

Parsing options in command line.



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/cmdlib/application.rb', line 345

def option_parser( optlist )
  return if optlist.size == 0
  deletelist = []
  # search option in argument list.
  ARGV.each_with_index do |opt,i|
	o = getopt( opt )
	if o[:n] != '' and o[:n] != 'h' and o[:n] != 'help'
	  o[:i] = i
	  # Search in application list
	  result = option_search( o, optlist )
	  if result != nil then
 deletelist << opt
 result = option_set( o, optlist[result] )
 deletelist << result if result != ''
	  end
	end
  end
  # delete option from ARGV.
  deletelist.each do |n|
	ARGV.delete( n )
  end
end

#option_search(option, options) ⇒ Object

Search option in application options. Return option key-name if option found, else return nil. option – object formed by getopt. options – options object list.



298
299
300
301
302
303
304
# File 'lib/cmdlib/application.rb', line 298

def option_search( option, options )
  # Search in global options list
  options.each_value do |opt|
    return opt.longname.to_sym if option_compare( option, opt )
  end
  return nil
end

#option_set(opt_form, opt_app) ⇒ Object

Set application option object. opt_form – object formed by getopt. opt_app – application option object. Return delete name in ARGV or ”.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/cmdlib/application.rb', line 311

def option_set ( opt_form, opt_app )
  if opt_app.param then
	# option parameter buit-in option name by '='
	if opt_form[:n].include? '=' then
	  opt_app.value = opt_form[:n].split('=')[-1]
	  return ''
	end
	# option parameter buit-in option name
	if opt_form[:t] == OPTION_PREFIX_SHORT and
opt_form[:n].length > opt_app.shortname.length then
	  opt_app.value = opt_form[:n][opt_app.shortname.length, opt_form[:n].length - opt_app.shortname.length]
	  return ''
	end
	# option parameter present in next ARGV.
	if opt_form[:i].next >= ARGV.size then
      puts "fatal error: unable to find argument for option '#{opt_form[:n]}'."
      exit
	end
	# if next argument is a option.
	arg = ARGV[opt_form[:i].next]
    if getopt( arg )[:n] != '' then
      puts "fatal error: miss argument for option '#{opt_form[:t]}#{opt_form[:n]}'."
      exit
    end
	opt_app.value = arg
	return arg
  else
	opt_app.value = true
  end
  return ''
end

#runObject

Main method to run application.



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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
203
204
205
206
207
208
209
210
# File 'lib/cmdlib/application.rb', line 93

def run
  option_parser @options
  
  # Check on include version request.
  if @options[:version].value then
	puts "#{@name}, version #{@version}"
	exit
  end

  # Check on include help request.
  if ARGV[0] == 'help' or ARGV[0] == '--help' or ARGV[0] == '-h' then
	# Help arguments apsent, well then display information about application.
	if ARGV.size == 1 then
	  puts
	  puts "*** #{@name} ***".center(80)
	  # Display about info.
	  if @about.size > 0 then
 puts '** ABOUT:'
 @about.each do |line|
   puts "  #{line}"
 end
	  end
	  # Display usage info.
	  if @usage.size > 0 then
 puts
 puts '** USAGE:'
 @usage.each do |line|
   puts "  #{line}"
 end
	  end
	  # Display options info.
	  puts
	  puts '** OPTIONS:'
	  display_options @options
	  # Display commands info
	  if @commands.size > 0 then
 @commands.each do |c| c.init end
 puts
 puts '** COMMANDS:'
 display_commands @commands
 puts
 puts "For details, type: help [COMMAND]"
	  end
	  puts
	# Help arguments exist, find command in application command list.
	else
	  ARGV.delete_at( 0 )
	  cmd = command_select
	  if ARGV.size != 0 then
 puts "fatal error: unknown command '#{ARGV[0]}'"
 exit
	  end
	  # Display describe information on command.
	  puts
	  puts Cmdlib::Describe.outtitle( cmd.name )
	  puts "  #{cmd.brief}"
	  if cmd.details.size > 0 then
 puts
 puts '** DETAILS:'
 cmd.details.each do |e|
   puts "  #{e}"
 end
	  end
	  if cmd.example.size > 0 then
 puts
 puts '** EXAMPLE:'
 cmd.example.each do |e|
   puts "  #{e}"
 end
	  end
	  # Display options info.
	  if cmd.options.size > 0 then
 puts
 puts '** OPTIONS:'	  
 display_options cmd.options
	  end
	  # Display commands info.
	  if cmd.subcmd.size > 0 then
 cmd.subcmd.each do |c| c.init end
 puts
 puts '** SUBCOMMANDS:'
 display_commands cmd.subcmd
 puts
 puts "For details, type: help #{cmd.name} [SUBCOMMAND]"
	  end
	  puts
	end
	exit
  end
  
  # Handling default command (if exist her).
  if @default != nil then
	option_excess
	if ARGV.size < @default.argnum then
	  puts "fatal error: to few arguments for programm, use <help>."
	else
	  @default.handler( @options, ARGV )
	end
	exit
  end
  
  # Handling commands.
  cmd = command_select
  if cmd == nil then
	puts "fatal error: unknown command or command miss, use <help>."
	exit
  end
  if ARGV.size < cmd.argnum then
	puts "fatal error: to few arguments for command, use: <help> <command name>."
	exit
  end
  # Scaning options fir this command
  option_parser cmd.options
  option_excess
  #cmd.init
  cmd.handler( @options, ARGV )
  exit
end