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

- (Command) initialize

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

+ (String) name



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

def self.name
  nil
end

+ (void) validate_file(file)

This method returns an undefined value.

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



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

- (String) help

Returns a help message for the individual command.



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

def help
  @slop.help
end

- (void) run(cmdline)

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.



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