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'].freeze
STACKS =
['ruby', 'python', 'node'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgumentProcessor

Returns a new instance of ArgumentProcessor.



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

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.



8
9
10
# File 'lib/replr/argument_processor.rb', line 8

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/replr/argument_processor.rb', line 8

def command
  @command
end

#librariesObject (readonly)

Returns the value of attribute libraries.



8
9
10
# File 'lib/replr/argument_processor.rb', line 8

def libraries
  @libraries
end

#stackObject (readonly)

Returns the value of attribute stack.



8
9
10
# File 'lib/replr/argument_processor.rb', line 8

def stack
  @stack
end

Instance Method Details

#check_argument_length!Object



16
17
18
19
20
21
# File 'lib/replr/argument_processor.rb', line 16

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

#check_arguments!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/replr/argument_processor.rb', line 23

def check_arguments!
  detect_stack
  detect_command
  detect_libraries

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

#detect_commandObject



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

def detect_command
  @command = COMMANDS.detect do |command|
    arguments[0] == command
  end
end

#detect_librariesObject



55
56
57
# File 'lib/replr/argument_processor.rb', line 55

def detect_libraries
  @libraries = arguments[1..-1].sort!
end

#detect_stackObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/replr/argument_processor.rb', line 37

def detect_stack
  detected_stack = STACKS.detect do |stack|
    arguments[0].match(/^#{stack}:?.*?/)
  end

  # return the full stack string (that includes version)
  # instead of just the stack name
  if detected_stack
    @stack = arguments[0]
  end
end

#puts_error(string) ⇒ Object



67
68
69
# File 'lib/replr/argument_processor.rb', line 67

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

#puts_usageObject



59
60
61
62
63
64
65
# File 'lib/replr/argument_processor.rb', line 59

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 one of #{STACKS.join(', ')}"
  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