Class: Replr::ArgumentProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/replr/argument_processor.rb

Overview

Processes command-line arguments

Constant Summary collapse

COMMANDS =
['prune']
STACKS =
['ruby']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgumentProcessor

Returns a new instance of ArgumentProcessor.



12
13
14
15
16
# File 'lib/replr/argument_processor.rb', line 12

def initialize
  @arguments = ARGV.map { |argument| argument.downcase.strip }
  check_argument_length!
  check_arguments!
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



10
11
12
# File 'lib/replr/argument_processor.rb', line 10

def arguments
  @arguments
end

Instance Method Details

#check_argument_length!Object



18
19
20
21
22
23
# File 'lib/replr/argument_processor.rb', line 18

def check_argument_length!
  if arguments.empty?
    puts_usage
    exit
  end
end

#check_arguments!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/replr/argument_processor.rb', line 25

def check_arguments!
  valid_stack = STACKS.detect do |stack|
    arguments[0].match(/^#{stack}:?.*?/)
  end
  valid_command = COMMANDS.detect do |command|
    arguments[0] == command
  end

  unless valid_stack or valid_command
    puts_error "First argument must be either be a command:
\t#{COMMANDS.join(' ')}
or one of a supported stack:
\t#{STACKS.join(' ')}"
    puts_usage
    exit
  end
end

#puts_error(string) ⇒ Object



51
52
53
# File 'lib/replr/argument_processor.rb', line 51

def puts_error(string)
  STDERR.puts(string)
end

#puts_usageObject



43
44
45
46
47
48
49
# File 'lib/replr/argument_processor.rb', line 43

def puts_usage
  puts_error "\nUsage: replr <stack> <libraries...>\n\n"
  puts_error "A single line REPL for your favorite languages & libraries\n\n"
  puts_error "\t<stack> is now only 'ruby'"
  puts_error "\t<libraries...> is a space separated list of libraries for the stack\n\n"
  puts_error "More commands:\n\n\treplr prune to delete all replr docker images (this saves space)"
end