Class: SCParse::Command

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

Overview

A command is a block of code that should have its options parsed before being executed. A command can also be a parent to subcommands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, execute_prerequisites = true) ⇒ Command

Returns a new instance of Command.



63
64
65
66
# File 'lib/scparse.rb', line 63

def initialize(name, execute_prerequisites = true)
  @name = name
  @execute_prerequisites = execute_prerequisites
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



59
60
61
# File 'lib/scparse.rb', line 59

def commands
  @commands
end

#nameObject (readonly)

Returns the value of attribute name.



58
59
60
# File 'lib/scparse.rb', line 58

def name
  @name
end

#optionsObject

Returns the value of attribute options.



60
61
62
# File 'lib/scparse.rb', line 60

def options
  @options
end

#parentObject

Returns the value of attribute parent.



61
62
63
# File 'lib/scparse.rb', line 61

def parent
  @parent
end

Instance Method Details

#add_command(command) ⇒ Object



68
69
70
71
72
# File 'lib/scparse.rb', line 68

def add_command(command)
  @commands ||= CommandHash.new
  @commands[command.name] = command
  command.parent = self
end

#execute(args) ⇒ Object

Run the main block of code for this command, first running the prerequisite block of code if supposed to do so.



128
129
130
131
# File 'lib/scparse.rb', line 128

def execute(args)
  parents.reverse_each {|parent| parent.prerequisites} if execute_prerequisites?
  @execution_block.call(self, args) if @execution_block
end

#execute_prerequisites?Boolean

Is this command supposed to run its prerequisiste block of code before executing (as defined in the constructor)?

Returns:

  • (Boolean)


117
118
119
# File 'lib/scparse.rb', line 117

def execute_prerequisites?
  return @execute_prerequisites
end

#has_command?(command) ⇒ Boolean

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

Returns:

  • (Boolean)


95
96
97
# File 'lib/scparse.rb', line 95

def has_command?(command)
  return @commands[command]
end

#has_commands?Boolean

Does this command have any subcommands it’s a parent to?

Returns:

  • (Boolean)


100
101
102
# File 'lib/scparse.rb', line 100

def has_commands?
  return @commands.nil? ? false : true
end

#parentsObject

Return an array of parents for this command, up to but not including the uber-parent ScriptCommandParser object.



83
84
85
86
87
88
89
90
91
# File 'lib/scparse.rb', line 83

def parents
  cmd = self
  parents = [cmd]
  begin
    cmd = cmd.parent
    parents << cmd
  end until cmd.parent.is_a?(ScriptCommandParser)
  return parents
end

#prerequisitesObject

Run the prerequisite block of code



111
112
113
# File 'lib/scparse.rb', line 111

def prerequisites
  @prerequisites_block.call(self) if @prerequisites_block
end

#scparserObject

Return the uber-parent, which is a ScriptCommandParser object.



75
76
77
78
79
# File 'lib/scparse.rb', line 75

def scparser
  cmd = parent
  cmd = cmd.parent while !cmd.nil? && !cmd.is_a?(ScriptCommandParser)
  return cmd
end

#set_execution_block(&block) ⇒ Object

Set the main block of code for this command.



122
123
124
# File 'lib/scparse.rb', line 122

def set_execution_block(&block)
  @execution_block = block
end

#set_prerequisites_block(&block) ⇒ Object

Set the block of code to run as a prerequisite to executing the command (such as bootstrapping code).



106
107
108
# File 'lib/scparse.rb', line 106

def set_prerequisites_block(&block)
  @prerequisites_block = block
end

#show_helpObject

Along with showing the usage for the command, this also displays any and all the child subcommands that belong to this command.



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/scparse.rb', line 150

def show_help
  puts usage
  puts
  if has_commands?
    list_commands
    puts
  end
  unless options.nil? || options.summarize.empty?
    puts "Available options:"
    puts options.summarize
  end
end

#usageObject

Define the usage of this command, using the name of the command and the uber-parent ScriptCommandParser.



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/scparse.rb', line 135

def usage
  usage = "Usage: #{scparser.name}"
  usage << " [options] "
  usage << parents.reverse.collect do |parent|
    unless parent.name == "main"
      use = parent.name
      use << " [options]"
    end
  end.join('')
  usage << (has_commands? ? " COMMAND [options] [ARGS]" : " [ARGS] ")
  return usage
end