Module: CommandKit::Open
Overview
Adds helper methods for opening files.
Features
- Prints
No such file or directory
error if the given file does not exist. - Handles
-
file paths which indicate input should be read from STDIN or output written to STDOUT.
Examples
include CommandKit::Open
def run(path)
open(path) do |file|
# ...
end
end
Constant Summary
Constants included from Printing
Instance Method Summary collapse
-
#open(path, mode = 'r') {|file| ... } ⇒ File, ...
Opens a file for reading or writing.
Methods included from Printing
#print_error, #print_exception
Methods included from Stdio
#abort, #gets, #initialize, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Instance Method Details
#open(path, mode = 'r') {|file| ... } ⇒ File, ...
Opens a file for reading or writing.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/command_kit/open.rb', line 82 def open(path,mode='r',&block) if path == '-' io = case mode when /[wa]/ then stdout else stdin end if block_given? return yield(io) else return io end end begin File.open(path,mode,&block) rescue Errno::ENOENT print_error "No such file or directory: #{path}" exit(1) end end |