vli 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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:
@@ -1,7 +1,6 @@
1
1
  module Vli
2
2
  module Error
3
-
3
+ class CLIInvalidOptions < StandardError; end
4
4
  class VliInterrupt < StandardError; end
5
-
6
5
  end
7
6
  end
@@ -1,3 +1,3 @@
1
1
  module Vli
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: