Class: RikaCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/rika/cli/rika_command.rb

Overview

This command line application enables the parsing of documents on the command line. Syntax is:

rika [options] <file or url> [...file or url...]

Run with -h or –help option for more details.

Defaults to outputting both content (text) and metadata, but the -t and -m flags can be used to enable or suppress either. Supports output formats of JSON, Pretty JSON, YAML, Awesome Print, to_s, and inspect (see Formatters class).

Constant Summary collapse

FORMAT_DESCRIPTIONS =
Hash.new('Unknown').merge(
  'a' => 'AwesomePrint',
  'i' => 'inspect',
  'j' => 'JSON',
  'J' => 'Pretty JSON',
  't' => 'to_s',
  'y' => 'YAML'
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ RikaCommand

Returns a new instance of RikaCommand.

Parameters:

  • (defaults to: ARGV)

    command line arguments; default to ARGV but may be overridden for testing



41
42
43
44
45
46
# File 'lib/rika/cli/rika_command.rb', line 41

def initialize(args = ARGV)
  # Dup the array in case it has been frozen. The array will be modified later when options are parsed
  # and removed, and when directories are removed, so this array should not be frozen.
  @args = args.dup
  @bad_targets = Hash.new { |hash, key| hash[key] = [] }
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def args
  @args
end

#bad_targetsObject (readonly)

Returns the value of attribute bad_targets.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def bad_targets
  @bad_targets
end

#help_textObject (readonly)

Returns the value of attribute help_text.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def help_text
  @help_text
end

#metadata_formatterObject (readonly)

Returns the value of attribute metadata_formatter.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def 
  
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def options
  @options
end

#targetsObject (readonly)

Returns the value of attribute targets.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def targets
  @targets
end

#text_formatterObject (readonly)

Returns the value of attribute text_formatter.



29
30
31
# File 'lib/rika/cli/rika_command.rb', line 29

def text_formatter
  @text_formatter
end

Class Method Details

.output_help_text(help_text, error_message = nil) ⇒ void

This method returns an undefined value.

Outputs help text to stdout

Parameters:

  • The help text to display

  • (defaults to: nil)

    Optional error message to display on stderr before the help text



35
36
37
38
# File 'lib/rika/cli/rika_command.rb', line 35

def self.output_help_text(help_text, error_message = nil)
  $stderr.puts(error_message) if error_message
  puts help_text
end

Instance Method Details

#callInteger

Main method and entry point for this class’ work.

Returns:

  • exit code (0 for success, non-zero for errors)



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rika/cli/rika_command.rb', line 50

def call
  prepare
  report_and_exit_if_no_targets_specified
  
  if options[:dry_run]
    display_dry_run_info
    return 0
  end
  
  process_targets
  report_bad_targets
  bad_targets.values.flatten.empty? ? 0 : 1
end