require 'pathname' module FeduxOrg module Stdlib module Command module Which # Search for command # @param [String] cmd # name of command or path to command (will be reduced to basename and then searched in PATH) # # @param [Array,String] paths # a string containing paths separated by "File::PATH_SEPARATOR" or an array of paths # # @param [Array,String] pathexts # a string containing pathexts separated by ";" or an array of pathexts # # @return [String] # path to command def which(cmd, paths=ENV['PATH'].split(File::PATH_SEPARATOR), pathexts=ENV['PATHEXT'].to_s.split( /;/ ) ) cmd = Pathname.new( cmd.to_s ) return nil if cmd.to_s.empty? if cmd.absolute? return cmd.to_s if cmd.executable? return nil end pathexts = [''] if pathexts.blank? Array( paths ).each do |path| Array( pathexts ).each do |ext| file = Pathname.new( File.join(path, "#{cmd.to_s}#{ext.to_s}") ) return file.to_s if file.executable? end end nil end end end end end