Class: Replr::ArgumentProcessor
- Inherits:
-
Object
- Object
- Replr::ArgumentProcessor
- 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
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#libraries ⇒ Object
readonly
Returns the value of attribute libraries.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #check_argument_length! ⇒ Object
- #check_arguments! ⇒ Object
- #detect_command ⇒ Object
- #detect_libraries ⇒ Object
- #detect_stack ⇒ Object
-
#initialize ⇒ ArgumentProcessor
constructor
A new instance of ArgumentProcessor.
- #puts_error(string) ⇒ Object
- #puts_usage ⇒ Object
Constructor Details
#initialize ⇒ ArgumentProcessor
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
8 9 10 |
# File 'lib/replr/argument_processor.rb', line 8 def arguments @arguments end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
8 9 10 |
# File 'lib/replr/argument_processor.rb', line 8 def command @command end |
#libraries ⇒ Object (readonly)
Returns the value of attribute libraries.
8 9 10 |
# File 'lib/replr/argument_processor.rb', line 8 def libraries @libraries end |
#stack ⇒ Object (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_command ⇒ Object
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_libraries ⇒ Object
55 56 57 |
# File 'lib/replr/argument_processor.rb', line 55 def detect_libraries @libraries = arguments[1..-1].sort! end |
#detect_stack ⇒ Object
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_usage ⇒ Object
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 |