Class: S3Antivirus::Completer
- Inherits:
-
Object
- Object
- S3Antivirus::Completer
- Defined in:
- lib/s3_antivirus/completer.rb
Defined Under Namespace
Classes: Script
Instance Method Summary collapse
-
#all_commands ⇒ Object
all top-level commands.
- #command_params(raw = false) ⇒ Object
- #found?(command) ⇒ Boolean
-
#initialize(command_class, *params) ⇒ Completer
constructor
A new instance of Completer.
-
#log(msg) ⇒ Object
Useful for debugging.
- #options_completion ⇒ Object
- #params_completion ⇒ Object
- #run ⇒ Object
- #subcommand?(command) ⇒ Boolean
-
#thor_group_command? ⇒ Boolean
hacky way to detect that command is a registered Thor::Group command.
Constructor Details
#initialize(command_class, *params) ⇒ Completer
Returns a new instance of Completer.
73 74 75 76 77 |
# File 'lib/s3_antivirus/completer.rb', line 73 def initialize(command_class, *params) @params = params @current_command = @params[0] @command_class = command_class # CLI initiall end |
Instance Method Details
#all_commands ⇒ Object
all top-level commands
117 118 119 120 121 122 |
# File 'lib/s3_antivirus/completer.rb', line 117 def all_commands commands = @command_class.all_commands.reject do |k,v| v.is_a?(Thor::HiddenCommand) end commands.keys end |
#command_params(raw = false) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/s3_antivirus/completer.rb', line 124 def command_params(raw=false) params = @command_class.instance_method(@current_command).parameters # Example: # >> Sub.instance_method(:goodbye).parameters # => [[:req, :name]] # >> raw ? params : params.map!(&:last) end |
#found?(command) ⇒ Boolean
111 112 113 114 |
# File 'lib/s3_antivirus/completer.rb', line 111 def found?(command) public_methods = @command_class.public_instance_methods(false) command && public_methods.include?(command.to_sym) end |
#log(msg) ⇒ Object
Useful for debugging. Using puts messes up completion.
153 154 155 156 157 |
# File 'lib/s3_antivirus/completer.rb', line 153 def log(msg) File.open("/tmp/complete.log", "a") do |file| file.puts(msg) end end |
#options_completion ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/s3_antivirus/completer.rb', line 139 def used = ARGV.select { |a| a.include?('--') } # so we can remove used options = @command_class.all_commands[@current_command]..keys = @command_class..keys = + + ['help'] .map! { |o| "--#{o.to_s.gsub('_','-')}" } = - used .uniq end |
#params_completion ⇒ Object
133 134 135 136 137 |
# File 'lib/s3_antivirus/completer.rb', line 133 def params_completion offset = @params.size - 1 offset_params = command_params[offset..-1] command_params[offset..-1].first end |
#run ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/s3_antivirus/completer.rb', line 79 def run if subcommand?(@current_command) subcommand_class = @command_class.subcommand_classes[@current_command] @params.shift # destructive Completer.new(subcommand_class, *@params).run # recursively use subcommand return end # full command has been found! unless found?(@current_command) puts all_commands return end # will only get to here if command aws found (above) arity = @command_class.instance_method(@current_command).arity.abs if @params.size > arity or thor_group_command? puts else puts params_completion end end |
#subcommand?(command) ⇒ Boolean
102 103 104 |
# File 'lib/s3_antivirus/completer.rb', line 102 def subcommand?(command) @command_class.subcommands.include?(command) end |
#thor_group_command? ⇒ Boolean
hacky way to detect that command is a registered Thor::Group command
107 108 109 |
# File 'lib/s3_antivirus/completer.rb', line 107 def thor_group_command? command_params(raw=true) == [[:rest, :args]] end |