vli 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/vli/command/base.rb +32 -0
- data/lib/vli/error.rb +1 -2
- data/lib/vli/version.rb +1 -1
- metadata +1 -1
data/lib/vli/command/base.rb
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
module Vli
|
6
6
|
module Command
|
7
7
|
class Base
|
8
|
+
include Util::SafePuts
|
8
9
|
|
9
10
|
def initialize(argv, env)
|
10
11
|
@argv = argv
|
@@ -13,6 +14,37 @@ module Vli
|
|
13
14
|
|
14
15
|
def execute; end
|
15
16
|
|
17
|
+
# Parses the options given an OptionParser instance.
|
18
|
+
#
|
19
|
+
# This is a convenience method that properly handles duping the
|
20
|
+
# originally argv array so that it is not destroyed.
|
21
|
+
#
|
22
|
+
# This method will also automatically detect "-h" and "--help"
|
23
|
+
# and print help. And if any invalid options are detected, the help
|
24
|
+
# will be printed, as well.
|
25
|
+
#
|
26
|
+
# If this method returns `nil`, then you should assume that help
|
27
|
+
# was printed and parsing failed.
|
28
|
+
def parse_options(opts=nil)
|
29
|
+
# Creating a shallow copy of the arguments so the OptionParser
|
30
|
+
# doesn't destroy the originals.
|
31
|
+
argv = @argv.dup
|
32
|
+
|
33
|
+
# Default opts to a blank optionparser if none is given
|
34
|
+
opts ||= OptionParser.new
|
35
|
+
|
36
|
+
# Add the help option, which must be on every command.
|
37
|
+
opts.on_tail("-h", "--help", "Print this help") do
|
38
|
+
safe_puts(opts.help)
|
39
|
+
return nil
|
40
|
+
end
|
41
|
+
|
42
|
+
opts.parse!(argv)
|
43
|
+
return argv
|
44
|
+
rescue OptionParser::InvalidOption
|
45
|
+
raise Error::CLIInvalidOptions, :help => opts.help.chomp
|
46
|
+
end
|
47
|
+
|
16
48
|
# This method will split the argv given into three parts: the
|
17
49
|
# flags to this command, the subcommand, and the flags to the
|
18
50
|
# subcommand. For example:
|
data/lib/vli/error.rb
CHANGED
data/lib/vli/version.rb
CHANGED