jashmenn-git-style-binaries 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -9,6 +9,12 @@ This gem uses [`trollop`](http://trollop.rubyforge.org/) for option parsing
9
9
 
10
10
  gem install jashmenn-git-style-binaries --source=http://gems.github.com
11
11
 
12
+ ## Screencast
13
+
14
+ Checkout <a href="http://www.xcombinator.com/movies/git-style-binaries.mov">the new screencast!</a>
15
+
16
+ <a href="http://www.xcombinator.com/movies/git-style-binaries.mov"><img src="http://github.com/jashmenn/git-style-binaries/tree/master/doc/gsb-screencast.png?raw=true" width='880' height='784' border=0></a>
17
+
12
18
  ## Try it out
13
19
 
14
20
  cd `gem env gemdir`/gems/jashmenn-git-style-binaries-0.1.4/test/fixtures
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ begin
16
16
  gem.add_dependency 'thoughtbot-shoulda' # for running the tests
17
17
 
18
18
  excludes = /(README\.html)/
19
- gem.files = (FileList["[A-Z]*.*", "{bin,examples,generators,lib,rails,spec,test}/**/*", 'Rakefile', 'LICENSE*']).delete_if{|f| f =~ excludes}
19
+ gem.files = (FileList["[A-Z]*.*", "{bin,examples,generators,lib,rails,spec,test,vendor}/**/*", 'Rakefile', 'LICENSE*']).delete_if{|f| f =~ excludes}
20
20
  gem.extra_rdoc_files = FileList["README*", "ChangeLog*", "LICENSE*"].delete_if{|f| f =~ excludes}
21
21
  end
22
22
  rescue LoadError
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 9
2
3
  :major: 0
3
4
  :minor: 1
4
- :patch: 7
@@ -1,6 +1,17 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
  require 'rubygems'
3
- require 'trollop'
3
+
4
+ # Load the vendor gems
5
+ $:.unshift(File.dirname(__FILE__) + "/../vendor/gems")
6
+ %w(trollop).each do |library|
7
+ begin
8
+ require "#{library}/lib/#{library}"
9
+ rescue
10
+ puts "There was an error loading #{library}. Try running git submodule init && git submodule update to correct the problem"
11
+ require 'trollop'
12
+ end
13
+ end
14
+
4
15
  require 'ext/core'
5
16
  require 'ext/colorize'
6
17
  require 'git-style-binary/autorunner'
@@ -84,7 +84,7 @@ class RunningBinariesTest < Test::Unit::TestCase
84
84
 
85
85
  %w(before after).each do |time|
86
86
  should "run the callback #{time}_run}" do
87
- assert @stdout.match /#{time}_run command/
87
+ assert @stdout.match(/#{time}_run command/)
88
88
  end
89
89
  end
90
90
  end
@@ -94,7 +94,7 @@ class RunningBinariesTest < Test::Unit::TestCase
94
94
 
95
95
  %w(before after).each do |time|
96
96
  should "not run the callback #{time}_run" do
97
- assert_nil @stdout.match /#{time}_run command/
97
+ assert_nil @stdout.match(/#{time}_run command/)
98
98
  end
99
99
  end
100
100
  end
@@ -0,0 +1,35 @@
1
+ Trollop FAQ
2
+ -----------
3
+
4
+ Q: Why should I use Trollop?
5
+ A: Because it will take you FEWER LINES OF CODE to do reasonable option
6
+ parsing than any other option parser in existence.
7
+
8
+ Look:
9
+
10
+ opts = Trollop::options do
11
+ opt :monkey, "Use monkey mode"
12
+ opt :goat, "Use goat mode", :default => true
13
+ opt :num_limbs, "Set number of limbs", :default => 4
14
+ end
15
+
16
+ That's it. And opts is a hash and you do whatever you want with it.
17
+ Trivial. You don't have to mix option processing code blocks with the
18
+ declarations. You don't have to make a class for every option (what is
19
+ this, Java?). You don't have to write more than 1 line of code per
20
+ option.
21
+
22
+ Q: Why is it called "Trollop"?
23
+ A: No reason.
24
+
25
+ Q: Why does Trollop disallow numeric short argument names, like '-1'
26
+ and '-9'?
27
+ A: Because it's ambiguous whether these are arguments or negative
28
+ integer or floating-point parameters to arguments. E.g., what
29
+ about "-f -3". Is that a negative three parameter to -f, or two
30
+ separate parameters?
31
+
32
+ I could be very clever and detect when there are no arguments
33
+ that require floating-point parameters, and allow such short option
34
+ names in those cases, but opted for simplicity and consistency.
35
+
@@ -0,0 +1,97 @@
1
+ == 1.13 / 2009-03-16
2
+ * Fix parsing of "--longarg=<value with spaces>".
3
+
4
+ == 1.12 / 2009-01-30
5
+ * Fix some unit test failures in the last release. Should be more careful.
6
+ * Make default short options only be assigned *after* all user-specified
7
+ short options. Now there's a little less juggling to do when you just
8
+ want to specify a few short options.
9
+
10
+ == 1.11 / 2009-01-29
11
+ * Set <opt>_given keys in the results hash for options that were specified
12
+ on the commandline.
13
+
14
+ == 1.10.2 / 2008-10-23
15
+ * No longer try `stty size` for screen size detection. Just use curses, and
16
+ screen users will have to deal with the screen clearing.
17
+
18
+ == 1.10.1 / 2008-10-22
19
+ * Options hash now responds to method calls as well as standard hash lookup.
20
+ * Default values for multi-occurrence parameters now autoboxed.
21
+ * The relationship between multi-value, multi-occurrence, and default values
22
+ improved and explained.
23
+ * Documentation improvements.
24
+
25
+ == 1.10 / 2008-10-21
26
+ * Added :io type for parameters that point to IO streams (filenames, URIs, etc).
27
+ * For screen size detection, first try `stty size` before loading Curses.
28
+ * Improved documentation.
29
+
30
+ == 1.9 / 2008-08-20
31
+ * Added 'stop_on_unknown' command to stop parsing on any unknown argument.
32
+ This is useful for handling sub-commands when you don't know the entire
33
+ set of commands up front. (E.g. if the initial arguments can change it.)
34
+ * Added a :multi option for parameters, signifying that they can be specified
35
+ multiple times.
36
+ * Added :ints, :strings, :doubles, and :floats option types, which can take
37
+ multiple arguments.
38
+
39
+ == 1.8.2 / 2008-06-25
40
+ * Bugfix for #conflicts and #depends error messages
41
+
42
+ == 1.8.1 / 2008-06-24
43
+ * Bugfix for short option autocreation
44
+ * More aggressive documentation
45
+
46
+ == 1.8 / 2008-06-16
47
+ * Sub-command support via Parser#stop_on
48
+
49
+ == 1.7.2 / 2008-01-16
50
+ * Ruby 1.9-ify. Apparently this means replacing :'s with ;'s.
51
+
52
+ == 1.7.1 / 2008-01-07
53
+ * Documentation improvements
54
+
55
+ == 1.7 / 2007-06-17
56
+ * Fix incorrect error message for multiple missing required arguments
57
+ (thanks to Neill Zero)
58
+
59
+ == 1.6 / 2007-04-01
60
+ * Don't attempt curses screen-width magic unless running on a terminal.
61
+
62
+ == 1.5 / 2007-03-31
63
+ * --help and --version do the right thing even if the rest of the
64
+ command line is incorrect.
65
+ * Added #conflicts and #depends to model dependencies and exclusivity
66
+ between arguments.
67
+ * Minor bugfixes.
68
+
69
+ == 1.4 / 2007-03-26
70
+ * Disable short options with :short => :none.
71
+ * Minor bugfixes and error message improvements.
72
+
73
+ == 1.3 / 2007-01-31
74
+ * Wrap at (screen width - 1) instead of screen width.
75
+ * User can override --help and --version.
76
+ * Bugfix in handling of -v and -h.
77
+ * More tests to confirm the above.
78
+
79
+ == 1.2 / 2007-01-31
80
+ * Minor documentation tweaks.
81
+ * Removed hoe dependency.
82
+
83
+ == 1.1 / 2007-01-30
84
+ * Trollop::options now passes any arguments as block arguments. Since
85
+ instance variables are not properly captured by the block, this
86
+ makes it slightly less noisy to pass them in as local variables.
87
+ (A real-life use for _why's cloaker!)
88
+ * Help display now preserves original argument order.
89
+ * Trollop::die now also has a single string form in case death is not
90
+ due to a single argument.
91
+ * Parser#text now an alias for Parser#banner, and can be called
92
+ multiple times, with the output being placed in the right position
93
+ in the help text.
94
+ * Slightly more indicative formatting for parameterized arguments.
95
+
96
+ == 1.0 / 2007-01-29
97
+ * Initial release.
@@ -0,0 +1,7 @@
1
+ FAQ.txt
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/trollop.rb
7
+ test/test_trollop.rb
@@ -0,0 +1,40 @@
1
+ == trollop
2
+
3
+ by William Morgan (wmorgan-trollop at the masanjin dot nets or http://cs.stanford.edu/~ruby)
4
+
5
+ Main page: http://trollop.rubyforge.org
6
+
7
+ Release announcements and comments: http://all-thing.net/search/label/trollop
8
+
9
+ Documentation quickstart: See Trollop::options (for some reason rdoc isn't
10
+ linking that; it's in the top right of the screen if you're browsing online)
11
+ and then Trollop::Parser#opt. Also see the examples at http://trollop.rubyforge.org/.
12
+
13
+ == DESCRIPTION
14
+
15
+ Trollop is a commandline option parser for Ruby that just gets out of your
16
+ way. One line of code per option is all you need to write. For that, you get
17
+ a nice automatically-generated help page, robust option parsing, command
18
+ subcompletion, and sensible defaults for everything you don't specify.
19
+
20
+ == FEATURES/PROBLEMS
21
+
22
+ - Dirt-simple usage.
23
+ - Sensible defaults. No tweaking necessary, much tweaking possible.
24
+ - Support for long options, short options, short option bundling,
25
+ and automatic type validation and conversion.
26
+ - Support for subcommands.
27
+ - Automatic help message generation, wrapped to current screen width.
28
+ - Lots of unit tests.
29
+
30
+ == REQUIREMENTS
31
+
32
+ * A burning desire to write less code.
33
+
34
+ == INSTALL
35
+
36
+ * gem install trollop
37
+
38
+ == LICENSE
39
+
40
+ Copyright (c) 2008 William Morgan. Trollop is distributed under the same terms as Ruby.
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,735 @@
1
+ ## lib/trollop.rb -- trollop command-line processing library
2
+ ## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
3
+ ## Copyright:: Copyright 2007 William Morgan
4
+ ## License:: GNU GPL version 2
5
+
6
+ require 'date'
7
+
8
+ module Trollop
9
+
10
+ VERSION = "1.13"
11
+
12
+ ## Thrown by Parser in the event of a commandline error. Not needed if
13
+ ## you're using the Trollop::options entry.
14
+ class CommandlineError < StandardError; end
15
+
16
+ ## Thrown by Parser if the user passes in '-h' or '--help'. Handled
17
+ ## automatically by Trollop#options.
18
+ class HelpNeeded < StandardError; end
19
+
20
+ ## Thrown by Parser if the user passes in '-h' or '--version'. Handled
21
+ ## automatically by Trollop#options.
22
+ class VersionNeeded < StandardError; end
23
+
24
+ ## Regex for floating point numbers
25
+ FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))$/
26
+
27
+ ## Regex for parameters
28
+ PARAM_RE = /^-(-|\.$|[^\d\.])/
29
+
30
+ ## The commandline parser. In typical usage, the methods in this class
31
+ ## will be handled internally by Trollop::options. In this case, only the
32
+ ## #opt, #banner and #version, #depends, and #conflicts methods will
33
+ ## typically be called.
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.
38
+ class Parser
39
+
40
+ ## The set of values that indicate a flag option when passed as the
41
+ ## +:type+ parameter of #opt.
42
+ FLAG_TYPES = [:flag, :bool, :boolean]
43
+
44
+ ## The set of values that indicate a single-parameter option when
45
+ ## passed as the +:type+ parameter of #opt.
46
+ ##
47
+ ## A value of +io+ corresponds to a readable IO resource, including
48
+ ## a filename, URI, or the strings 'stdin' or '-'.
49
+ SINGLE_ARG_TYPES = [:int, :integer, :string, :double, :float, :io, :date]
50
+
51
+ ## The set of values that indicate a multiple-parameter option when
52
+ ## passed as the +:type+ parameter of #opt.
53
+ MULTI_ARG_TYPES = [:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
54
+
55
+ ## The complete set of legal values for the +:type+ parameter of #opt.
56
+ TYPES = FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
57
+
58
+ INVALID_SHORT_ARG_REGEX = /[\d-]/ #:nodoc:
59
+
60
+ ## The values from the commandline that were not interpreted by #parse.
61
+ attr_reader :leftovers
62
+
63
+ ## The complete configuration hashes for each option. (Mainly useful
64
+ ## for testing.)
65
+ attr_reader :specs
66
+
67
+ ## Initializes the parser, and instance-evaluates any block given.
68
+ def initialize *a, &b
69
+ @version = nil
70
+ @leftovers = []
71
+ @specs = {}
72
+ @long = {}
73
+ @short = {}
74
+ @order = []
75
+ @constraints = []
76
+ @stop_words = []
77
+ @stop_on_unknown = false
78
+
79
+ #instance_eval(&b) if b # can't take arguments
80
+ cloaker(&b).bind(self).call(*a) if b
81
+ end
82
+
83
+ ## Define an option. +name+ is the option name, a unique identifier
84
+ ## for the option that you will use internally, which should be a
85
+ ## symbol or a string. +desc+ is a string description which will be
86
+ ## displayed in help messages.
87
+ ##
88
+ ## Takes the following optional arguments:
89
+ ##
90
+ ## [+:long+] Specify the long form of the argument, i.e. the form with two dashes. If unspecified, will be automatically derived based on the argument name by turning the +name+ option into a string, and replacing any _'s by -'s.
91
+ ## [+:short+] Specify the short form of the argument, i.e. the form with one dash. If unspecified, will be automatically derived from +name+.
92
+ ## [+:type+] Require that the argument take a parameter or parameters of type +type+. For a single parameter, the value can be a member of +SINGLE_ARG_TYPES+, or a corresponding Ruby class (e.g. +Integer+ for +:int+). For multiple-argument parameters, the value can be any member of +MULTI_ARG_TYPES+ constant. If unset, the default argument type is +:flag+, meaning that the argument does not take a parameter. The specification of +:type+ is not necessary if a +:default+ is given.
93
+ ## [+:default+] Set the default value for an argument. Without a default value, the hash returned by #parse (and thus Trollop::options) will have a +nil+ value for this key unless the argument is given on the commandline. The argument type is derived automatically from the class of the default value given, so specifying a +:type+ is not necessary if a +:default+ is given. (But see below for an important caveat when +:multi+: is specified too.) If the argument is a flag, and the default is set to +true+, then if it is specified on the the commandline the value will be +false+.
94
+ ## [+:required+] If set to +true+, the argument must be provided on the commandline.
95
+ ## [+:multi+] If set to +true+, allows multiple occurrences of the option on the commandline. Otherwise, only a single instance of the option is allowed. (Note that this is different from taking multiple parameters. See below.)
96
+ ##
97
+ ## Note that there are two types of argument multiplicity: an argument
98
+ ## can take multiple values, e.g. "--arg 1 2 3". An argument can also
99
+ ## be allowed to occur multiple times, e.g. "--arg 1 --arg 2".
100
+ ##
101
+ ## Arguments that take multiple values should have a +:type+ parameter
102
+ ## drawn from +MULTI_ARG_TYPES+ (e.g. +:strings+), or a +:default:+
103
+ ## value of an array of the correct type (e.g. [String]). The
104
+ ## value of this argument will be an array of the parameters on the
105
+ ## commandline.
106
+ ##
107
+ ## Arguments that can occur multiple times should be marked with
108
+ ## +:multi+ => +true+. The value of this argument will also be an array.
109
+ ##
110
+ ## These two attributes can be combined (e.g. +:type+ => +:strings+,
111
+ ## +:multi+ => +true+), in which case the value of the argument will be
112
+ ## an array of arrays.
113
+ ##
114
+ ## There's one ambiguous case to be aware of: when +:multi+: is true and a
115
+ ## +:default+ is set to an array (of something), it's ambiguous whether this
116
+ ## is a multi-value argument as well as a multi-occurrence argument.
117
+ ## In thise case, Trollop assumes that it's not a multi-value argument.
118
+ ## If you want a multi-value, multi-occurrence argument with a default
119
+ ## value, you must specify +:type+ as well.
120
+
121
+ def opt name, desc="", opts={}
122
+ raise ArgumentError, "you already have an argument named '#{name}'" if @specs.member? name
123
+
124
+ ## fill in :type
125
+ opts[:type] = # normalize
126
+ case opts[:type]
127
+ when :boolean, :bool; :flag
128
+ when :integer; :int
129
+ when :integers; :ints
130
+ when :double; :float
131
+ when :doubles; :floats
132
+ when Class
133
+ case opts[:type].name
134
+ when 'TrueClass', 'FalseClass'; :flag
135
+ when 'String'; :string
136
+ when 'Integer'; :int
137
+ when 'Float'; :float
138
+ when 'IO'; :io
139
+ when 'Date'; :date
140
+ else
141
+ raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'"
142
+ end
143
+ when nil; nil
144
+ else
145
+ raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type])
146
+ opts[:type]
147
+ end
148
+
149
+ ## for options with :multi => true, an array default doesn't imply
150
+ ## a multi-valued argument. for that you have to specify a :type
151
+ ## as well. (this is how we disambiguate an ambiguous situation;
152
+ ## see the docs for Parser#opt for details.)
153
+ disambiguated_default =
154
+ if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
155
+ opts[:default].first
156
+ else
157
+ opts[:default]
158
+ end
159
+
160
+ type_from_default =
161
+ case disambiguated_default
162
+ when Integer; :int
163
+ when Numeric; :float
164
+ when TrueClass, FalseClass; :flag
165
+ when String; :string
166
+ when IO; :io
167
+ when Date; :date
168
+ when Array
169
+ if opts[:default].empty?
170
+ raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
171
+ end
172
+ case opts[:default][0] # the first element determines the types
173
+ when Integer; :ints
174
+ when Numeric; :floats
175
+ when String; :strings
176
+ when IO; :ios
177
+ when Date; :dates
178
+ else
179
+ raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
180
+ end
181
+ when nil; nil
182
+ else
183
+ raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'"
184
+ end
185
+
186
+ raise ArgumentError, ":type specification and default type don't match" if opts[:type] && type_from_default && opts[:type] != type_from_default
187
+
188
+ opts[:type] = opts[:type] || type_from_default || :flag
189
+
190
+ ## fill in :long
191
+ opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
192
+ opts[:long] =
193
+ case opts[:long]
194
+ when /^--([^-].*)$/
195
+ $1
196
+ when /^[^-]/
197
+ opts[:long]
198
+ else
199
+ raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
200
+ end
201
+ raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
202
+
203
+ ## fill in :short
204
+ opts[:short] = opts[:short].to_s if opts[:short] unless opts[:short] == :none
205
+ opts[:short] = case opts[:short]
206
+ when /^-(.)$/; $1
207
+ when nil, :none, /^.$/; opts[:short]
208
+ else raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'"
209
+ end
210
+
211
+ if opts[:short]
212
+ raise ArgumentError, "short option name #{opts[:short].inspect} is already taken; please specify a (different) :short" if @short[opts[:short]]
213
+ raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ INVALID_SHORT_ARG_REGEX
214
+ end
215
+
216
+ ## fill in :default for flags
217
+ opts[:default] = false if opts[:type] == :flag && opts[:default].nil?
218
+
219
+ ## autobox :default for :multi (multi-occurrence) arguments
220
+ opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].is_a?(Array)
221
+
222
+ ## fill in :multi
223
+ opts[:multi] ||= false
224
+
225
+ opts[:desc] ||= desc
226
+ @long[opts[:long]] = name
227
+ @short[opts[:short]] = name if opts[:short] && opts[:short] != :none
228
+ @specs[name] = opts
229
+ @order << [:opt, name]
230
+ end
231
+
232
+ ## Sets the version string. If set, the user can request the version
233
+ ## on the commandline. Should probably be of the form "<program name>
234
+ ## <version number>".
235
+ def version s=nil; @version = s if s; @version end
236
+
237
+ ## Adds text to the help display. Can be interspersed with calls to
238
+ ## #opt to build a multi-section help page.
239
+ def banner s; @order << [:text, s] end
240
+ alias :text :banner
241
+
242
+ ## Marks two (or more!) options as requiring each other. Only handles
243
+ ## undirected (i.e., mutual) dependencies. Directed dependencies are
244
+ ## better modeled with Trollop::die.
245
+ def depends *syms
246
+ syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
247
+ @constraints << [:depends, syms]
248
+ end
249
+
250
+ ## Marks two (or more!) options as conflicting.
251
+ def conflicts *syms
252
+ syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
253
+ @constraints << [:conflicts, syms]
254
+ end
255
+
256
+ ## Defines a set of words which cause parsing to terminate when
257
+ ## encountered, such that any options to the left of the word are
258
+ ## parsed as usual, and options to the right of the word are left
259
+ ## intact.
260
+ ##
261
+ ## A typical use case would be for subcommand support, where these
262
+ ## would be set to the list of subcommands. A subsequent Trollop
263
+ ## invocation would then be used to parse subcommand options, after
264
+ ## shifting the subcommand off of ARGV.
265
+ def stop_on *words
266
+ @stop_words = [*words].flatten
267
+ end
268
+
269
+ ## Similar to #stop_on, but stops on any unknown word when encountered
270
+ ## (unless it is a parameter for an argument). This is useful for
271
+ ## cases where you don't know the set of subcommands ahead of time,
272
+ ## i.e., without first parsing the global options.
273
+ def stop_on_unknown
274
+ @stop_on_unknown = true
275
+ end
276
+
277
+ ## Parses the commandline. Typically called by Trollop::options.
278
+ def parse cmdline=ARGV
279
+ vals = {}
280
+ required = {}
281
+
282
+ opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
283
+ opt :help, "Show this message" unless @specs[:help] || @long["help"]
284
+
285
+ @specs.each do |sym, opts|
286
+ required[sym] = true if opts[:required]
287
+ vals[sym] = opts[:default]
288
+ end
289
+
290
+ resolve_default_short_options
291
+
292
+ ## resolve symbols
293
+ given_args = {}
294
+ @leftovers = each_arg cmdline do |arg, params|
295
+ sym = case arg
296
+ when /^-([^-])$/
297
+ @short[$1]
298
+ when /^--([^-]\S*)$/
299
+ @long[$1]
300
+ else
301
+ raise CommandlineError, "invalid argument syntax: '#{arg}'"
302
+ end
303
+ raise CommandlineError, "unknown argument '#{arg}'" unless sym
304
+
305
+ if given_args.include?(sym) && !@specs[sym][:multi]
306
+ raise CommandlineError, "option '#{arg}' specified multiple times"
307
+ end
308
+
309
+ given_args[sym] ||= {}
310
+
311
+ given_args[sym][:arg] = arg
312
+ given_args[sym][:params] ||= []
313
+
314
+ # The block returns the number of parameters taken.
315
+ num_params_taken = 0
316
+
317
+ unless params.nil?
318
+ if SINGLE_ARG_TYPES.include?(@specs[sym][:type])
319
+ given_args[sym][:params] << params[0, 1] # take the first parameter
320
+ num_params_taken = 1
321
+ elsif MULTI_ARG_TYPES.include?(@specs[sym][:type])
322
+ given_args[sym][:params] << params # take all the parameters
323
+ num_params_taken = params.size
324
+ end
325
+ end
326
+
327
+ num_params_taken
328
+ end
329
+
330
+ ## check for version and help args
331
+ raise VersionNeeded if given_args.include? :version
332
+ raise HelpNeeded if given_args.include? :help
333
+
334
+ ## check constraint satisfaction
335
+ @constraints.each do |type, syms|
336
+ constraint_sym = syms.find { |sym| given_args[sym] }
337
+ next unless constraint_sym
338
+
339
+ case type
340
+ when :depends
341
+ syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} requires --#{@specs[sym][:long]}" unless given_args.include? sym }
342
+ when :conflicts
343
+ syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} conflicts with --#{@specs[sym][:long]}" if given_args.include?(sym) && (sym != constraint_sym) }
344
+ end
345
+ end
346
+
347
+ required.each do |sym, val|
348
+ raise CommandlineError, "option '#{sym}' must be specified" unless given_args.include? sym
349
+ end
350
+
351
+ ## parse parameters
352
+ given_args.each do |sym, given_data|
353
+ arg = given_data[:arg]
354
+ params = given_data[:params]
355
+
356
+ opts = @specs[sym]
357
+ raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag
358
+
359
+ vals["#{sym}_given".intern] = true # mark argument as specified on the commandline
360
+
361
+ case opts[:type]
362
+ when :flag
363
+ vals[sym] = !opts[:default]
364
+ when :int, :ints
365
+ vals[sym] = params.map { |pg| pg.map { |p| parse_integer_parameter p, arg } }
366
+ when :float, :floats
367
+ vals[sym] = params.map { |pg| pg.map { |p| parse_float_parameter p, arg } }
368
+ when :string, :strings
369
+ vals[sym] = params.map { |pg| pg.map { |p| p.to_s } }
370
+ when :io, :ios
371
+ vals[sym] = params.map { |pg| pg.map { |p| parse_io_parameter p, arg } }
372
+ when :date, :dates
373
+ vals[sym] = params.map { |pg| pg.map { |p| parse_date_parameter p, arg } }
374
+ end
375
+
376
+ if SINGLE_ARG_TYPES.include?(opts[:type])
377
+ unless opts[:multi] # single parameter
378
+ vals[sym] = vals[sym][0][0]
379
+ else # multiple options, each with a single parameter
380
+ vals[sym] = vals[sym].map { |p| p[0] }
381
+ end
382
+ elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
383
+ vals[sym] = vals[sym][0] # single option, with multiple parameters
384
+ end
385
+ # else: multiple options, with multiple parameters
386
+ end
387
+
388
+ ## allow openstruct-style accessors
389
+ class << vals
390
+ def method_missing(m, *args)
391
+ self[m] || self[m.to_s]
392
+ end
393
+ end
394
+ vals
395
+ end
396
+
397
+ def parse_date_parameter param, arg #:nodoc:
398
+ begin
399
+ begin
400
+ time = Chronic.parse(param)
401
+ rescue NameError
402
+ # chronic is not available
403
+ end
404
+ time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
405
+ rescue ArgumentError => e
406
+ raise CommandlineError, "option '#{arg}' needs a date"
407
+ end
408
+ end
409
+
410
+ ## Print the help message to +stream+.
411
+ def educate stream=$stdout
412
+ width # just calculate it now; otherwise we have to be careful not to
413
+ # call this unless the cursor's at the beginning of a line.
414
+
415
+ left = {}
416
+ @specs.each do |name, spec|
417
+ left[name] = "--#{spec[:long]}" +
418
+ (spec[:short] && spec[:short] != :none ? ", -#{spec[:short]}" : "") +
419
+ case spec[:type]
420
+ when :flag; ""
421
+ when :int; " <i>"
422
+ when :ints; " <i+>"
423
+ when :string; " <s>"
424
+ when :strings; " <s+>"
425
+ when :float; " <f>"
426
+ when :floats; " <f+>"
427
+ when :io; " <filename/uri>"
428
+ when :ios; " <filename/uri+>"
429
+ when :date; " <date>"
430
+ when :dates; " <date+>"
431
+ end
432
+ end
433
+
434
+ leftcol_width = left.values.map { |s| s.length }.max || 0
435
+ rightcol_start = leftcol_width + 6 # spaces
436
+
437
+ unless @order.size > 0 && @order.first.first == :text
438
+ stream.puts "#@version\n" if @version
439
+ stream.puts "Options:"
440
+ end
441
+
442
+ @order.each do |what, opt|
443
+ if what == :text
444
+ stream.puts wrap(opt)
445
+ next
446
+ end
447
+
448
+ spec = @specs[opt]
449
+ stream.printf " %#{leftcol_width}s: ", left[opt]
450
+ desc = spec[:desc] + begin
451
+ default_s = case spec[:default]
452
+ when $stdout; "<stdout>"
453
+ when $stdin; "<stdin>"
454
+ when $stderr; "<stderr>"
455
+ when Array
456
+ spec[:default].join(", ")
457
+ else
458
+ spec[:default].to_s
459
+ end
460
+
461
+ if spec[:default]
462
+ if spec[:desc] =~ /\.$/
463
+ " (Default: #{default_s})"
464
+ else
465
+ " (default: #{default_s})"
466
+ end
467
+ else
468
+ ""
469
+ end
470
+ end
471
+ stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
472
+ end
473
+ end
474
+
475
+ def width #:nodoc:
476
+ @width ||= if $stdout.tty?
477
+ begin
478
+ require 'curses'
479
+ Curses::init_screen
480
+ x = Curses::cols
481
+ Curses::close_screen
482
+ x
483
+ rescue Exception
484
+ 80
485
+ end
486
+ else
487
+ 80
488
+ end
489
+ end
490
+
491
+ def wrap str, opts={} # :nodoc:
492
+ if str == ""
493
+ [""]
494
+ else
495
+ str.split("\n").map { |s| wrap_line s, opts }.flatten
496
+ end
497
+ end
498
+
499
+ private
500
+
501
+ ## yield successive arg, parameter pairs
502
+ def each_arg args
503
+ remains = []
504
+ i = 0
505
+
506
+ until i >= args.length
507
+ if @stop_words.member? args[i]
508
+ remains += args[i .. -1]
509
+ return remains
510
+ end
511
+ case args[i]
512
+ when /^--$/ # arg terminator
513
+ remains += args[(i + 1) .. -1]
514
+ return remains
515
+ when /^--(\S+?)=(.*)$/ # long argument with equals
516
+ yield "--#{$1}", [$2]
517
+ i += 1
518
+ when /^--(\S+)$/ # long argument
519
+ params = collect_argument_parameters(args, i + 1)
520
+ unless params.empty?
521
+ num_params_taken = yield args[i], params
522
+ unless num_params_taken
523
+ if @stop_on_unknown
524
+ remains += args[i + 1 .. -1]
525
+ return remains
526
+ else
527
+ remains += params
528
+ end
529
+ end
530
+ i += 1 + num_params_taken
531
+ else # long argument no parameter
532
+ yield args[i], nil
533
+ i += 1
534
+ end
535
+ when /^-(\S+)$/ # one or more short arguments
536
+ shortargs = $1.split(//)
537
+ shortargs.each_with_index do |a, j|
538
+ if j == (shortargs.length - 1)
539
+ params = collect_argument_parameters(args, i + 1)
540
+ unless params.empty?
541
+ num_params_taken = yield "-#{a}", params
542
+ unless num_params_taken
543
+ if @stop_on_unknown
544
+ remains += args[i + 1 .. -1]
545
+ return remains
546
+ else
547
+ remains += params
548
+ end
549
+ end
550
+ i += 1 + num_params_taken
551
+ else # argument no parameter
552
+ yield "-#{a}", nil
553
+ i += 1
554
+ end
555
+ else
556
+ yield "-#{a}", nil
557
+ end
558
+ end
559
+ else
560
+ if @stop_on_unknown
561
+ remains += args[i .. -1]
562
+ return remains
563
+ else
564
+ remains << args[i]
565
+ i += 1
566
+ end
567
+ end
568
+ end
569
+
570
+ remains
571
+ end
572
+
573
+ def parse_integer_parameter param, arg
574
+ raise CommandlineError, "option '#{arg}' needs an integer" unless param =~ /^\d+$/
575
+ param.to_i
576
+ end
577
+
578
+ def parse_float_parameter param, arg
579
+ raise CommandlineError, "option '#{arg}' needs a floating-point number" unless param =~ FLOAT_RE
580
+ param.to_f
581
+ end
582
+
583
+ def parse_io_parameter param, arg
584
+ case param
585
+ when /^(stdin|-)$/i; $stdin
586
+ else
587
+ require 'open-uri'
588
+ begin
589
+ open param
590
+ rescue SystemCallError => e
591
+ raise CommandlineError, "file or url for option '#{arg}' cannot be opened: #{e.message}"
592
+ end
593
+ end
594
+ end
595
+
596
+ def collect_argument_parameters args, start_at
597
+ params = []
598
+ pos = start_at
599
+ while args[pos] && args[pos] !~ PARAM_RE && !@stop_words.member?(args[pos]) do
600
+ params << args[pos]
601
+ pos += 1
602
+ end
603
+ params
604
+ end
605
+
606
+ def resolve_default_short_options
607
+ @order.each do |type, name|
608
+ next unless type == :opt
609
+ opts = @specs[name]
610
+ next if opts[:short]
611
+
612
+ c = opts[:long].split(//).find { |d| d !~ INVALID_SHORT_ARG_REGEX && !@short.member?(d) }
613
+ raise ArgumentError, "can't generate a default short option name for #{opts[:long].inspect}: out of unique characters" unless c
614
+
615
+ opts[:short] = c
616
+ @short[c] = name
617
+ end
618
+ end
619
+
620
+ def wrap_line str, opts={}
621
+ prefix = opts[:prefix] || 0
622
+ width = opts[:width] || (self.width - 1)
623
+ start = 0
624
+ ret = []
625
+ until start > str.length
626
+ nextt =
627
+ if start + width >= str.length
628
+ str.length
629
+ else
630
+ x = str.rindex(/\s/, start + width)
631
+ x = str.index(/\s/, start) if x && x < start
632
+ x || str.length
633
+ end
634
+ ret << (ret.empty? ? "" : " " * prefix) + str[start ... nextt]
635
+ start = nextt + 1
636
+ end
637
+ ret
638
+ end
639
+
640
+ ## instance_eval but with ability to handle block arguments
641
+ ## thanks to why: http://redhanded.hobix.com/inspect/aBlockCostume.html
642
+ def cloaker &b
643
+ (class << self; self; end).class_eval do
644
+ define_method :cloaker_, &b
645
+ meth = instance_method :cloaker_
646
+ remove_method :cloaker_
647
+ meth
648
+ end
649
+ end
650
+ end
651
+
652
+ ## The top-level entry method into Trollop. Creates a Parser object,
653
+ ## passes the block to it, then parses +args+ with it, handling any
654
+ ## errors or requests for help or version information appropriately (and
655
+ ## then exiting). Modifies +args+ in place. Returns a hash of option
656
+ ## values.
657
+ ##
658
+ ## The block passed in should contain zero or more calls to +opt+
659
+ ## (Parser#opt), zero or more calls to +text+ (Parser#text), and
660
+ ## probably a call to +version+ (Parser#version).
661
+ ##
662
+ ## The returned block contains a value for every option specified with
663
+ ## +opt+. The value will be the value given on the commandline, or the
664
+ ## default value if the option was not specified on the commandline. For
665
+ ## every option specified on the commandline, a key "<option
666
+ ## name>_given" will also be set in the hash.
667
+ ##
668
+ ## Example:
669
+ ##
670
+ ## require 'trollop'
671
+ ## opts = Trollop::options do
672
+ ## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
673
+ ## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
674
+ ## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
675
+ ## opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
676
+ ## end
677
+ ##
678
+ ## ## if called with no arguments
679
+ ## p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
680
+ ##
681
+ ## ## if called with --monkey
682
+ ## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
683
+ ##
684
+ ## See more examples at http://trollop.rubyforge.org.
685
+ def options args = ARGV, *a, &b
686
+ @p = Parser.new(*a, &b)
687
+ begin
688
+ vals = @p.parse args
689
+ args.clear
690
+ @p.leftovers.each { |l| args << l }
691
+ vals
692
+ rescue CommandlineError => e
693
+ $stderr.puts "Error: #{e.message}."
694
+ $stderr.puts "Try --help for help."
695
+ exit(-1)
696
+ rescue HelpNeeded
697
+ @p.educate
698
+ exit
699
+ rescue VersionNeeded
700
+ puts @p.version
701
+ exit
702
+ end
703
+ end
704
+
705
+ ## Informs the user that their usage of 'arg' was wrong, as detailed by
706
+ ## 'msg', and dies. Example:
707
+ ##
708
+ ## options do
709
+ ## opt :volume, :default => 0.0
710
+ ## end
711
+ ##
712
+ ## die :volume, "too loud" if opts[:volume] > 10.0
713
+ ## die :volume, "too soft" if opts[:volume] < 0.1
714
+ ##
715
+ ## In the one-argument case, simply print that message, a notice
716
+ ## about -h, and die. Example:
717
+ ##
718
+ ## options do
719
+ ## opt :whatever # ...
720
+ ## end
721
+ ##
722
+ ## Trollop::die "need at least one filename" if ARGV.empty?
723
+ def die arg, msg=nil
724
+ if msg
725
+ $stderr.puts "Error: argument --#{@p.specs[arg][:long]} #{msg}."
726
+ else
727
+ $stderr.puts "Error: #{arg}."
728
+ end
729
+ $stderr.puts "Try --help for help."
730
+ exit(-1)
731
+ end
732
+
733
+ module_function :options, :die
734
+
735
+ end # module