class Abt::Cli
Attributes
command[R]
err_output[R]
input[R]
output[R]
prompt[R]
remaining_args[R]
Public Class Methods
new(argv: ARGV, input: $stdin, output: $stdout, err_output: $stderr)
click to toggle source
# File lib/abt/cli.rb, line 15 def initialize(argv: ARGV, input: $stdin, output: $stdout, err_output: $stderr) (@command, *@remaining_args) = argv @input = input @output = output @err_output = err_output @prompt = Abt::Cli::Prompt.new(output: err_output) end
Public Instance Methods
abort(message)
click to toggle source
# File lib/abt/cli.rb, line 53 def abort(message) raise Abort, message end
aris()
click to toggle source
# File lib/abt/cli.rb, line 61 def aris @aris ||= ArgumentsParser.new(sanitized_piped_args + remaining_args).parse end
directory_config()
click to toggle source
# File lib/abt/cli.rb, line 65 def directory_config @directory_config ||= Abt::DirectoryConfig.new end
exit_with_message(message)
click to toggle source
# File lib/abt/cli.rb, line 57 def exit_with_message(message) raise Exit, message end
perform()
click to toggle source
# File lib/abt/cli.rb, line 23 def perform if command.nil? warn("No command specified, printing help\n\n") @command = "help" end return process_alias if alias? return process_global_command if global_command? process_aris end
print(*args)
click to toggle source
# File lib/abt/cli.rb, line 49 def print(*args) output.print(*args) end
print_ari(scheme, path, description = nil)
click to toggle source
# File lib/abt/cli.rb, line 35 def print_ari(scheme, path, description = nil) command = "#{scheme}:#{path}" command += " # #{description}" unless description.nil? output.puts command end
puts(*args)
click to toggle source
# File lib/abt/cli.rb, line 45 def puts(*args) output.puts(*args) end
warn(*args)
click to toggle source
# File lib/abt/cli.rb, line 41 def warn(*args) err_output.puts(*args) end
Private Instance Methods
alias?()
click to toggle source
# File lib/abt/cli.rb, line 71 def alias? command[0] == "@" end
get_command_class(scheme)
click to toggle source
# File lib/abt/cli.rb, line 157 def get_command_class(scheme) provider = Abt.scheme_provider(scheme) return nil if provider.nil? provider.command_class(command) end
global_command?()
click to toggle source
# File lib/abt/cli.rb, line 88 def global_command? return true if aris.empty? return true if aris.first.scheme.nil? false end
print_command(name, ari)
click to toggle source
# File lib/abt/cli.rb, line 164 def print_command(name, ari) warn("===== #{name.upcase} #{ari} =====") end
process_alias()
click to toggle source
# File lib/abt/cli.rb, line 75 def process_alias matching_alias = directory_config.dig("aliases", command[1..-1]) abort("No such alias #{command}") if matching_alias.nil? with_args = matching_alias.sub("$@", remaining_args.join(" ")) with_program_name = with_args.gsub("$0", $PROGRAM_NAME).strip humanized = with_args.gsub("$0", "abt").strip warn(humanized) system(with_program_name) end
process_ari(ari)
click to toggle source
# File lib/abt/cli.rb, line 143 def process_ari(ari) command_class = get_command_class(ari.scheme) return false if command_class.nil? print_command(command, ari) if output.isatty begin command_class.new(ari: ari, cli: self).perform rescue Exit => e puts e.message end true end
process_aris()
click to toggle source
# File lib/abt/cli.rb, line 126 def process_aris used_schemes = [] aris.each do |ari| if used_schemes.include?(ari.scheme) warn("Dropping command for already used scheme: #{ari}") next end used_schemes << ari.scheme if process_ari(ari) end return unless used_schemes.empty? && output.isatty abort("No providers found for command and ARI(s)") end
process_global_command()
click to toggle source
# File lib/abt/cli.rb, line 95 def process_global_command command_class = GlobalCommands.command_class(command) abort("No such global command: #{command}, perhaps you forgot to add an ARI?") if command_class.nil? begin ari = aris.first || Abt::Ari.new command_class.new(cli: self, ari: ari).perform rescue Exit => e puts e.message end end
sanitized_piped_args()
click to toggle source
# File lib/abt/cli.rb, line 108 def sanitized_piped_args return [] if input.isatty @sanitized_piped_args ||= begin input_string = input.read.strip abort("No input from pipe") if input_string.nil? || input_string.empty? # Exclude comment part of piped input lines lines_without_comments = input_string.lines.map { |line| line.split(" # ").first } # Allow multiple ARIs on a single piped input line # TODO: Force the user to pick a single ARI joined_lines = lines_without_comments.join(" ").strip joined_lines.split(/\s+/) end end