[DRE-commits] [ruby-trollop] 03/06: Imported Upstream version 2.0
Jonas Genannt
jonas at brachium-system.net
Sat Jan 25 22:56:02 UTC 2014
This is an automated email from the git hooks/post-receive script.
hggh-guest pushed a commit to branch master
in repository ruby-trollop.
commit 04a853ff0eb581b0a381a48c8caaeefd37076a44
Author: Jonas Genannt <jonas at brachium-system.net>
Date: Sat Jan 25 23:46:45 2014 +0100
Imported Upstream version 2.0
---
.gitignore | 1 -
FAQ.txt | 100 +++++++++++++++------------
History.txt | 9 +++
README.txt | 30 ++++-----
Rakefile | 45 -------------
lib/trollop.rb | 83 ++++++++++++-----------
metadata.yml | 59 ++++++++++++++++
test/test_trollop.rb | 113 +++++++++++++++++++++----------
www/index.html | 187 ---------------------------------------------------
9 files changed, 259 insertions(+), 368 deletions(-)
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index 1377554..0000000
--- a/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-*.swp
diff --git a/FAQ.txt b/FAQ.txt
index 809b35d..0b379fb 100644
--- a/FAQ.txt
+++ b/FAQ.txt
@@ -2,45 +2,37 @@ Trollop FAQ
-----------
Q: Why is it called "Trollop"?
-A: No reason.
+A: No good reason. Something something option parsing.
Q: Why should I use Trollop?
-A: Because it will take you FEWER LINES OF CODE to do reasonable option parsing
- than any other option parser out there.
-
- Look at this:
+A: Because it will take you fewer lines of code to parse commandline arguments
+ than anything else out there.
+ Like this:
opts = Trollop::options do
opt :monkey, "Use monkey mode"
opt :goat, "Use goat mode", :default => true
opt :num_limbs, "Set number of limbs", :default => 4
end
- That's it. And opts is a hash and you do whatever you want with it.
- Trivial. You don't have to mix option processing code blocks with the
- declarations. You don't have to make a class for every option (what is this,
- Java?). You don't have to write more than 1 line of code per option.
+ That's it. 'opts' will be a hash and you can do whatever you want with it.
+ You don't have to mix processing code with the declarations. You don't have
+ to make a class for every option (what is this, Java?). You don't have to
+ write more than 1 line of code per option.
Plus, you get a beautiful help screen that detects your terminal width and
- wraps appropriately. C'mon, that's hot.
+ wraps appropriately.
Q: What is the philosophy behind Trollop?
-A: Must a commandline option processor have a philosophy?
-
-Q: Seriously now. What is it?
-A: Ok, it's this: Trollop *just* does the parsing and gives you a hash table
- of the result. So whatever fancy logic or constraints you need, you can
- implement by operating on that hash table. Options that disable other
- options, fancy constraints involving multiple sets of values across multiple
- sets of options, etc. are all left for you to do manually.
+A: Trollop does the parsing and gives you a hash table of options. You then
+ write whatever fancy constraint logic you need as regular Ruby code operating
+ on that hash table.
- (Trollop does support limited constraint setting (see #conflicts and
- #depends), but any non-trivial program will need to get fancier.)
+ (Trollop does support limited constraints (see #conflicts and #depends), but
+ any non-trivial program will probably need to get fancier.)
- The result is that using Trollop is pretty simple, and whatever bizarre
- logic you want, you can write yourself. And don't forget, you can call
- Trollop::die to abort the program and give a fancy options-related error
- message.
+ Then if you need to abort and tell the user to fix their command line at any
+ point, you can call #die and Trollop will do that for you in a pretty way.
Q: What happens to the other stuff on the commandline?
A: Anything Trollop doesn't recognize as an option or as an option parameter is
@@ -49,36 +41,58 @@ A: Anything Trollop doesn't recognize as an option or as an option parameter is
Q: Does Trollop support multiple-value arguments?
A: Yes. If you set the :type of an option to something plural, like ":ints",
":strings", ":doubles", ":floats", ":ios", it will accept multiple arguments
- on the commandline and the value will be an array of these.
+ on the commandline, and the value will be an array of the parameters.
Q: Does Trollop support arguments that can be given multiple times?
A: Yes. If you set :multi to true, then the argument can appear multiple times
on the commandline, and the value will be an array of the parameters.
Q: Does Trollop support subcommands?
-A: Yes. You get subcommand support by adding a call #stop_on within the options
- block, and passing the names of the subcommands to it. (See the third
- example on the webpage.) When Trollop encounters one of these subcommands on
- the commandline, it stops processing and returns.
+A: Yes: you can direct Trollop to stop processing when it encounters certain
+ tokens. Then you can re-call Trollop with the subcommand-specific
+ configuration to process the rest of the commandline.
- ARGV at that point will contain the subcommand followed by any subcommand
- options, since Trollop has contained the rest. So you can consume the
- subcommand and call Trollop.options again with the particular options set
- for that subcommand.
+ See the third example on the webpage.
- If you don't know the subcommands ahead of time, you can call
+ (And if you don't know the subcommands ahead of time, you can call
#stop_on_unknown, which will cause Trollop to stop when it encounters any
unknown token. This might be more trouble than its worth if you're also
- passing filenames on the commandline.
-
- It's probably easier to see the example on the webpage than to read all
- that.
+ passing filenames on the commandline.)
Q: Why does Trollop disallow numeric short argument names, like '-1' and '-9'?
A: Because it's ambiguous whether these are arguments or negative integer or
- floating-point parameters to arguments. E.g., what about "-f -3". Is that a
- negative three parameter to -f, or two separate parameters?
+ floating-point parameters to arguments. E.g., is "-f -3" a negative floating
+ point parameter to -f, or two separate arguments?
+
+Q: What was the big change in version 2.0?
+A: The big change was boolean parameter (aka flag) handling. In pre-2.0,
+ not specifying a flag on the commandline would result in the option being set
+ to its default value; specifying it on the commandline would result in the
+ option being set to the opposite of its default value. This was weird for
+ options with a default of true:
+ opt :magic, "Use magic", default: true
+ Using --magic with the above configuration would result in a :magic => false
+ value in the options hash.
+
+ In 2.0, we introduce the GNU-style notion of a --no-x parameter. Now,
+ specifying --x will always set the option :x to true, regardless of its
+ default value, and specifying --no-x will always set the option :x to false,
+ regardless of its default value. The default value only comes into play when
+ neither form is given on the commandline.
+
+ E.g.:
+ opt :magic, "Use magic", :default => true
+
+ Using --magic will result in :magic => true, and --no-magic will result in
+ :magic => false, and neither will result in :magic => true.
+
+ There is one exception: if the option itself starts with a "no_", then you'll
+ get the opposite behavior:
+
+ opt :no_magic, "Don't use magic", :default => true
+
+ Using --magic will result in :no_magic => false, and --no-magic will result in
+ :no_magic => true, and neither will result in :no_magic => true.
+
+
- I could be very clever and detect when there are no arguments that require
- floating-point parameters, and allow such short option names in those cases,
- but opted for simplicity and consistency.
diff --git a/History.txt b/History.txt
index bb2c8df..de3a416 100644
--- a/History.txt
+++ b/History.txt
@@ -1,3 +1,12 @@
+== 2.0 / 2012-08-11
+* Change flag logic: --no-X will always be false, and --X will always be true,
+ regardless of default.
+* For flags that default to true, display --no-X instead of --X in the help
+ menu. Accept both versions on the commandline.
+* Fix a spurious warning
+* Update Rakefile to 1.9
+* Minor documentation fixes
+
== 1.16.2 / 2010-04-06
* Bugfix in Trollop::options. Thanks to Brian C. Thomas for pointing it out.
diff --git a/README.txt b/README.txt
index 7258008..51ddae0 100644
--- a/README.txt
+++ b/README.txt
@@ -4,27 +4,26 @@ by William Morgan (http://masanjin.net/)
Main page: http://trollop.rubyforge.org
-Release announcements and comments: http://all-thing.net/label/trollop
+Release announcements and comments: http://masanjin.net/blog/label/trollop/.
Documentation quickstart: See Trollop.options and then Trollop::Parser#opt.
Also see the examples at http://trollop.rubyforge.org/.
== DESCRIPTION
-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.
+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, and sensible defaults
+for everything you don't specify.
-== FEATURES/PROBLEMS
+== FEATURES
- Dirt-simple usage.
+- Single file. Throw it in lib/ if you don't want to make it a Rubygem dependency.
- Sensible defaults. No tweaking necessary, much tweaking possible.
-- Support for long options, short options, short option bundling, and
- automatic type validation and conversion.
-- Support for subcommands.
+- Support for long options, short options, subcommands, and automatic type validation and
+ conversion.
- Automatic help message generation, wrapped to current screen width.
-- Lots of unit tests.
== REQUIREMENTS
@@ -38,15 +37,14 @@ subcompletion, and sensible defaults for everything you don't specify.
require 'trollop'
opts = Trollop::options do
- opt :monkey, "Use monkey mode" # flag --monkey, default false
- opt :goat, "Use goat mode", :default => true # flag --goat, default true
- opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
- opt :num_thumbs, "Number of thumbs", :type => :int # integer --num-thumbs <i>, default nil
+ opt :monkey, "Use monkey mode" # flag --monkey, default false
+ opt :name, "Monkey name", :type => :string # string --name <s>, default nil
+ opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
end
- p opts # a hash: { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
+ p opts # a hash: { :monkey=>false, :name=>nil, :num_limbs=>4, :help=>false }
== LICENSE
-Copyright (c) 2008--2009 William Morgan. Trollop is distributed under the same
+Copyright (c) 2008--2012 William Morgan. Trollop is distributed under the same
terms as Ruby.
diff --git a/Rakefile b/Rakefile
deleted file mode 100644
index d379118..0000000
--- a/Rakefile
+++ /dev/null
@@ -1,45 +0,0 @@
-require 'rubygems'
-$:.unshift "lib"
-require 'trollop'
-require 'rake/gempackagetask.rb'
-
-spec = Gem::Specification.new do |s|
- s.name = "trollop"
- s.version = Trollop::VERSION
- s.date = Time.now.to_s
- s.email = "wmorgan-trollop at masanjin.net"
- s.authors = ["William Morgan"]
- s.summary = "Trollop is a commandline option parser for Ruby that just gets out of your way."
- s.homepage = "http://trollop.rubyforge.org"
- s.files = %w(lib/trollop.rb test/test_trollop.rb) + Dir["*.txt"]
- s.executables = []
- s.rubyforge_project = "trollop"
- s.description = "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."
-end
-
-WWW_FILES = FileList["www/*"] + %w(README.txt FAQ.txt)
-task :upload_webpage => WWW_FILES do |t|
- sh "rsync -Paz -essh #{t.prerequisites * ' '} wmorgan at rubyforge.org:/var/www/gforge-projects/trollop/"
-end
-
-task :rdoc do |t|
- sh "rdoc lib README.txt History.txt -m README.txt"
-end
-
-task :upload_docs => :rdoc do |t|
- sh "rsync -az -essh doc/* wmorgan at rubyforge.org:/var/www/gforge-projects/trollop/trollop/"
-end
-
-task :test do
- sh %!ruby -w -Ilib:ext:bin:test -e 'require "rubygems"; require "test/unit"; require "test/test_trollop.rb"'!
-end
-
-Rake::GemPackageTask.new(spec) do |pkg|
- pkg.need_tar = true
-end
-
-# vim: syntax=ruby
diff --git a/lib/trollop.rb b/lib/trollop.rb
index 06ee2af..13d567d 100644
--- a/lib/trollop.rb
+++ b/lib/trollop.rb
@@ -7,12 +7,12 @@ require 'date'
module Trollop
-VERSION = "1.16.2"
+VERSION = "2.0"
## Thrown by Parser in the event of a commandline error. Not needed if
## you're using the Trollop::options entry.
class CommandlineError < StandardError; end
-
+
## Thrown by Parser if the user passes in '-h' or '--help'. Handled
## automatically by Trollop#options.
class HelpNeeded < StandardError; end
@@ -154,12 +154,11 @@ class Parser
## a multi-valued argument. for that you have to specify a :type
## as well. (this is how we disambiguate an ambiguous situation;
## see the docs for Parser#opt for details.)
- disambiguated_default =
- if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
- opts[:default].first
- else
- opts[:default]
- end
+ disambiguated_default = if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
+ opts[:default].first
+ else
+ opts[:default]
+ end
type_from_default =
case disambiguated_default
@@ -193,15 +192,11 @@ class Parser
## fill in :long
opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
- opts[:long] =
- case opts[:long]
- when /^--([^-].*)$/
- $1
- when /^[^-]/
- opts[:long]
- else
- raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
- end
+ opts[:long] = case opts[:long]
+ when /^--([^-].*)$/; $1
+ when /^[^-]/; opts[:long]
+ else; raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
+ end
raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
## fill in :short
@@ -295,19 +290,26 @@ class Parser
vals[sym] = [] if opts[:multi] && !opts[:default] # multi arguments default to [], not nil
end
- resolve_default_short_options
+ resolve_default_short_options!
## resolve symbols
given_args = {}
@leftovers = each_arg cmdline do |arg, params|
- sym = case arg
- when /^-([^-])$/
- @short[$1]
- when /^--([^-]\S*)$/
- @long[$1]
+ ## handle --no- forms
+ arg, negative_given = if arg =~ /^--no-([^-]\S*)$/
+ ["--#{$1}", true]
else
- raise CommandlineError, "invalid argument syntax: '#{arg}'"
+ [arg, false]
end
+
+ sym = case arg
+ when /^-([^-])$/; @short[$1]
+ when /^--([^-]\S*)$/; @long[$1] || @long["no-#{$1}"]
+ else; raise CommandlineError, "invalid argument syntax: '#{arg}'"
+ end
+
+ sym = nil if arg =~ /--no-/ # explicitly invalidate --no-no- arguments
+
raise CommandlineError, "unknown argument '#{arg}'" unless sym
if given_args.include?(sym) && !@specs[sym][:multi]
@@ -315,8 +317,8 @@ class Parser
end
given_args[sym] ||= {}
-
given_args[sym][:arg] = arg
+ given_args[sym][:negative_given] = negative_given
given_args[sym][:params] ||= []
# The block returns the number of parameters taken.
@@ -358,8 +360,7 @@ class Parser
## parse parameters
given_args.each do |sym, given_data|
- arg = given_data[:arg]
- params = given_data[:params]
+ arg, params, negative_given = given_data.values_at :arg, :params, :negative_given
opts = @specs[sym]
raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag
@@ -368,7 +369,7 @@ class Parser
case opts[:type]
when :flag
- vals[sym] = !opts[:default]
+ vals[sym] = (sym.to_s =~ /^no_/ ? negative_given : !negative_given)
when :int, :ints
vals[sym] = params.map { |pg| pg.map { |p| parse_integer_parameter p, arg } }
when :float, :floats
@@ -415,19 +416,19 @@ class Parser
# chronic is not available
end
time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
- rescue ArgumentError => e
+ rescue ArgumentError
raise CommandlineError, "option '#{arg}' needs a date"
end
end
## Print the help message to +stream+.
def educate stream=$stdout
- width # just calculate it now; otherwise we have to be careful not to
+ width # hack: calculate it now; otherwise we have to be careful not to
# call this unless the cursor's at the beginning of a line.
-
left = {}
- @specs.each do |name, spec|
+ @specs.each do |name, spec|
left[name] = "--#{spec[:long]}" +
+ (spec[:type] == :flag && spec[:default] ? ", --no-#{spec[:long]}" : "") +
(spec[:short] && spec[:short] != :none ? ", -#{spec[:short]}" : "") +
case spec[:type]
when :flag; ""
@@ -627,7 +628,7 @@ private
params
end
- def resolve_default_short_options
+ def resolve_default_short_options!
@order.each do |type, name|
next unless type == :opt
opts = @specs[name]
@@ -662,7 +663,7 @@ private
end
## instance_eval but with ability to handle block arguments
- ## thanks to why: http://redhanded.hobix.com/inspect/aBlockCostume.html
+ ## thanks to _why: http://redhanded.hobix.com/inspect/aBlockCostume.html
def cloaker &b
(class << self; self; end).class_eval do
define_method :cloaker_, &b
@@ -692,17 +693,16 @@ end
##
## require 'trollop'
## opts = Trollop::options do
-## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
-## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
-## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
-## opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
+## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
+## opt :name, "Monkey name", :type => :string # a string --name <s>, defaulting to nil
+## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
## end
##
## ## if called with no arguments
-## p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
+## p opts # => {:monkey=>false, :name=>nil, :num_limbs=>4, :help=>false}
##
## ## if called with --monkey
-## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
+## p opts # => {:monkey=>true, :name=>nil, :num_limbs=>4, :help=>false, :monkey_given=>true}
##
## See more examples at http://trollop.rubyforge.org.
def options args=ARGV, *a, &b
@@ -728,8 +728,9 @@ end
## end
##
## opts = Trollop::with_standard_exception_handling p do
-## p.parse ARGV
+## o = p.parse ARGV
## raise Trollop::HelpNeeded if ARGV.empty? # show help screen
+## o
## end
##
## Requires passing in the parser object.
diff --git a/metadata.yml b/metadata.yml
new file mode 100644
index 0000000..23532f4
--- /dev/null
+++ b/metadata.yml
@@ -0,0 +1,59 @@
+--- !ruby/object:Gem::Specification
+name: trollop
+version: !ruby/object:Gem::Version
+ version: '2.0'
+ prerelease:
+platform: ruby
+authors:
+- William Morgan
+autorequire:
+bindir: bin
+cert_chain: []
+date: 2012-08-14 00:00:00.000000000 Z
+dependencies: []
+description: ! '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.'
+email: wmorgan-trollop at masanjin.net
+executables: []
+extensions: []
+extra_rdoc_files: []
+files:
+- lib/trollop.rb
+- test/test_trollop.rb
+- FAQ.txt
+- History.txt
+- release-script.txt
+- README.txt
+homepage: http://trollop.rubyforge.org
+licenses: []
+post_install_message:
+rdoc_options: []
+require_paths:
+- lib
+required_ruby_version: !ruby/object:Gem::Requirement
+ none: false
+ requirements:
+ - - ! '>='
+ - !ruby/object:Gem::Version
+ version: '0'
+required_rubygems_version: !ruby/object:Gem::Requirement
+ none: false
+ requirements:
+ - - ! '>='
+ - !ruby/object:Gem::Version
+ version: '0'
+requirements: []
+rubyforge_project: trollop
+rubygems_version: 1.8.23
+signing_key:
+specification_version: 3
+summary: Trollop is a commandline option parser for Ruby that just gets out of your
+ way.
+test_files: []
diff --git a/test/test_trollop.rb b/test/test_trollop.rb
index 687c209..90bcdb4 100644
--- a/test/test_trollop.rb
+++ b/test/test_trollop.rb
@@ -3,6 +3,7 @@
## Copyright:: Copyright 2007 William Morgan
## License:: GNU GPL version 2
+require 'rubygems'
require 'test/unit'
require 'stringio'
require 'trollop'
@@ -41,7 +42,7 @@ class Trollop < ::Test::Unit::TestCase
assert_raise(CommandlineError) { @p.parse(%w(--arg2)) }
assert_raise(CommandlineError) { @p.parse(%w(--arg2 --arg3)) }
end
-
+
## flags that take an argument error unless given one
def test_argflags_demand_args
@p.opt "goodarg", "desc", :type => String
@@ -247,24 +248,66 @@ class Trollop < ::Test::Unit::TestCase
def test_conflicting_longs_detected
assert_nothing_raised { @p.opt "goodarg", "desc", :long => "--goodarg" }
assert_raise(ArgumentError) { @p.opt "badarg", "desc", :long => "--goodarg" }
- end
+ end
## two args can't have the same :short
def test_conflicting_shorts_detected
assert_nothing_raised { @p.opt "goodarg", "desc", :short => "-g" }
assert_raise(ArgumentError) { @p.opt "badarg", "desc", :short => "-g" }
- end
+ end
- def test_flag_defaults
- @p.opt "defaultfalse", "desc"
- @p.opt "defaulttrue", "desc", :default => true
+ ## note: this behavior has changed in trollop 2.0!
+ def test_flag_parameters
+ @p.opt :defaultnone, "desc"
+ @p.opt :defaultfalse, "desc", :default => false
+ @p.opt :defaulttrue, "desc", :default => true
+
+ ## default state
+ opts = @p.parse []
+ assert_equal false, opts[:defaultnone]
+ assert_equal false, opts[:defaultfalse]
+ assert_equal true, opts[:defaulttrue]
+
+ ## specifying turns them on, regardless of default
+ opts = @p.parse %w(--defaultfalse --defaulttrue --defaultnone)
+ assert_equal true, opts[:defaultnone]
+ assert_equal true, opts[:defaultfalse]
+ assert_equal true, opts[:defaulttrue]
+
+ ## using --no- form turns them off, regardless of default
+ opts = @p.parse %w(--no-defaultfalse --no-defaulttrue --no-defaultnone)
+ assert_equal false, opts[:defaultnone]
+ assert_equal false, opts[:defaultfalse]
+ assert_equal false, opts[:defaulttrue]
+ end
+
+ ## note: this behavior has changed in trollop 2.0!
+ def test_flag_parameters_for_inverted_flags
+ @p.opt :no_default_none, "desc"
+ @p.opt :no_default_false, "desc", :default => false
+ @p.opt :no_default_true, "desc", :default => true
+
+ ## default state
opts = @p.parse []
- assert_equal false, opts["defaultfalse"]
- assert_equal true, opts["defaulttrue"]
+ assert_equal false, opts[:no_default_none]
+ assert_equal false, opts[:no_default_false]
+ assert_equal true, opts[:no_default_true]
- opts = @p.parse %w(--defaultfalse --defaulttrue)
- assert_equal true, opts["defaultfalse"]
- assert_equal false, opts["defaulttrue"]
+ ## specifying turns them all on, regardless of default
+ opts = @p.parse %w(--no-default-false --no-default-true --no-default-none)
+ p opts
+ assert_equal true, opts[:no_default_none]
+ assert_equal true, opts[:no_default_false]
+ assert_equal true, opts[:no_default_true]
+
+ ## using dropped-no form turns them all off, regardless of default
+ opts = @p.parse %w(--default-false --default-true --default-none)
+ assert_equal false, opts[:no_default_none]
+ assert_equal false, opts[:no_default_false]
+ assert_equal false, opts[:no_default_true]
+
+ ## disallow double negatives for reasons of sanity preservation
+ assert_raise(CommandlineError) { @p.parse %w(--no-no-default-true) }
end
def test_special_flags_work
@@ -679,27 +722,27 @@ EOM
assert_nothing_raised { @p.conflicts :one, :two }
assert_nothing_raised { @p.parse %w(--one) }
assert_nothing_raised { @p.parse %w(--two) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--one --two) }
+ assert_raises(CommandlineError) { @p.parse %w(--one --two) }
@p.opt :hello
@p.opt :yellow
@p.opt :mellow
@p.opt :jello
@p.conflicts :hello, :yellow, :mellow, :jello
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello --yellow --mellow --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello --mellow --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
- assert_nothing_raised { opts = @p.parse %w(--hello) }
- assert_nothing_raised { opts = @p.parse %w(--jello) }
- assert_nothing_raised { opts = @p.parse %w(--yellow) }
- assert_nothing_raised { opts = @p.parse %w(--mellow) }
+ assert_nothing_raised { @p.parse %w(--hello) }
+ assert_nothing_raised { @p.parse %w(--jello) }
+ assert_nothing_raised { @p.parse %w(--yellow) }
+ assert_nothing_raised { @p.parse %w(--mellow) }
- assert_nothing_raised { opts = @p.parse %w(--mellow --one) }
- assert_nothing_raised { opts = @p.parse %w(--mellow --two) }
+ assert_nothing_raised { @p.parse %w(--mellow --one) }
+ assert_nothing_raised { @p.parse %w(--mellow --two) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--mellow --two --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--one --mellow --two --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
end
def test_conflict_error_messages
@@ -721,7 +764,7 @@ EOM
assert_raises(ArgumentError) { @p.depends :one, :two }
@p.opt :two
assert_nothing_raised { @p.depends :one, :two }
- assert_nothing_raised { opts = @p.parse %w(--one --two) }
+ assert_nothing_raised { @p.parse %w(--one --two) }
assert_raises(CommandlineError) { @p.parse %w(--one) }
assert_raises(CommandlineError) { @p.parse %w(--two) }
@@ -730,17 +773,17 @@ EOM
@p.opt :mellow
@p.opt :jello
@p.depends :hello, :yellow, :mellow, :jello
- assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello --mellow --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello --jello) }
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--hello) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--mellow) }
+ assert_raises(CommandlineError) { @p.parse %w(--hello) }
+ assert_raises(CommandlineError) { @p.parse %w(--mellow) }
- assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello --one --two) }
- assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello --one --two a b c) }
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello --one --two) }
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello --one --two a b c) }
- assert_raises(CommandlineError) { opts = @p.parse %w(--mellow --two --jello --one) }
+ assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello --one) }
end
def test_depend_error_messages
@@ -1049,7 +1092,7 @@ EOM
ARGV.clear
ARGV.unshift "-h"
assert_raises(SystemExit) do
- opts = ::Trollop::options do
+ ::Trollop::options do
opt :potato
end
raise "broken"
@@ -1060,7 +1103,7 @@ EOM
ARGV.clear
ARGV.unshift "-v"
assert_raises(SystemExit) do
- opts = ::Trollop::options do
+ ::Trollop::options do
version "1.2"
opt :potato
end
@@ -1083,7 +1126,7 @@ EOM
def test_simple_interface_handles_die
ARGV.clear
ARGV.unshift "--potato"
- opts = ::Trollop::options do
+ ::Trollop::options do
opt :potato
end
assert_raises(SystemExit) { ::Trollop::die :potato, "is invalid" }
diff --git a/www/index.html b/www/index.html
deleted file mode 100644
index de73f1a..0000000
--- a/www/index.html
+++ /dev/null
@@ -1,187 +0,0 @@
-<html>
-<head>
-<title>Trollop</title>
-<style type="text/css">
-body { width: 800px; margin-left: auto; margin-right: auto; }
-code { font-size: 14px; }
-.ruby { background: #111122; padding: 10px; color: #228822; width: 800px; }
-.ruby .normal { color: #fff; }
-.ruby .comment { color: #99f }
-.ruby .keyword { color: #A44; font-weight: bold; }
-.ruby .method { color: #44f; }
-.ruby .class { color: #0c4; }
-.ruby .module { color: #050; }
-.ruby .punct { color: #FF0; font-weight: bold; }
-.ruby .symbol { color: #ff0; }
-.ruby .string { color: #4f4 }
-.ruby .char { color: #F07; }
-.ruby .ident { color: #fff; }
-.ruby .constant { color: #0c4; }
-.ruby .regex { color: #B66; background: #444; }
-.ruby .number { color: #F99; }
-.ruby .attribute { color: #fc4; }
-.ruby .global { color: #7FB; }
-.ruby .expr { color: #827; }
-.ruby .escape { color: #277; }
-</style>
-</head>
-
-<body>
-
-<h1>Trollop</h1>
-
-<p style="border: solid 1px black; background: #ccc; padding: 0.5em;">
-Current project news: see the <a href="http://all-thing.net/label/trollop">blog</a>.
-</p>
-
-<p>Trollop is a commandline option parser for Ruby that <i>gets out of your
-way</i>. One line of code per option is all you need to write. For that, you get
-a nice automatically-generated help page (fit to your screen size!), robust
-option parsing, command subcompletion, and sensible defaults for everything you
-don't specify.</p>
-
-<p> Reasons to use Trollop:
-<ol>
-<li> It requires <i>fewer lines of code</i> than any other option out there. </li>
-<li> It doesn't require you to subclass some shit just to use a damn option parser.</li>
-</li>
-<li> It's a single file. You don't even need to make it a dependency. Just throw it in <code>lib/</code>.</li>
-<li> This gradient-free webpage. Fight the candy! </li>
-</ol>
-</p>
-
-<p> To install, you have three options:
-<ul>
-<li>Trollop is a single file with no dependencies. You can <a href="http://gitorious.org/trollop/mainline/blobs/raw/master/lib/trollop.rb">just download it</a> and throw it in your code directory.</li>
-<li>You can use rubygems and command your computer to <code>gem install trollop</code>.</li>
-<li>You can download the tarball from the <a href="http://rubyforge.org/projects/trollop/">Trollop Rubyforge page</a>.</p>
-</ul>
-
-<p> To hack, command your computer to <code>git clone git://gitorious.org/trollop/mainline.git</code>, or see the <a href="http://gitorious.org/projects/trollop">Trollop Gitorious page</a>.</p>
-
-<p>To understand, read the examples below, the <a href="FAQ.txt">FAQ</a>, and the <a href="trollop/">API docs</a>.
-
-<h2>Examples</h2>
-<h3>Simple</h3>
-<pre class="ruby"><code><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
-<span class="ident">opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
- <span class="ident">opt</span> <span class="symbol">:monkey</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Use monkey mode</span><span class="punct">"</span> <span class="comment"># flag --monkey, default false</span>
- <span class="ident">opt</span> <span class="symbol">:goat</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Use goat mode</span><span class="punct">",</span> <span class="symbol">:default</span> <span class="punct">=></span> <span class="constant">true</span> <span class="comment"># flag --goat, default true</span>
- <span class="ident">opt</span> <span class="symbol">:num_limbs</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Number of limbs</span><span class="punct">",</span> <span class="symbol">:default</span> <span class="punct">=></span> <span class="number">4</span> <span class="comment"># integer --num-limbs <i>, default to 4</span>
- <span class="ident">opt</span> <span class="symbol">:num_thumbs</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Number of thumbs</span><span class="punct">",</span> <span class="symbol">:type</span> <span class="punct">=></span> <span class="symbol">:int</span> <span class="comment"># integer --num-thumbs <i>, default nil</span>
-<span class="keyword">end</span>
-
-<span class="ident">p</span> <span class="ident">opts</span> <span class="comment"># a hash: { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }</span>
-</code></pre>
-
-<p>
-<ul>
-<li><tt>Trollop::options</tt> returns a hash of values. That's all the output you get.</li>
-<li>Underscores are converted to dashes. <tt>opt :hello_there</tt> corresponds to an option <tt>--hello-there</tt>.
-<li>All options are taken to be boolean flags, defaulting to false, unless you specify a default or a type. The type will be inferred from the default if given, so no need to specify both.</li>
-<li>Short (one-character) option names are created automatically. You can set them manually with <tt>:short</tt>.
-</ul>
-</p>
-
-<h3>Medium</h3>
-<pre class="ruby"><code><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
-<span class="ident">opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
- <span class="ident">version</span> <span class="punct">"</span><span class="string">test 1.2.3 (c) 2008 William Morgan</span><span class="punct">"</span>
- <span class="ident">banner</span> <span class="punct"><<-</span><span class="constant">EOS</span><span class="string">
-Test is an awesome program that does something very, very important.
-
-Usage:
- test [options] <filenames>+
-where [options] are:
-</span><span class="constant">EOS</span>
-
- <span class="ident">opt</span> <span class="symbol">:ignore</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Ignore incorrect values</span><span class="punct">"</span>
- <span class="ident">opt</span> <span class="symbol">:file</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Extra data filename to read in, with a very long option description like this one</span><span class="punct">",</span>
- <span class="symbol">:type</span> <span class="punct">=></span> <span class="constant">String</span>
- <span class="ident">opt</span> <span class="symbol">:volume</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Volume level</span><span class="punct">",</span> <span class="symbol">:default</span> <span class="punct">=></span> <span class="number">3.0</span>
- <span class="ident">opt</span> <span class="symbol">:iters</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Number of iterations</span><span class="punct">",</span> <span class="symbol">:default</span> <span class="punct">=></span> <span class="number">5</span>
-<span class="keyword">end</span>
-<span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="symbol">:volume</span><span class="punct">,</span> <span class="punct">"</span><span class="string">must be non-negative</span><span class="punct">"</span> <span class="keyword">if</span> <span class="ident">opts</span><span class="punct">[</span><span class="symbol">:volume</span><span class="punct">]</span> <span class="punct"><</span> <span class="number">0</span>
-<span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="symbol">:file</span><span class="punct">,</span> <span class="punct">"</span><span class="string">must exist</span><span class="punct">"</span> <span class="keyword">unless</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">exist?</span><span class="punct">(</span><span class="ident">opts</span><span class="punct">[</span><span clas [...]
-</code></pre>
-
-<p>
-<ul>
-<li>You can specify a version and some banner text for the help message. <tt>--help</tt> and <tt>--version</tt> are defined for you.</li>
-<li>Any special error-checking is done by you, on the option hash. You can call <tt>Trollop::die</tt> to die with a nice message.</li>
-</ul>
-</p>
-
-<h3>Sub-commands, a la SVN and Git</h3>
-<pre class="ruby"><code><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
-
-<span class="comment">## Here's a program called "magic". We want this behavior:</span>
-<span class="comment">##</span>
-<span class="comment">## magic delete <fn> => deletes a file</span>
-<span class="comment">## magic copy <fn> => copies a file</span>
-<span class="comment">##</span>
-<span class="comment">## So 'delete' and 'copy' are subcommands.</span>
-<span class="comment">##</span>
-<span class="comment">## There are some global options, which appear to the left of the subcommand.</span>
-<span class="comment">## There are some subcommand options, which appear to the right.</span>
-<span class="comment">##</span>
-<span class="comment">## Subcommand options can be specific to the subcommand. 'delete' might take</span>
-<span class="comment">## different options from 'copy'.</span>
-<span class="comment">##</span>
-<span class="comment">## We do this by calling Trollop twice; one for the global options and once for</span>
-<span class="comment">## the subcommand options. We need to tell Trollop what the subcommands are, so</span>
-<span class="comment">## that it stops processing the global options when it encounters one of them.</span>
-
-<span class="constant">SUB_COMMANDS</span> <span class="punct">=</span> <span class="punct">%w(</span><span class="string">delete copy</span><span class="punct">)</span>
-<span class="ident">global_opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
- <span class="ident">banner</span> <span class="punct">"</span><span class="string">magic file deleting and copying utility</span><span class="punct">"</span>
- <span class="ident">opt</span> <span class="symbol">:dry_run</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Don't actually do anything</span><span class="punct">",</span> <span class="symbol">:short</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">-n</span><span class="punct">"</span>
- <span class="ident">stop_on</span> <span class="constant">SUB_COMMANDS</span>
-<span class="keyword">end</span>
-
-<span class="ident">cmd</span> <span class="punct">=</span> <span class="constant">ARGV</span><span class="punct">.</span><span class="ident">shift</span> <span class="comment"># get the subcommand</span>
-<span class="ident">cmd_opts</span> <span class="punct">=</span> <span class="keyword">case</span> <span class="ident">cmd</span>
- <span class="keyword">when</span> <span class="punct">"</span><span class="string">delete</span><span class="punct">"</span> <span class="comment"># parse delete options</span>
- <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
- <span class="ident">opt</span> <span class="symbol">:force</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Force deletion</span><span class="punct">"</span>
- <span class="keyword">end</span>
- <span class="keyword">when</span> <span class="punct">"</span><span class="string">copy</span><span class="punct">"</span> <span class="comment"># parse copy options</span>
- <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
- <span class="ident">opt</span> <span class="symbol">:double</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Copy twice for safety's sake</span><span class="punct">"</span>
- <span class="keyword">end</span>
- <span class="keyword">else</span>
- <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="punct">"</span><span class="string">unknown subcommand <span class="expr">#{cmd.inspect}</span></span><span class="punct">"</span>
- <span class="keyword">end</span>
-
-<span class="ident">puts</span> <span class="punct">"</span><span class="string">Global options: <span class="expr">#{global_opts.inspect}</span></span><span class="punct">"</span>
-<span class="ident">puts</span> <span class="punct">"</span><span class="string">Subcommand: <span class="expr">#{cmd.inspect}</span></span><span class="punct">"</span>
-<span class="ident">puts</span> <span class="punct">"</span><span class="string">Subcommand options: <span class="expr">#{cmd_opts.inspect}</span></span><span class="punct">"</span>
-<span class="ident">puts</span> <span class="punct">"</span><span class="string">Remaining arguments: <span class="expr">#{ARGV.inspect}</span></span><span class="punct">"</span></code></pre>
-
-<h3>Specialized parsing behavior</h3>
-<p>Sometimes calling <code>Trollop::options</code> does too much for us. In this example, we create the parser without using <code>Trollop::options</code>, and force it to display the help message (and exit) if the user doesn't supply any arguments. The <code>with_standard_exception_handling</code> method takes care of dealing with the <code>VersionNeeded</code>, <code>HelpNeeded</code> and <code>CommandlineError</code> exceptions that <code>Parser#parse</code> throws, but we could handl [...]
-
-<pre class="ruby"><code><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
-<span class="ident">p</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="constant">Parser</span><span class="punct">.</span><span class="ident">new</span> <span class="keyword">do</span>
- <span class="ident">opt</span> <span class="symbol">:monkey</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Use monkey mode</span><span class="punct">"</span> <span class="comment"># a flag --monkey, defaulting to false</span>
- <span class="ident">opt</span> <span class="symbol">:goat</span><span class="punct">,</span> <span class="punct">"</span><span class="string">Use goat mode</span><span class="punct">",</span> <span class="symbol">:default</span> <span class="punct">=></span> <span class="constant">true</span> <span class="comment"># a flag --goat, defaulting to true</span>
-<span class="keyword">end</span>
-
-<span class="ident">opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">with_standard_exception_handling</span> <span class="ident">p</span> <span class="keyword">do</span>
- <span class="ident">p</span><span class="punct">.</span><span class="ident">parse</span> <span class="constant">ARGV</span>
- <span class="keyword">raise</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="constant">HelpNeeded</span> <span class="keyword">if</span> <span class="constant">ARGV</span><span class="punct">.</span><span class="ident">empty?</span> <span class="comment"># show help screen</span>
-<span class="keyword">end</span></code></pre>
-
-<h2> Contributors </h2>
-
-<p>Trollop is brought to you by <a href="http://cs.stanford.edu/~ruby/">William Morgan</a> and by:
-<ul>
-<li>Tuomas Kareinen <tkareine at the gmail dot coms></li>
-<li>Ohad Lutzky <ohad at the lutzky dot nets></li>
-<li>Erik Ostrom <erik at the echographia dot coms></li>
-<li>Will Fitzgerald <will.fitzgerald at the pobox dot coms></li>
-</ul>
-</p>
-
-</body>
-</html>
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-trollop.git
More information about the Pkg-ruby-extras-commits
mailing list