Class: SCParse::ScriptCommandParser

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

Overview

Super-duper uber-parent class for all commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = $0) ⇒ ScriptCommandParser

Creates a new command object with the name ‘main’ and sets itself as the parent of the command.



266
267
268
269
270
# File 'lib/scparse.rb', line 266

def initialize(name = $0)
  @main = Command.new('main')
  @main.parent = self
  @name = name
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



261
262
263
# File 'lib/scparse.rb', line 261

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



262
263
264
# File 'lib/scparse.rb', line 262

def name
  @name
end

Instance Method Details

#add_command(command) ⇒ Object

Adds a subcommand to the main command object.



273
274
275
# File 'lib/scparse.rb', line 273

def add_command(command)
  @main.add_command(command)
end

#has_command?(command) ⇒ Boolean

Does this application have a command with the given name? Note: returns command or nil, not true/false.

Returns:

  • (Boolean)


280
281
282
# File 'lib/scparse.rb', line 280

def has_command?(command)
  @main.has_command?(command)
end

#optionsObject

Returns any options in the main command object.



285
286
287
# File 'lib/scparse.rb', line 285

def options
  @main.options
end

#options=(options) ⇒ Object

Sets the options for the main command object.



290
291
292
# File 'lib/scparse.rb', line 290

def options=(options)
  @main.options = options
end

#parse!(argv = ARGV) ⇒ Object

Runs the main command object using the given arguments (which by default is ARGV).



302
303
304
305
306
307
308
309
310
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/scparse.rb', line 302

def parse!(argv = ARGV)
  cmd = @main
  opts = Array.new
  args = Array.new(argv)

  # The cmd variable is set to nil once we've reached the
  # last child command and it's executed.
  while !cmd.nil?
    # If the command has subcommands, then it should not
    # be executed on.  Rather, look to see if a subcommand
    # is specified or if options have been passed.
    if cmd.has_commands?
      arg = args.shift
      
      if arg
        if cmd.commands[arg]
          cmd.options.parse!(opts) unless cmd.options.nil?
          cmd = cmd.commands[arg]
          opts.clear
        else
          opts << arg
        end
      else
        raise RuntimeError
      end
    else
      # This command has no subcommands, which means it
      # is the command to execute on.
      cmd.options.parse!(args) unless cmd.options.nil? || cmd.is_a?(Script)
      cmd.execute(args)
      cmd = nil
    end
  end

rescue RuntimeError, OptionParser::ParseError => error
  # OptionParser will raise an exception of an option is passed
  # that isn't recognized.  Thus, show the help screen!
  @main.commands['help'].execute([]) if @main.commands['help']
  exit
end

#set_prerequisites_block(&block) ⇒ Object

Sets the prerequisites block for the main command object.



296
297
298
# File 'lib/scparse.rb', line 296

def set_prerequisites_block(&block)
  @main.set_prerequisites_block(&block)
end