R
RichardOnRails
Hi,
Search on WindowsXP/SP2 didn't seem to locate a file is looking for.
As someone relatively new to Ruby, I wonder whether there are any
Rubyisms I could have used to cut down the the 80+ lines I took to
accomplish this task, given the USAGE requirement I posed in the
code.
Thanks in Advance,
Richard
# Search.rb
# K:/_Projects/Ruby/_Ruby_Tests/TestSearchDir/SearchWithArgs
# USAGE: Search.rb -d DirPath [-f regexpFileNameSought] [-t
regexpTextSought]
require 'find'
def Match(path, reTxt)
open(path) { |f|
f.each_line { |line|
if (line =~ reTxt)
puts "MATCH found in following line"
puts line
return true
end
}
}
return false
end
def ShowQuit(msg, arg = '')
puts "ERROR " + "="*10,
msg + ": " + arg,
"="*16
exit
end
argPrefix = '-'
argFlags = %{d f t} # reDirectory, reFilename, reText
argVals=Hash.new
#args = "-d K:/_Projects/Ruby/_Ruby_Tests -f search.*[.]rb$ -t
curArg".split(' ')
args = "-d K:/_Projects/Ruby/_Ruby_Tests -t curArg".split(' ')
puts args.join(' ')
ix = 0
while( ix<args.size ) do
curArg = args[ix]
# Is current arg. a valid flag?
ShowQuit("Invalid flag", curArg) unless
(curArg.size==2 && argFlags.include?(curArg[1,1]))
# Has the current argument been seen?
if (argVals.keys.include?(curArg))
ShowQuit("Duplicate flag", curArg)
end
# Initialize hash for this flag; increment index;
argVals[curArg] = nil
ix += 1
# If argument value follows flag and is not, itself, a flag
if (args[ix] && args[ix][0,1] != argPrefix)
argVals[curArg] = args[ix]
ix += 1
end
end
puts "Succes in getting arguments!"
p argVals
ShowQuit("No dirname(-d) argument") unless argVals['-d']
ShowQuit("Dirname(-d) argument does not identify a directory") unless
File.directory?(argVals['-d'] )
if (reFnIn = argVals['-f'])
reFn = Regexp.new(reFnIn, Regexp::IGNORECASE)
ShowQuit("Invalid filename expression") unless
reFn.class.to_s=='Regexp'
end
if (reTxtIn = argVals['-t'])
reTxt = Regexp.new(reTxtIn, Regexp::IGNORECASE)
ShowQuit("Invalid text expression") unless reTxt.class.to_s=='Regexp'
end
Find.find(argVals['-d']) { |path|
fbn = File.basename(path)
next if(fbn =~ /^[.][.]?$/)
next if(reFnIn && fbn !~ reFn)
if ( argVals['-t'] && File.file?(path) )
puts path if Match(path, reTxt)
end
}
Search on WindowsXP/SP2 didn't seem to locate a file is looking for.
As someone relatively new to Ruby, I wonder whether there are any
Rubyisms I could have used to cut down the the 80+ lines I took to
accomplish this task, given the USAGE requirement I posed in the
code.
Thanks in Advance,
Richard
# Search.rb
# K:/_Projects/Ruby/_Ruby_Tests/TestSearchDir/SearchWithArgs
# USAGE: Search.rb -d DirPath [-f regexpFileNameSought] [-t
regexpTextSought]
require 'find'
def Match(path, reTxt)
open(path) { |f|
f.each_line { |line|
if (line =~ reTxt)
puts "MATCH found in following line"
puts line
return true
end
}
}
return false
end
def ShowQuit(msg, arg = '')
puts "ERROR " + "="*10,
msg + ": " + arg,
"="*16
exit
end
argPrefix = '-'
argFlags = %{d f t} # reDirectory, reFilename, reText
argVals=Hash.new
#args = "-d K:/_Projects/Ruby/_Ruby_Tests -f search.*[.]rb$ -t
curArg".split(' ')
args = "-d K:/_Projects/Ruby/_Ruby_Tests -t curArg".split(' ')
puts args.join(' ')
ix = 0
while( ix<args.size ) do
curArg = args[ix]
# Is current arg. a valid flag?
ShowQuit("Invalid flag", curArg) unless
(curArg.size==2 && argFlags.include?(curArg[1,1]))
# Has the current argument been seen?
if (argVals.keys.include?(curArg))
ShowQuit("Duplicate flag", curArg)
end
# Initialize hash for this flag; increment index;
argVals[curArg] = nil
ix += 1
# If argument value follows flag and is not, itself, a flag
if (args[ix] && args[ix][0,1] != argPrefix)
argVals[curArg] = args[ix]
ix += 1
end
end
puts "Succes in getting arguments!"
p argVals
ShowQuit("No dirname(-d) argument") unless argVals['-d']
ShowQuit("Dirname(-d) argument does not identify a directory") unless
File.directory?(argVals['-d'] )
if (reFnIn = argVals['-f'])
reFn = Regexp.new(reFnIn, Regexp::IGNORECASE)
ShowQuit("Invalid filename expression") unless
reFn.class.to_s=='Regexp'
end
if (reTxtIn = argVals['-t'])
reTxt = Regexp.new(reTxtIn, Regexp::IGNORECASE)
ShowQuit("Invalid text expression") unless reTxt.class.to_s=='Regexp'
end
Find.find(argVals['-d']) { |path|
fbn = File.basename(path)
next if(fbn =~ /^[.][.]?$/)
next if(reFnIn && fbn !~ reFn)
if ( argVals['-t'] && File.file?(path) )
puts path if Match(path, reTxt)
end
}