cooloptions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.1.0 / 2006-11-30
2
+
3
+ * Initial Release
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/cooloptions.rb
6
+ test/test_cooloptions.rb
data/README.txt ADDED
@@ -0,0 +1,40 @@
1
+ = CoolOptions
2
+ by Nathaniel Talbott <nathaniel@terralien.com>
3
+ http://cooloptions.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ CoolOptions is a simple wrapper around optparse that provides less options and more convenience.
8
+
9
+ == SYNOPSYS:
10
+
11
+ options = CoolOptions.parse!("[options] PROJECTNAME") do |o|
12
+ o.on :svk, "[no-]svk", "Use svk.", true
13
+ o.on :project_path, "project-path PATH", "Root of project workspaces.", File.expand_path("~/svk")
14
+ o.on :repository, "repository URL", "Remote subversion repository."
15
+ o.on :repository_path, "repository-path PATH", "Remote repository path.", "/", "l"
16
+ o.on :mirror_path, "mirror-path SVKPATH", "SVK mirror path.", "//"
17
+ o.on :local_path, "local-path SVKPATH", "SVK local path.", "//local", "t"
18
+ o.on :create_structure, "[no-]create-structure", "Create trunk/tags/branches structure.", true
19
+ o.on :finish, "[no-]finish" , "Prep and commit the new project.", true
20
+
21
+ o.after do |r|
22
+ r.project_path = File.expand_path(r.project_path)
23
+ raise "Invalid path." unless File.exist?(r.project_path)
24
+ r.project_name = ARGV.shift
25
+ end
26
+ end
27
+
28
+ == REQUIREMENTS:
29
+
30
+ optparse (included in Ruby stdlib)
31
+
32
+ == INSTALL:
33
+
34
+ sudo gem install cooloptions
35
+
36
+ == LICENSE:
37
+
38
+ The RUBY License
39
+
40
+ Copyright (c) 2006 Nathaniel Talbott and Terralien, Inc. All Rights Reserved.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/cooloptions.rb'
4
+
5
+ Hoe.new('cooloptions', CoolOptions::VERSION) do |p|
6
+ p.rubyforge_name = 'cooloptions'
7
+ p.summary = p.description = 'CoolOptions is a simple wrapper around optparse that provides less options and more convenience.'
8
+ p.url = 'https://rubyforge.org/projects/cooloptions/'
9
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
10
+ p.author = "Nathaniel Talbott"
11
+ p.email = "nathaniel@terralien.com"
12
+ p.extra_deps = []
13
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2006 Nathaniel Talbott and Terralien, Inc. All Rights Reserved.
2
+ # Licensed under the RUBY license.
3
+
4
+ require 'optparse'
5
+ require 'ostruct'
6
+
7
+ class CoolOptions
8
+ VERSION = '0.1.0'
9
+
10
+ class Error < StandardError #:nodoc:
11
+ end
12
+
13
+ def self.parse!(*args)
14
+ o = new
15
+ yield o
16
+ o.parse!(*args)
17
+ rescue Error => e
18
+ out.puts e.message
19
+ o.help true
20
+ end
21
+
22
+ def self.out #:nodoc:
23
+ @out || STDOUT
24
+ end
25
+
26
+ def out #:nodoc:
27
+ self.class.out
28
+ end
29
+
30
+ def self.out=(out) #:nodoc:
31
+ @out = out
32
+ end
33
+
34
+ def initialize
35
+ @options = []
36
+ @after = nil
37
+ end
38
+
39
+ def on(key, long, message, default=nil, short=nil)
40
+ unless short
41
+ short = if /^\[/ =~ long
42
+ long.split(/\]/).last
43
+ else
44
+ long
45
+ end[0,1]
46
+ end
47
+ @options << [key, short, long, message, default]
48
+ end
49
+
50
+ def parse!(banner="[options]", argv=ARGV)
51
+ result = {}
52
+ required = []
53
+ OptionParser.new do |o|
54
+ @o = o
55
+ o.banner = "Usage: #{File.basename($0)} #{banner}"
56
+
57
+ @options.each do |(key, short, long, message, default)|
58
+ args = ["-#{short}", "--#{long}", message]
59
+ if default.nil?
60
+ required << key
61
+ else
62
+ result[key] = default
63
+ args << "Default is: #{default}"
64
+ end
65
+ o.on(*args){|e| result[key] = e}
66
+ end
67
+
68
+ o.on('-h', '--help', "This help info."){help}
69
+ end.parse!(argv)
70
+ required.reject!{|e| result.key?(e)}
71
+ error "Missing required options: #{required.join(', ')}" unless required.empty?
72
+ result = OpenStruct.new(result)
73
+ @after.call(result) if @after
74
+ result
75
+ end
76
+
77
+ def error(message)
78
+ raise Error, message, caller
79
+ end
80
+
81
+ def after(&after)
82
+ @after = after
83
+ end
84
+
85
+ def help(error=false)
86
+ out.puts @o
87
+ exit(error ? 1 : 0)
88
+ end
89
+ end
90
+
@@ -0,0 +1,95 @@
1
+ # --
2
+ # Copyright (c) 2006 Nathaniel Talbott and Terralien, Inc. All Rights Reserved.
3
+ # Licensed under the RUBY license.
4
+
5
+ require 'test/unit'
6
+ require 'cooloptions'
7
+ require 'stringio'
8
+
9
+ class TestCoolOptions < Test::Unit::TestCase
10
+ def setup
11
+ @out = CoolOptions.out = StringIO.new
12
+ end
13
+
14
+ def parse!(argv, &block)
15
+ CoolOptions.parse!('', argv, &block)
16
+ rescue Exception
17
+ raise if $!.class >= StandardError
18
+ raise "Exception raised: #{$!.inspect}#{"\nOutput:\n" + @out.string if $!.class == SystemExit}"
19
+ end
20
+
21
+ def test_should_handle_booleans
22
+ r = parse!(%w(-a --no-b --c)) do |o|
23
+ o.on :a, '[no-]a', 'a', false
24
+ o.on :b, '[no-]b', 'b', true
25
+ o.on :c, '[no-]c', 'c', false
26
+ o.on :d, '[no-]d', 'd', true
27
+ end
28
+
29
+ assert r.a
30
+ assert !r.b
31
+ assert r.c
32
+ assert r.d
33
+ end
34
+
35
+ def test_should_handle_strings
36
+ r = parse!(%w(-a b --c=d)) do |o|
37
+ o.on :a, 'a ARG', 'a'
38
+ o.on :c, 'c ARG', 'c'
39
+ end
40
+
41
+ assert_equal 'b', r.a
42
+ assert_equal 'd', r.c
43
+ end
44
+
45
+ def test_should_ignore_non_options
46
+ r = CoolOptions.parse!('', argv=%w(ab -c de)) do |o|
47
+ o.on :c, '[no-]c', 'c'
48
+ end
49
+
50
+ assert r.c
51
+ assert_equal %w(ab de), argv
52
+ end
53
+
54
+ def test_should_call_after
55
+ called = false
56
+ r = parse!(%w(-a)) do |o|
57
+ o.on :a, '[no-]a', 'a'
58
+ o.after{|r| assert r.a; called=true}
59
+ end
60
+ assert called
61
+ end
62
+
63
+ def test_should_not_catch_random_errors
64
+ assert_raise(RuntimeError) do
65
+ parse!([]) do |o|
66
+ o.after{raise RuntimeError}
67
+ end
68
+ end
69
+ end
70
+
71
+ def test_should_output_help
72
+ begin
73
+ r = CoolOptions.parse!('details', %w(--help)) do |o|
74
+ o.on :a, '[no-]a', 'aa'
75
+ end
76
+ rescue SystemExit
77
+ rescued = true
78
+ end
79
+
80
+ assert rescued
81
+ assert_equal <<EOH, @out.string
82
+ Usage: #{File.basename($0)} details
83
+ -a, --[no-]a aa
84
+ -h, --help This help info.
85
+ EOH
86
+ end
87
+
88
+ def test_should_require_options_with_no_default
89
+ assert_raise(SystemExit) do
90
+ CoolOptions.parse!([]) do |o|
91
+ o.on :a, 'a A', 'a'
92
+ end
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: cooloptions
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-12-07 00:00:00 -05:00
8
+ summary: CoolOptions is a simple wrapper around optparse that provides less options and more convenience.
9
+ require_paths:
10
+ - lib
11
+ email: nathaniel@terralien.com
12
+ homepage: https://rubyforge.org/projects/cooloptions/
13
+ rubyforge_project: cooloptions
14
+ description: CoolOptions is a simple wrapper around optparse that provides less options and more convenience.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Nathaniel Talbott
30
+ files:
31
+ - History.txt
32
+ - Manifest.txt
33
+ - README.txt
34
+ - Rakefile
35
+ - lib/cooloptions.rb
36
+ - test/test_cooloptions.rb
37
+ test_files:
38
+ - test/test_cooloptions.rb
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies: []
50
+