Method: CommandLion::App.run

Defined in:
lib/command_lion/app.rb

.run(&block) ⇒ Object

This run method is a pretty important method when using command lion typically.

Under the hood, an application object is initialized. The block of code passed to this method is then used as the code that is ran in the context of a application object. So all of those methods will be available.



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
# File 'lib/command_lion/app.rb', line 151

def self.run(&block)
  # Initialize an instance of an App object.
  app = new
  # Evaluate the block of code within the context of that App object.
  app.instance_eval(&block)
  # Parse the application logic out.
  app.parse
  # Sometimes a command-line application is run without being given any arguments.
  if ARGV.empty?
    # Default to a help menu.
    if cmd = app.commands[:help]
      cmd.before.call if cmd.before?
      cmd.action.call if cmd.action?
      cmd.after.call  if cmd.after?
      # maybe exit?
    else
      # Use the default help menu for the application unless that's been
      # explictly removed by the author for whatever reason.
      default_help(app) unless app.default_help_menu_removed?
    end
  else
    threadz = false
    app.commands.each do |_, cmd|
      next unless cmd.given?
      if cmd.threaded?
        threadz = [] unless threadz
        threadz << Thread.new do 
          cmd.before.call if cmd.before?
          cmd.action.call if cmd.action?
          cmd.after.call  if cmd.after?
        end
      else
        cmd.before.call if cmd.before?
        cmd.action.call if cmd.action?
        cmd.after.call  if cmd.after?
      end
    end
    threadz.map(&:join) if threadz
  end
end