Class: Nanoc3::CLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/cli/command.rb

Overview

An abstract superclass for commands that can be executed. These are not the same as Cri commands, but are used in conjuction with Cri commands. nanoc commands will be called by Cri commands and will perform the actual execution of the command, as well as perform error handling if necessary.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, arguments, command) ⇒ Command

Returns a new instance of Command.

Parameters:

  • options (Hash)

    A hash contain the options and their values

  • arguments (Array)

    The list of arguments

  • command (Cri::Command)

    The Cri command



25
26
27
28
29
# File 'lib/nanoc3/cli/command.rb', line 25

def initialize(options, arguments, command)
  @options   = options
  @arguments = arguments
  @command   = command
end

Instance Attribute Details

#argumentsArray (readonly)

Returns The list of arguments.

Returns:

  • (Array)

    The list of arguments



15
16
17
# File 'lib/nanoc3/cli/command.rb', line 15

def arguments
  @arguments
end

#commandCri::Command (readonly)

Returns The Cri command.

Returns:

  • (Cri::Command)

    The Cri command



18
19
20
# File 'lib/nanoc3/cli/command.rb', line 18

def command
  @command
end

#optionsHash (readonly)

Returns A hash contain the options and their values.

Returns:

  • (Hash)

    A hash contain the options and their values



12
13
14
# File 'lib/nanoc3/cli/command.rb', line 12

def options
  @options
end

Class Method Details

.call(options, arguments, command) ⇒ void

This method returns an undefined value.

Runs the command with the given options, arguments and Cri command. This is a convenience method so that no individual command needs to be created.

Parameters:

  • options (Hash)

    A hash contain the options and their values

  • arguments (Array)

    The list of arguments

  • command (Cri::Command)

    The Cri command



42
43
44
# File 'lib/nanoc3/cli/command.rb', line 42

def self.call(options, arguments, command)
  self.new(options, arguments, command).call
end

Instance Method Details

#callvoid

This method returns an undefined value.

Runs the command.



49
50
51
52
53
# File 'lib/nanoc3/cli/command.rb', line 49

def call
  Nanoc3::CLI::ErrorHandler.handle_while(:command => self) do
    self.run
  end
end

#runvoid

This method is abstract.

This method returns an undefined value.

Performs the actual execution of the command.



60
61
# File 'lib/nanoc3/cli/command.rb', line 60

def run
end

#siteNanoc3::Site

Gets the site (Site instance) in the current directory and loads its data.

Returns:

  • (Nanoc3::Site)

    The site in the current working directory



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nanoc3/cli/command.rb', line 67

def site
  # Load site if possible
  @site ||= nil
  if File.file?('config.yaml') && @site.nil?
    begin
      @site = Nanoc3::Site.new('.')
    rescue Nanoc3::Errors::UnknownDataSource => e
      $stderr.puts "Unknown data source: #{e}"
      exit 1
    end
  end

  @site
end