trollop 1.15 → 1.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 1.16 / 2010-04-01
2
+ * Add Trollop::with_standard_exception_handling method for easing the use of Parser directly.
3
+ * Handle scientific notation in float arguments, thanks to Will Fitzgerald.
4
+ * Drop hoe dependency.
5
+
1
6
  == 1.15 / 2009-09-30
2
7
  * Don't raise an exception when out of short arguments (thanks to Rafael
3
8
  Sevilla for pointing out how dumb this behavior was).
data/README.txt CHANGED
@@ -4,7 +4,7 @@ by William Morgan (http://masanjin.net/)
4
4
 
5
5
  Main page: http://trollop.rubyforge.org
6
6
 
7
- Release announcements and comments: http://all-thing.net/search/label/trollop
7
+ Release announcements and comments: http://all-thing.net/label/trollop
8
8
 
9
9
  Documentation quickstart: See Trollop::options (for some reason rdoc isn't
10
10
  linking that; it's in the top right of the screen if you're browsing online)
@@ -1,13 +1,13 @@
1
1
  ## lib/trollop.rb -- trollop command-line processing library
2
2
  ## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
3
3
  ## Copyright:: Copyright 2007 William Morgan
4
- ## License:: GNU GPL version 2
4
+ ## License:: the same terms as ruby itself
5
5
 
6
6
  require 'date'
7
7
 
8
8
  module Trollop
9
9
 
10
- VERSION = "1.15"
10
+ VERSION = "1.16"
11
11
 
12
12
  ## Thrown by Parser in the event of a commandline error. Not needed if
13
13
  ## you're using the Trollop::options entry.
@@ -22,7 +22,7 @@ class HelpNeeded < StandardError; end
22
22
  class VersionNeeded < StandardError; end
23
23
 
24
24
  ## Regex for floating point numbers
25
- FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))$/
25
+ FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?[\d]+)?$/
26
26
 
27
27
  ## Regex for parameters
28
28
  PARAM_RE = /^-(-|\.$|[^\d\.])/
@@ -32,9 +32,10 @@ PARAM_RE = /^-(-|\.$|[^\d\.])/
32
32
  ## #opt, #banner and #version, #depends, and #conflicts methods will
33
33
  ## typically be called.
34
34
  ##
35
- ## If it's necessary to instantiate this class (for more complicated
36
- ## argument-parsing situations), be sure to call #parse to actually
37
- ## produce the output hash.
35
+ ## If you want to instantiate this class yourself (for more complicated
36
+ ## argument-parsing logic), call #parse to actually produce the output hash,
37
+ ## and consider calling it from within
38
+ ## Trollop::with_standard_exception_handling.
38
39
  class Parser
39
40
 
40
41
  ## The set of values that indicate a flag option when passed as the
@@ -277,7 +278,10 @@ class Parser
277
278
  @stop_on_unknown = true
278
279
  end
279
280
 
280
- ## Parses the commandline. Typically called by Trollop::options.
281
+ ## Parses the commandline. Typically called by Trollop::options,
282
+ ## but you can call it directly if you need more control.
283
+ ##
284
+ ## throws CommandlineError, HelpNeeded, and VersionNeeded exceptions.
281
285
  def parse cmdline=ARGV
282
286
  vals = {}
283
287
  required = {}
@@ -349,7 +353,7 @@ class Parser
349
353
  end
350
354
 
351
355
  required.each do |sym, val|
352
- raise CommandlineError, "option '#{sym}' must be specified" unless given_args.include? sym
356
+ raise CommandlineError, "option --#{@specs[sym][:long]} must be specified" unless given_args.include? sym
353
357
  end
354
358
 
355
359
  ## parse parameters
@@ -389,6 +393,11 @@ class Parser
389
393
  # else: multiple options, with multiple parameters
390
394
  end
391
395
 
396
+ ## modify input in place with only those
397
+ ## arguments we didn't process
398
+ cmdline.clear
399
+ @leftovers.each { |l| cmdline << l }
400
+
392
401
  ## allow openstruct-style accessors
393
402
  class << vals
394
403
  def method_missing(m, *args)
@@ -653,11 +662,10 @@ private
653
662
  end
654
663
  end
655
664
 
656
- ## The top-level entry method into Trollop. Creates a Parser object,
657
- ## passes the block to it, then parses +args+ with it, handling any
658
- ## errors or requests for help or version information appropriately (and
659
- ## then exiting). Modifies +args+ in place. Returns a hash of option
660
- ## values.
665
+ ## The easy, syntactic-sugary entry method into Trollop. Creates a Parser,
666
+ ## passes the block to it, then parses +args+ with it, handling any errors or
667
+ ## requests for help or version information appropriately (and then exiting).
668
+ ## Modifies +args+ in place. Returns a hash of option values.
661
669
  ##
662
670
  ## The block passed in should contain zero or more calls to +opt+
663
671
  ## (Parser#opt), zero or more calls to +text+ (Parser#text), and
@@ -686,22 +694,47 @@ end
686
694
  ## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
687
695
  ##
688
696
  ## See more examples at http://trollop.rubyforge.org.
689
- def options args = ARGV, *a, &b
690
- @p = Parser.new(*a, &b)
697
+ def options args=ARGV, *a, &b
698
+ p = Parser.new(*a, &b)
699
+ with_standard_exception_handling(p) { p.parse args }
700
+ end
701
+
702
+ ## If Trollop::options doesn't do quite what you want, you can create a Parser
703
+ ## object and call Parser#parse on it. That method will throw CommandlineError,
704
+ ## HelpNeeded and VersionNeeded exceptions when necessary; if you want to
705
+ ## have these handled for you in the standard manner (e.g. show the help
706
+ ## and then exit upon an HelpNeeded exception), call your code from within
707
+ ## a block passed to this method.
708
+ ##
709
+ ## Note that this method will call System#exit after handling an exception!
710
+ ##
711
+ ## Usage example:
712
+ ##
713
+ ## require 'trollop'
714
+ ## p = Trollop::Parser.new do
715
+ ## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
716
+ ## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
717
+ ## end
718
+ ##
719
+ ## opts = Trollop::with_standard_exception_handling p do
720
+ ## p.parse ARGV
721
+ ## raise Trollop::HelpNeeded if ARGV.empty? # show help screen
722
+ ## end
723
+ ##
724
+ ## Requires passing in the parser object.
725
+
726
+ def with_standard_exception_handling parser
691
727
  begin
692
- vals = @p.parse args
693
- args.clear
694
- @p.leftovers.each { |l| args << l }
695
- vals
728
+ yield
696
729
  rescue CommandlineError => e
697
730
  $stderr.puts "Error: #{e.message}."
698
731
  $stderr.puts "Try --help for help."
699
732
  exit(-1)
700
733
  rescue HelpNeeded
701
- @p.educate
734
+ parser.educate
702
735
  exit
703
736
  rescue VersionNeeded
704
- puts @p.version
737
+ puts parser.version
705
738
  exit
706
739
  end
707
740
  end
@@ -734,6 +767,6 @@ def die arg, msg=nil
734
767
  exit(-1)
735
768
  end
736
769
 
737
- module_function :options, :die
770
+ module_function :options, :die, :with_standard_exception_handling
738
771
 
739
772
  end # module
@@ -0,0 +1,15 @@
1
+ Just a few simple steps to make a new release.
2
+
3
+ vi History.txt # and describe changes
4
+ git-rank-contributors -o -h >> www/index.html
5
+ vi www/index.html # and integrate contributors
6
+ ## git add, git commit, etc
7
+ vi lib/trollop.rb # and bump version number
8
+ git commit -a -m "bump to..."
9
+ git tag release-<releasename>
10
+ rake gem
11
+ rake tarball
12
+ gem push pkg/<gem name>
13
+ git push
14
+ git push --tags
15
+ rake upload_webpage
@@ -82,7 +82,7 @@ class Trollop < ::Test::Unit::TestCase
82
82
 
83
83
  # single arg: int
84
84
  assert_nothing_raised { @p.opt "argsi", "desc", :default => 0 }
85
- assert_nothing_raised { opts = @p.parse("--") }
85
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
86
86
  assert_equal 0, opts["argsi"]
87
87
  assert_nothing_raised { opts = @p.parse(%w(--argsi 4)) }
88
88
  assert_equal 4, opts["argsi"]
@@ -91,18 +91,20 @@ class Trollop < ::Test::Unit::TestCase
91
91
 
92
92
  # single arg: float
93
93
  assert_nothing_raised { @p.opt "argsf", "desc", :default => 3.14 }
94
- assert_nothing_raised { opts = @p.parse("--") }
94
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
95
95
  assert_equal 3.14, opts["argsf"]
96
96
  assert_nothing_raised { opts = @p.parse(%w(--argsf 2.41)) }
97
97
  assert_equal 2.41, opts["argsf"]
98
98
  assert_nothing_raised { opts = @p.parse(%w(--argsf 2)) }
99
99
  assert_equal 2, opts["argsf"]
100
+ assert_nothing_raised { opts = @p.parse(%w(--argsf 1.0e-2)) }
101
+ assert_equal 1.0e-2, opts["argsf"]
100
102
  assert_raise(CommandlineError) { @p.parse(%w(--argsf hello)) }
101
103
 
102
104
  # single arg: date
103
105
  date = Date.today
104
106
  assert_nothing_raised { @p.opt "argsd", "desc", :default => date }
105
- assert_nothing_raised { opts = @p.parse("--") }
107
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
106
108
  assert_equal Date.today, opts["argsd"]
107
109
  assert_nothing_raised { opts = @p.parse(['--argsd', 'Jan 4, 2007']) }
108
110
  assert_equal Date.civil(2007, 1, 4), opts["argsd"]
@@ -110,7 +112,7 @@ class Trollop < ::Test::Unit::TestCase
110
112
 
111
113
  # single arg: string
112
114
  assert_nothing_raised { @p.opt "argss", "desc", :default => "foobar" }
113
- assert_nothing_raised { opts = @p.parse("--") }
115
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
114
116
  assert_equal "foobar", opts["argss"]
115
117
  assert_nothing_raised { opts = @p.parse(%w(--argss 2.41)) }
116
118
  assert_equal "2.41", opts["argss"]
@@ -119,7 +121,7 @@ class Trollop < ::Test::Unit::TestCase
119
121
 
120
122
  # multi args: ints
121
123
  assert_nothing_raised { @p.opt "argmi", "desc", :default => [3, 5] }
122
- assert_nothing_raised { opts = @p.parse("--") }
124
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
123
125
  assert_equal [3, 5], opts["argmi"]
124
126
  assert_nothing_raised { opts = @p.parse(%w(--argmi 4)) }
125
127
  assert_equal [4], opts["argmi"]
@@ -128,7 +130,7 @@ class Trollop < ::Test::Unit::TestCase
128
130
 
129
131
  # multi args: floats
130
132
  assert_nothing_raised { @p.opt "argmf", "desc", :default => [3.34, 5.21] }
131
- assert_nothing_raised { opts = @p.parse("--") }
133
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
132
134
  assert_equal [3.34, 5.21], opts["argmf"]
133
135
  assert_nothing_raised { opts = @p.parse(%w(--argmf 2)) }
134
136
  assert_equal [2], opts["argmf"]
@@ -139,7 +141,7 @@ class Trollop < ::Test::Unit::TestCase
139
141
  # multi args: dates
140
142
  dates = [Date.today, Date.civil(2007, 1, 4)]
141
143
  assert_nothing_raised { @p.opt "argmd", "desc", :default => dates }
142
- assert_nothing_raised { opts = @p.parse("--") }
144
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
143
145
  assert_equal dates, opts["argmd"]
144
146
  assert_nothing_raised { opts = @p.parse(['--argmd', 'Jan 4, 2007']) }
145
147
  assert_equal [Date.civil(2007, 1, 4)], opts["argmd"]
@@ -147,7 +149,7 @@ class Trollop < ::Test::Unit::TestCase
147
149
 
148
150
  # multi args: strings
149
151
  assert_nothing_raised { @p.opt "argmst", "desc", :default => %w(hello world) }
150
- assert_nothing_raised { opts = @p.parse("--") }
152
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
151
153
  assert_equal %w(hello world), opts["argmst"]
152
154
  assert_nothing_raised { opts = @p.parse(%w(--argmst 3.4)) }
153
155
  assert_equal ["3.4"], opts["argmst"]
@@ -1039,7 +1041,7 @@ EOM
1039
1041
 
1040
1042
  def test_multi_args_default_to_empty_array
1041
1043
  @p.opt :arg1, "arg", :multi => true
1042
- opts = @p.parse ""
1044
+ opts = @p.parse []
1043
1045
  assert_equal [], opts[:arg1]
1044
1046
  end
1045
1047
  end
metadata CHANGED
@@ -1,7 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trollop
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.15"
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 16
8
+ version: "1.16"
5
9
  platform: ruby
6
10
  authors:
7
11
  - William Morgan
@@ -9,78 +13,59 @@ autorequire:
9
13
  bindir: bin
10
14
  cert_chain: []
11
15
 
12
- date: 2009-09-30 00:00:00 -04:00
16
+ date: 2010-04-01 16:46:01 -04:00
13
17
  default_executable:
14
18
  dependencies: []
15
19
 
16
- description: |+
17
- Documentation quickstart: See Trollop::options (for some reason rdoc isn't
18
- linking that; it's in the top right of the screen if you're browsing online)
19
- and then Trollop::Parser#opt. Also see the examples at
20
- http://trollop.rubyforge.org/.
21
-
22
- == DESCRIPTION
23
-
24
- == REQUIREMENTS
25
-
26
- * A burning desire to write less code.
27
-
28
- == INSTALL
29
-
30
- * gem install trollop
31
-
32
- == LICENSE
33
-
34
- Copyright (c) 2008--2009 William Morgan. Trollop is distributed under the same
35
- terms as Ruby.
36
-
37
-
20
+ description: |-
21
+ Trollop is a commandline option parser for Ruby that just
22
+ gets out of your way. One line of code per option is all you need to write.
23
+ For that, you get a nice automatically-generated help page, robust option
24
+ parsing, command subcompletion, and sensible defaults for everything you don't
25
+ specify.
38
26
  email: wmorgan-trollop@masanjin.net
39
27
  executables: []
40
28
 
41
29
  extensions: []
42
30
 
43
- extra_rdoc_files:
44
- - FAQ.txt
45
- - History.txt
46
- - Manifest.txt
47
- - README.txt
31
+ extra_rdoc_files: []
32
+
48
33
  files:
49
- - FAQ.txt
50
- - History.txt
51
- - Manifest.txt
52
- - README.txt
53
- - Rakefile
54
34
  - lib/trollop.rb
55
35
  - test/test_trollop.rb
36
+ - README.txt
37
+ - release-script.txt
38
+ - FAQ.txt
39
+ - History.txt
56
40
  has_rdoc: true
57
41
  homepage: http://trollop.rubyforge.org
58
42
  licenses: []
59
43
 
60
44
  post_install_message:
61
- rdoc_options:
62
- - --main
63
- - README.txt
45
+ rdoc_options: []
46
+
64
47
  require_paths:
65
48
  - lib
66
49
  required_ruby_version: !ruby/object:Gem::Requirement
67
50
  requirements:
68
51
  - - ">="
69
52
  - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
70
55
  version: "0"
71
- version:
72
56
  required_rubygems_version: !ruby/object:Gem::Requirement
73
57
  requirements:
74
58
  - - ">="
75
59
  - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
76
62
  version: "0"
77
- version:
78
63
  requirements: []
79
64
 
80
65
  rubyforge_project: trollop
81
- rubygems_version: 1.3.2
66
+ rubygems_version: 1.3.6
82
67
  signing_key:
83
68
  specification_version: 3
84
- summary: Trollop is a commandline option parser for Ruby that just gets out of your way. One line of code per option is all you need to write. For that, you get a nice automatically-generated help page, robust option parsing, command subcompletion, and sensible defaults for everything you don't specify.
85
- test_files:
86
- - test/test_trollop.rb
69
+ summary: Trollop is a commandline option parser for Ruby that just gets out of your way.
70
+ test_files: []
71
+
@@ -1,7 +0,0 @@
1
- FAQ.txt
2
- History.txt
3
- Manifest.txt
4
- README.txt
5
- Rakefile
6
- lib/trollop.rb
7
- test/test_trollop.rb
data/Rakefile DELETED
@@ -1,36 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- require 'rubygems'
4
- require 'hoe'
5
-
6
- $:.unshift "lib"
7
- require 'trollop'
8
-
9
- class Hoe
10
- def extra_dev_deps; @extra_dev_deps.reject { |x| x[0] == "hoe" } end
11
- end
12
-
13
- Hoe.new('trollop', Trollop::VERSION) do |p|
14
- p.rubyforge_name = 'trollop'
15
- p.author = "William Morgan"
16
- p.summary = "Trollop is a commandline option parser for Ruby that just gets out of your way. One line of code per option is all you need to write. For that, you get a nice automatically-generated help page, robust option parsing, command subcompletion, and sensible defaults for everything you don't specify."
17
- p.description = p.paragraphs_of('README.txt', 4..5, 9..18).join("\n\n").gsub(/== SYNOPSIS/, "Synopsis")
18
- p.url = "http://trollop.rubyforge.org"
19
- p.changes = p.paragraphs_of('History.txt', 0..0).join("\n\n")
20
- p.email = "wmorgan-trollop@masanjin.net"
21
- end
22
-
23
- WWW_FILES = FileList["www/*"] + %w(README.txt FAQ.txt)
24
- task :upload_webpage => WWW_FILES do |t|
25
- sh "rsync -Paz -essh #{t.prerequisites * ' '} wmorgan@rubyforge.org:/var/www/gforge-projects/trollop/"
26
- end
27
-
28
- task :rdoc do |t|
29
- sh "rdoc lib README.txt History.txt -m README.txt"
30
- end
31
-
32
- task :upload_docs => :rdoc do |t|
33
- sh "rsync -az -essh doc/* wmorgan@rubyforge.org:/var/www/gforge-projects/trollop/trollop/"
34
- end
35
-
36
- # vim: syntax=ruby