optparse-subcommand 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bodaniel Jeanes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # optparse-subcommand
2
+
3
+ This gem extends Ruby's standard library `OptionParser` class to allow popular
4
+ subcommand-style tools (e.g. git).
5
+
6
+ ## Usage
7
+
8
+ A new method `subcommand` is added to the `OptionParser` instance allowing you
9
+ to define an action to occur when that subcommand is detected as an argument.
10
+
11
+ The `subcommand` method takes a block which yields a new `OptionParser` instance.
12
+ Simply define your option parsing recursively for all subcommands.
13
+
14
+ Example:
15
+
16
+ ``` Ruby
17
+ require 'optparse/subcommand' # Also requires 'optparse'
18
+ require 'ostruct'
19
+
20
+ options = OpenStruct.new
21
+ options.verbose = false
22
+ options.action = nil
23
+ options.action_options = OpenStruct.new
24
+
25
+ parser = OptionParser.new do |opts|
26
+ opts.on '-v', '--[no-]verbose' do |verbose|
27
+ options.verbose = verbose
28
+ end
29
+
30
+ opts.subcommand 'foo' do |foo|
31
+ options.action = :foo
32
+
33
+ foo.on '-x' do
34
+ options.action_options.x = true
35
+ end
36
+ end
37
+ end
38
+
39
+ parser.parse(%w[-v foo -x]) # or ARGV, whatever...
40
+
41
+ options # => #<OpenStruct verbose=true, action=:foo, action_options=#<OpenStruct x=true>>
42
+ ```
43
+
44
+ ## License
45
+
46
+ See `LICENSE` file in this directory.
@@ -0,0 +1,35 @@
1
+ require 'optparse'
2
+
3
+ class OptionParser
4
+ module Subcommand
5
+ def subcommand(key, &block)
6
+ subcommand = lambda { OptionParser.new(&block) }
7
+ subcommands[key.to_s] = subcommand
8
+ end
9
+
10
+ private
11
+
12
+ def self.included(klass)
13
+ klass.instance_eval do
14
+ alias_method :parse_without_subcommand!, :parse!
15
+ alias_method :parse!, :parse_with_subcommand!
16
+ end
17
+ end
18
+
19
+ def parse_with_subcommand!(argv = default_argv)
20
+ subcommand = catch(:subcommand) do
21
+ return order!(argv) { |sub| throw :subcommand, sub }
22
+ end
23
+
24
+ sub_parser = subcommands[subcommand.to_s]
25
+ sub_parser.call.send(:parse!, argv) if sub_parser
26
+ end
27
+
28
+ def subcommands
29
+ @subcommands ||= {}
30
+ end
31
+ end
32
+
33
+ include Subcommand
34
+ end
35
+
@@ -0,0 +1,41 @@
1
+ require 'optparse/subcommand'
2
+
3
+ describe OptionParser do
4
+ def run(args)
5
+ options.parse(args.split(/\s+/))
6
+ hits
7
+ end
8
+
9
+ let(:hits) { [] }
10
+ let(:options) do
11
+ OptionParser.new do |opt|
12
+ opt.on '-t' do
13
+ hits << :t
14
+ end
15
+
16
+ opt.subcommand 'foo' do |foo|
17
+ hits << :foo
18
+
19
+ foo.on '-q' do
20
+ hits << :foo_q
21
+ end
22
+ end
23
+
24
+ opt.subcommand 'bar' do |bar|
25
+ hits << :bar
26
+
27
+ bar.on '-s' do
28
+ hits << :bar_s
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ specify { run("-t").should == [:t] }
35
+ specify { run("foo").should == [:foo] }
36
+ specify { run("foo -q").should == [:foo, :foo_q] }
37
+ specify { run("foo bar").should == [:foo] }
38
+ specify { run("-t foo").should == [:t, :foo] }
39
+
40
+ specify { lambda { run("foo -t") }.should raise_error(OptionParser::InvalidOption )}
41
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optparse-subcommand
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Bodaniel Jeanes
9
+ autorequire: optparse/subcommand
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70145307916660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70145307916660
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70145307915240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 2.9.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70145307915240
36
+ description: Add subcommand parsing to Ruby's OptionParser class
37
+ email: me@bjeanes.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - LICENSE
43
+ - README.md
44
+ - lib/optparse/subcommand.rb
45
+ - spec/optparse/subcommand_spec.rb
46
+ homepage: http://github.com/bjeanes/optparse-subcommand
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>'
62
+ - !ruby/object:Gem::Version
63
+ version: 1.3.1
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.10
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Add subcommand parsing to Ruby's OptionParser class
70
+ test_files:
71
+ - spec/optparse/subcommand_spec.rb