-
Notifications
You must be signed in to change notification settings - Fork 3
/
foo.rb
34 lines (30 loc) · 971 Bytes
/
foo.rb
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
require "pmgmt"
$pm = Pmgmt.new
def foo(command_name, *args)
op = Pmgmt.OptionsParser.new(command_name, args)
op.add_typed_option(
"--iterations=[iterations]",
Integer,
->(opts, v) {
raise ArgumentError.new("Only 7 iterations are allowed.") unless v == 7
opts.iterations = v
},
"The value of `iterations`, which must be ignored."
)
op.add_option(
"--ignore",
->(opts, v) { opts.ignore = true },
"Whether to ignore the value of `iterations`."
)
op.add_validator ->(opts) {
if opts.iterations
raise ArgumentError.new("`iterations` must be ignored.") unless opts.ignore
end
}
op.parse.validate
$pm.run_inline %W{echo Don't tell anyone about the fizbit.}, redact: "fizbit"
$pm.run_inline %W{echo You ran:} + [command_name] + args
$pm.status "Iterations was: #{op.opts.iterations}"
end
Pmgmt.register_command(:foo)
# alternatively: Pmgmt.register_command({:invocation => "foo", :fn => :foo})