Class: Arli::CLI::CommandFinder
- Inherits:
-
Object
- Object
- Arli::CLI::CommandFinder
- Includes:
- Helpers::Output
- Defined in:
- lib/arli/cli/command_finder.rb
Constant Summary
Constants included from Helpers::Output
Helpers::Output::CHAR_FAILURE, Helpers::Output::CHAR_SUCCESS
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#command ⇒ Object
Returns the value of attribute command.
-
#command_name ⇒ Object
Returns the value of attribute command_name.
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #detect_command ⇒ Object
- #factory ⇒ Object
-
#initialize(argv, config: Arli.config) ⇒ CommandFinder
constructor
A new instance of CommandFinder.
- #instantiate_command ⇒ Object
- #non_flag_argument? ⇒ Boolean
- #parse! ⇒ Object
- #parse_command_arguments!(cmd) ⇒ Object
Methods included from Helpers::Output
#___, #__p, #__pf, #__pt, #abort?, #action_fail, #action_ok, #backup?, #cursor, #debug, #debug?, disable!, enable!, enabled?, #error, #fuck, #header, #hr, #indent_cursor, #info, #ok, #overwrite?, #print_action_failure, #print_action_starting, #print_action_success, #print_target_dir, #quiet?, #raise_invalid_arli_command!, #report_exception, #verbose?
Constructor Details
#initialize(argv, config: Arli.config) ⇒ CommandFinder
Returns a new instance of CommandFinder.
21 22 23 24 |
# File 'lib/arli/cli/command_finder.rb', line 21 def initialize(argv, config: Arli.config) self.config = config self.argv = argv end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
19 20 21 |
# File 'lib/arli/cli/command_finder.rb', line 19 def argv @argv end |
#command ⇒ Object
Returns the value of attribute command.
19 20 21 |
# File 'lib/arli/cli/command_finder.rb', line 19 def command @command end |
#command_name ⇒ Object
Returns the value of attribute command_name.
19 20 21 |
# File 'lib/arli/cli/command_finder.rb', line 19 def command_name @command_name end |
#config ⇒ Object
Returns the value of attribute config.
19 20 21 |
# File 'lib/arli/cli/command_finder.rb', line 19 def config @config end |
Instance Method Details
#detect_command ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/arli/cli/command_finder.rb', line 39 def detect_command return nil unless non_flag_argument? cmd = argv.shift.to_sym if factory.valid_command?(cmd) factory.command_from_arg(cmd) else raise_invalid_arli_command!(cmd) end end |
#factory ⇒ Object
69 70 71 |
# File 'lib/arli/cli/command_finder.rb', line 69 def factory Arli::CLI::ParserFactory end |
#instantiate_command ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/arli/cli/command_finder.rb', line 54 def instantiate_command self.command_name ||= detect_command begin name = command_name.to_s.capitalize.to_sym command_class = ::Arli::Commands.const_get(name) raise_invalid_arli_command!(command_name) unless command_class command_class.new(config: config) if command_class end end |
#non_flag_argument? ⇒ Boolean
65 66 67 |
# File 'lib/arli/cli/command_finder.rb', line 65 def non_flag_argument? argv.first && argv.first !~ /^-.*$/ end |
#parse! ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/arli/cli/command_finder.rb', line 26 def parse! self.command_name = detect_command parse_command_arguments!(command_name) unless Arli.config.help self.command = instantiate_command if self.command config.runtime.command.instance = command config.runtime.command.name = command_name end end self end |
#parse_command_arguments!(cmd) ⇒ Object
49 50 51 52 |
# File 'lib/arli/cli/command_finder.rb', line 49 def parse_command_arguments!(cmd) parser = factory.command_parser(cmd) factory.parse_argv(parser, argv) if parser end |