-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath
More file actions
executable file
·68 lines (49 loc) · 1.42 KB
/
path
File metadata and controls
executable file
·68 lines (49 loc) · 1.42 KB
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
63
64
65
66
67
68
#!/usr/bin/env ruby -w
# Present and manipulate paths.
require 'optparse'
class Path
def initialize(var_name, *args)
path_string = args.empty? ? ENV[var_name] : args.join(":")
@elements = path_string.split(/:/)
end
def clean
@elements.delete('')
@elements.uniq!
end
def show(separator)
puts @elements.join(separator)
end
end
def parse_args
options = {
var_name: 'PATH'
}
op = OptionParser.new do |opts|
opts.banner = <<-EOS.gsub(/^ */, '')
Usage: #{opts.program_name} [option]... [argument]...
If called with non-flag command-line arguments, assemble them into a
new PATH value and emit it. The current $PATH is not included unless
explicitly mentioned: this lets you control the prepending or
appending of new values.
In the absence of non-flag command-line arguments, emit the current
$PATH.
EOS
opts.on("-c", "--clean", "Clean path, removing any dupes.") do
options[:clean] = true
end
opts.on("-m", "--manpath", "Examine MANPATH rather than PATH.") do
options[:var_name] = 'MANPATH'
end
opts.on("-p", "--pack", "Pack path into one long line.") do
options[:pack] = true
end
end
args = op.parse!
[args, options]
end
# ------------------
args, options = parse_args
separator = options[:pack] ? ":" : "\n"
path = Path.new(options[:var_name], *args)
path.clean if options[:clean]
path.show(separator)