-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_op_v5.rb
More file actions
37 lines (33 loc) · 905 Bytes
/
file_op_v5.rb
File metadata and controls
37 lines (33 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'thor'
class FileOp < Thor
class_option :verbose, :type => :boolean
desc 'output FILE_NAME', 'print out the contents of FILE_NAME'
option :stderr, :type => :boolean
def output(file_name)
log("Starting to read file...")
#options[:stderr] is either true or false depending
#on whether or not --stderr was passed
contents = File.read(file_name)
log("File contents:")
if options[:stderr]
log("(in stderr)")
$stderr.puts contents
else
log("(in stdout)")
$stdout.puts contents
end
end
no_commands do
def log(str)
puts str if options[:verbose]
end
end
desc 'touch FILE_NAME', 'creates an empty file named FILE_NAME'
option :chmod, :type => :numeric
def touch(file_name)
log("Touching file...")
f = File.new(file_name, "w")
f.chmod(options[:chmod]) if options[:chmod]
end
end
FileOp.start(ARGV)