-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl
executable file
·62 lines (45 loc) · 1.03 KB
/
dl
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby -w
# dl: directory list.
#
# Take a collection of paths from either stdin or a collection of files.
#
# Group items by their parent directories.
require 'optparse'
class PathSet
def initialize
@paths = Hash.new { |hash, key| hash[key] = [] }
end
def add(path)
@paths[File.dirname(path)] << File.basename(path)
end
def show(dirs_only = false)
@paths.sort.each do |dir, files|
puts dir
unless dirs_only
files.each do |file|
puts " #{file}"
end
puts
end
end
end
end
def parse_args
options = {}
op = OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [option]... [argument]..."
options[:dirs_only] = false
opts.on("-d", "--dirs_only", "Show only the directory components.") do
options[:dirs_only] = true
end
end
op.parse!
options
end
# ------------------
options = parse_args
pathset = PathSet.new()
ARGF.each_line do |line|
pathset.add(line.chomp)
end
pathset.show(options[:dirs_only])