Class: RCite::CLI::Command Abstract

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

Overview

This class is abstract.

Represents an RCite command. RCite uses the same structure for its CLI as gem, git etc., i.e. it has multiple commands that correspond to various actions. For example, rcite cite will cite a text, while rcite bib will create a bibliography entry.

This class provides helper methods that establish a framework for individual commands. Each of those has its own class that is a child of this class.

Child classes are expected to implement the following methods:

  • ::name -- Returns the command's name. See class method name.
  • #setup_slop -- Creates a new Slop instance and defines the options that may be used with the command. Sets the @slop instance variable accordingly.
  • #run! -- Runs the command after all options have been parsed and their validity has been checked.

Direct Known Subclasses

BibCommand, CiteCommand, ProcessCommand

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Calls the child's setup_slop method. See RCite::CLI::Command for details.



35
36
37
# File 'lib/rcite/cli/command.rb', line 35

def initialize
  setup_slop # must be defined by child classes
end

Class Method Details

.nameString

Returns the command's name. This is used to identify the command the user is calling. For example, RCite will search for a command with name 'cite' when called as rcite cite. Individual commands must override this variable.

Returns:

  • (String)

    the command's name. This is used to identify the command the user is calling. For example, RCite will search for a command with name 'cite' when called as rcite cite. Individual commands must override this variable.



30
31
32
# File 'lib/rcite/cli/command.rb', line 30

def self.name
  nil
end

.validate_file(file) ⇒ void

This method returns an undefined value.

Checks if the given file exists and is readable. Logs an error and exits if not.

Parameters:

  • file (String, File)

    The file to check.



70
71
72
73
74
75
# File 'lib/rcite/cli/command.rb', line 70

def self.validate_file(file)
  unless File.exist?(file) && File.stat(file).readable?
    log.error("Could not read file: #{file}")
    exit 1
  end
end

Instance Method Details

#helpString

Returns a help message for the individual command.

Returns:

  • (String)

    a help message with usage information and a summary of the command's options.



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

def help
  @slop.help
end

#run(cmdline) ⇒ void

This method returns an undefined value.

Parses the given cmdline as a command line array (usually ARGV) and runs the child's run! method. See RCite::CLI::Command for details.

Parameters:

  • cmdline (Array)

    An array of strings, one for each element of the command line. You will usually want to use ARGV here.



45
46
47
48
49
50
51
52
53
54
# File 'lib/rcite/cli/command.rb', line 45

def run(cmdline)
  @cmdline = cmdline
  begin
    @slop.parse!(cmdline)
  rescue Slop::MissingOptionError => ex
    log.error(ex.message)
    exit 1
  end
  run! # must be defined by child classes
end