-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomnitool.rb
More file actions
executable file
·155 lines (122 loc) · 4.41 KB
/
omnitool.rb
File metadata and controls
executable file
·155 lines (122 loc) · 4.41 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
#require 'byebug'
# Copyright(c) 2011, Gary D. Foster <gary.foster@gmail.com>
# This code is released under the MIT license.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
$extra_options = []
class Tool
@@help_text = {}
@@setupHook = []
private
Kernel.send :define_method, :option do |*args, &block|
short = args[0].to_s
if args.last.kind_of? Hash
hints = args.last
else
hints = { :description => "" }
end
if args[1].kind_of? Hash
long = ""
else
long = args[1].to_s
end
$extra_options << [short, long, block, hints]
end
Kernel.send :define_method, :setup do |&block|
@@setupHook << block
end
protected
Kernel.send :define_method, :command do |command, help_text, &block|
@@help_text[command.to_s] = help_text
Tool.send :define_method, command, &block
end
public
def initialize(options=nil)
@options = options
@@setupHook.each { |step| self.instance_eval(&step) }
return self
end
# default commands - always have a help command
command :help, "Print this list" do
cmd_list = @@help_text.keys.sort
puts "list of supported commands: "
help_output = cmd_list.collect do |cmd|
cmd + (@@help_text.include?(cmd) ? "\t\t" + @@help_text[cmd] : "")
end
puts help_output.join("\n")
end
# load our chameleon command file based upon our own name
cmd_file = "#{ENV['HOME']}/.ot_cmds/" << File.basename($0) << ".cmd"
system_cmd_file = "/var/lib/omnitool/ot_cmds" << File.basename($0) << ".cmd"
if FileTest.exists?(cmd_file)
load cmd_file
elsif FileTest.exists?(system_cmd_file)
load system_cmd_file
else
puts "Could not find a command file for myself in #{cmd_file} or #{system_cmd_file}"
exit 1
end
end
def main()
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage #{File.basename($0)} [options] ..."
$extra_options.each do |short, long, block, hints|
# name each option after the long name, falling back to the short name if not given
if long.empty?
key = short.to_sym
long_switch = nil
else
key = long.to_sym
long_switch = "--#{long} #{long.to_s.upcase}"
end
description = hints[:description]
if hints.include?(:switch) and hints[:switch]
opts.on("-#{short}", long_switch, description) do
options[key] = true
end
else
opts.on("-#{short}", long_switch, description) do |param|
if block.nil?
options[key] = param
else
options[key] = block.call
end
end
end
end
# and we always have a default -h switch
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
command = ARGV[0] || "help"
tool = Tool.new(options)
if tool.respond_to?(command)
tool.send(command)
else
puts "Unknown command: #{ARGV[0]}, try 'help'"
exit 1
end
end
if $0 == __FILE__
main
end