user-choices 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/History.txt +17 -0
  2. data/LICENSE.txt +34 -0
  3. data/Manifest.txt +40 -0
  4. data/README.txt +1 -0
  5. data/Rakefile +19 -0
  6. data/Rakefile.hoe +24 -0
  7. data/examples/older/README.txt +133 -0
  8. data/examples/older/command-line.rb +51 -0
  9. data/examples/older/default-values.rb +47 -0
  10. data/examples/older/multiple-sources.rb +63 -0
  11. data/examples/older/postprocess.rb +45 -0
  12. data/examples/older/switches.rb +50 -0
  13. data/examples/older/two-args.rb +37 -0
  14. data/examples/older/types.rb +67 -0
  15. data/examples/tutorial/index.html +648 -0
  16. data/examples/tutorial/tutorial1.rb +48 -0
  17. data/examples/tutorial/tutorial2.rb +52 -0
  18. data/examples/tutorial/tutorial3.rb +55 -0
  19. data/examples/tutorial/tutorial4.rb +55 -0
  20. data/examples/tutorial/tutorial5.rb +42 -0
  21. data/examples/tutorial/tutorial6.rb +42 -0
  22. data/examples/tutorial/tutorial7.rb +48 -0
  23. data/lib/user-choices/arglist-strategies.rb +178 -0
  24. data/lib/user-choices/builder.rb +89 -0
  25. data/lib/user-choices/command-line-source.rb +220 -0
  26. data/lib/user-choices/command.rb +42 -0
  27. data/lib/user-choices/conversions.rb +154 -0
  28. data/lib/user-choices/ruby-extensions.rb +20 -0
  29. data/lib/user-choices/sources.rb +269 -0
  30. data/lib/user-choices/version.rb +3 -0
  31. data/lib/user-choices.rb +131 -0
  32. data/setup.rb +1585 -0
  33. data/test/arglist-strategy-tests.rb +42 -0
  34. data/test/builder-tests.rb +569 -0
  35. data/test/command-line-source-tests.rb +443 -0
  36. data/test/conversion-tests.rb +157 -0
  37. data/test/set-standalone-test-paths.rb +5 -0
  38. data/test/source-tests.rb +442 -0
  39. data/test/user-choices-slowtests.rb +274 -0
  40. data/user-choices.tmproj +575 -0
  41. metadata +138 -0
data/History.txt ADDED
@@ -0,0 +1,17 @@
1
+ Version 1.1.0
2
+ * Handles YAML files as well as XML.
3
+ * Minor, incompatible changes to the API.
4
+ * Command-line arglist handling now works with other sources the
5
+ way options do. In particular, an empty arglist can, for example,
6
+ have its value given by defaults rather than locking the value
7
+ of the choice down to the empty array.
8
+ * Command-line arglists used to have their length specified independently
9
+ of all other sources. Now there's a :length "type" that applies to all
10
+ sources, including the command line. From the user's point of view,
11
+ though, wrong-sized arglists look like they're caught by OptionParser.
12
+ * These relatively minor changes required a bigger overhaul to the
13
+ internals. They're less stupid, but I suspect they're still stupid.
14
+ * A tutorial.
15
+
16
+ Version 1.0.0
17
+ * Version from _Everyday Scripting with Ruby_
data/LICENSE.txt ADDED
@@ -0,0 +1,34 @@
1
+ This software is Copyright (C) 2007 by Brian Marick <marick@exampler.com>. It is licensed according to "the Ruby license". Specifically:
2
+
3
+ You can redistribute it and/or modify it under either the terms of the GNU General Public License, version 2, <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) make other distribution arrangements with the author.
21
+
22
+ 3. You may modify and include part of the software into any other
23
+ software (possibly commercial).
24
+
25
+ 4. Text or files supplied as input to or produced as output from
26
+ the software do not automatically fall under the copyright of the
27
+ software, but belong to whomever generated them, and may be sold
28
+ commercially, and may be aggregated with this software.
29
+
30
+ 5. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
31
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33
+ PURPOSE.
34
+
data/Manifest.txt ADDED
@@ -0,0 +1,40 @@
1
+ History.txt
2
+ LICENSE.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ Rakefile.hoe
7
+ examples/older/README.txt
8
+ examples/older/command-line.rb
9
+ examples/older/default-values.rb
10
+ examples/older/multiple-sources.rb
11
+ examples/older/postprocess.rb
12
+ examples/older/switches.rb
13
+ examples/older/two-args.rb
14
+ examples/older/types.rb
15
+ examples/tutorial/index.html
16
+ examples/tutorial/tutorial1.rb
17
+ examples/tutorial/tutorial2.rb
18
+ examples/tutorial/tutorial3.rb
19
+ examples/tutorial/tutorial4.rb
20
+ examples/tutorial/tutorial5.rb
21
+ examples/tutorial/tutorial6.rb
22
+ examples/tutorial/tutorial7.rb
23
+ lib/user-choices.rb
24
+ lib/user-choices/arglist-strategies.rb
25
+ lib/user-choices/builder.rb
26
+ lib/user-choices/command-line-source.rb
27
+ lib/user-choices/command.rb
28
+ lib/user-choices/conversions.rb
29
+ lib/user-choices/ruby-extensions.rb
30
+ lib/user-choices/sources.rb
31
+ lib/user-choices/version.rb
32
+ setup.rb
33
+ test/arglist-strategy-tests.rb
34
+ test/builder-tests.rb
35
+ test/command-line-source-tests.rb
36
+ test/conversion-tests.rb
37
+ test/set-standalone-test-paths.rb
38
+ test/source-tests.rb
39
+ test/user-choices-slowtests.rb
40
+ user-choices.tmproj
data/README.txt ADDED
@@ -0,0 +1 @@
1
+ The best introduction to user-choices is the tutorial[http://user-choices.rubyforge.org].
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # This is a Makefile that would live in the root folder for a package
2
+ # that follows the Scripting for Testers conventions.
3
+
4
+ $started_from_rakefile = true
5
+
6
+ PACKAGE_ROOT = Dir.pwd
7
+ $:.unshift("#{PACKAGE_ROOT}/lib")
8
+ require 's4t-utils/load-path-auto-adjuster'
9
+
10
+ require 's4t-utils'
11
+ include S4tUtils
12
+
13
+ MyFileSystemName='user-choices' # No ".rb" extension.
14
+ MyModuleName='UserChoices'
15
+ MyRdocFiles = FileList.new("lib/#{MyFileSystemName}.rb",
16
+ "lib/#{MyFileSystemName}/**/*.rb").exclude('**/third-party/**')
17
+
18
+ require 's4t-utils/rakefile-common'
19
+
data/Rakefile.hoe ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Brian Marick on 2007-07-03.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require 'hoe'
7
+ require 'lib/user-choices/version'
8
+
9
+ Hoe.new("user-choices", UserChoices::Version) do |p|
10
+ p.rubyforge_name = "user-choices"
11
+ p.changes = "See History.txt"
12
+ p.author = "Brian Marick"
13
+ p.description = "Unified interface to command-line, environment, and configuration files."
14
+ p.summary = p.description
15
+ p.email = "marick@exampler.com"
16
+ p.extra_deps = [['xmp-simple', '>= 1.0.11'],
17
+ ['s4t-utils', '>= 1.0.2'],
18
+ ['extensions', '>= 0.6.0'], # for testing
19
+ ['builder' '>= 2.1.2']] # for testing
20
+ p.test_globs = "test/**/*-tests.rb"
21
+ p.rdoc_pattern = %r{README.txt|History.txt|lib/user-choices.rb|lib/user-choices/.+\.rb}
22
+ p.url = "http://user-choices.rubyforge.org"
23
+ p.remote_rdoc_dir = 'rdoc'
24
+ end
@@ -0,0 +1,133 @@
1
+ Here are brief instructions for using the different scripts in this directory. The tutorial files aren't described here; instead, the tutorial (on the website) describes them.
2
+
3
+
4
+ == Command-line arguments ==
5
+
6
+ + Help is generated for you:
7
+
8
+ prompt> ruby command-line.rb --help
9
+
10
+ + Options are placed into a hash, as is the argument list
11
+
12
+ prompt> ruby command-line.rb --choice cho sophie paul dawn me
13
+
14
+ + The usual variant ways of specifying options
15
+
16
+ prompt> ruby command-line.rb -c choice
17
+ prompt> ruby command-line.rb -cchoice
18
+ prompt> ruby command-line.rb --choi choice
19
+ prompt> ruby command-line.rb --choi choice -- -name1- -name2-
20
+
21
+ + There are error messages for extra and malformed options
22
+
23
+ prompt> ruby command-line.rb --choice
24
+ prompt> ruby command-line.rb --other 3 -- choice
25
+
26
+ == Default values ==
27
+
28
+ + You can specify default values when you define choices.
29
+
30
+ prompt> ruby default-values.rb --choice specific
31
+ prompt> ruby default-values.rb
32
+
33
+ + Note: defaulting behavior applies to other sources of choices, not
34
+ just command lines.
35
+
36
+ == Optional arguments ==
37
+
38
+ + When single optional arguments are required, the result is not a
39
+ list - it's either the value or nothing.
40
+
41
+ prompt> ruby default-values.rb only-arg
42
+
43
+ == Type checks and conversions
44
+
45
+ + There are ways to describe what kind of thing an argument
46
+ is. (Currently limited.)
47
+
48
+ prompt> ruby types.rb --a-or-b not-a argument
49
+ prompt> ruby types.rb --must-be-integer 1d argument
50
+ prompt> ruby types.rb --option-list first,second argument
51
+
52
+ + Integer-valued options are converted into integers, not left as
53
+ strings:
54
+
55
+ prompt> ruby types.rb --must-be-integer 3 argument
56
+
57
+ + Type-checking and type conversions apply to all sources, not just
58
+ command lines.
59
+
60
+ == Required Arguments ==
61
+
62
+ + You can require exactly one argument:
63
+
64
+ prompt> ruby types.rb
65
+ prompt> ruby types.rb argument extra-argument
66
+
67
+ == Switches (values without arguments) ==
68
+
69
+ + Switches can also be specified. Typically, their values are either
70
+ true or false (rather than strings or numbers).
71
+
72
+ + How they are given on the command line may be unfamiliar to you:
73
+
74
+ prompt> ruby switches.rb --help
75
+
76
+ + Examples of use:
77
+
78
+ prompt> ruby switches.rb 1 2
79
+ prompt> ruby switches.rb --switch 1 2
80
+ prompt> ruby switches.rb -s 2 1
81
+ prompt> ruby switches.rb --no-switch 1 2
82
+
83
+ == A range of arguments ==
84
+
85
+ + You can require that the number of arguments be in a range. In this
86
+ case, it's 2-4:
87
+
88
+ prompt> ruby switches.rb 1
89
+ prompt> ruby switches.rb 1 2
90
+ prompt> ruby switches.rb 1 2 3 4
91
+ prompt> ruby switches.rb 1 2 3 4 5
92
+
93
+ + You can also require any particular exact number of arguments:
94
+
95
+ prompt> ruby two-args.rb 1
96
+ prompt> ruby two-args.rb 1 2
97
+ prompt> ruby two-args.rb 1 2 3
98
+
99
+ == Postprocessing ==
100
+
101
+ + You can also postprocess choices into a form more convenient for the
102
+ rest of the program.
103
+
104
+ prompt> ruby postprocess.rb 1 2
105
+
106
+ == Multiple Sources ==
107
+
108
+ + First see what happens when no arguments are given.
109
+
110
+ prompt> ruby command-line.rb
111
+
112
+ + Create a file named ms-config.xml in your home / login folder, and
113
+ put the following in it. (Note the underscore in "ordinary_choice" -
114
+ this is the name of the choice, not the representation of that choice
115
+ on the command line.)
116
+
117
+ <config>
118
+ <ordinary_choice>greetings</ordinary_choice>
119
+ </config>
120
+
121
+ prompt> ruby multiple-sources.rb
122
+
123
+ Set an environment variable ms_ordinary_choice with value 'hi'.
124
+
125
+ prompt> ruby multiple-sources.rb
126
+
127
+ Which takes precedence, the environment or the configuration file?
128
+ (This is configurable.)
129
+
130
+ The command line arguments have highest precedence of all:
131
+
132
+ prompt> ruby multiple-sources.rb --ordinary-choice hello
133
+
@@ -0,0 +1,51 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+ require 'pp'
9
+ require 'user-choices'
10
+
11
+ class CommandLineExample < UserChoices::Command
12
+
13
+ # The _sources_ are the various places in which the user can
14
+ # describe her choices to the program. In this case, there's
15
+ # only the command line.
16
+
17
+ def add_sources(builder)
18
+ builder.add_source(UserChoices::CommandLineSource, :usage,
19
+ "Usage: ruby #{$0} [options] names...")
20
+ end
21
+
22
+ # Each individual choice is named with a symbol. The block
23
+ # describes the command line options corresponding to the choice, plus
24
+ # any help text for that option. The arguments to uses_options are passed
25
+ # on to OptionParser.
26
+
27
+ def add_choices(builder)
28
+ builder.add_choice(:choice) { | command_line |
29
+ command_line.uses_option("-c", "--choice CHOICE",
30
+ "CHOICE can be any string.")
31
+ }
32
+
33
+ # The argument list to the command is interpreted as another
34
+ # option. In this case, it's a list of strings.
35
+ builder.add_choice(:names) { | command_line |
36
+ command_line.uses_arglist
37
+ }
38
+ end
39
+
40
+ # Perform the command.
41
+ def execute
42
+ pp @user_choices
43
+ end
44
+ end
45
+
46
+
47
+ if $0 == __FILE__
48
+ S4tUtils.with_pleasant_exceptions do
49
+ CommandLineExample.new.execute
50
+ end
51
+ end
@@ -0,0 +1,47 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+
9
+ require 'pp'
10
+ require 'user-choices'
11
+
12
+ class ArgNotingCommand < UserChoices::Command
13
+
14
+ def add_sources(builder)
15
+ builder.add_source(UserChoices::CommandLineSource, :usage,
16
+ "Usage: ruby #{$0} [options] [name]")
17
+ end
18
+
19
+ def add_choices(builder)
20
+ # This example shows how you can specify a default value for an
21
+ # option.
22
+ builder.add_choice(:choice,
23
+ :default => 'default') { | command_line |
24
+ command_line.uses_option("-c", "--choice CHOICE",
25
+ "CHOICE can be any string.")
26
+ }
27
+
28
+ # uses_optional_arg allows either zero or one arguments. If an
29
+ # argument is given, it is directly the value of user_choices[key]
30
+ # (rather than being stored as a single-element array).
31
+ builder.add_choice(:name) { | command_line |
32
+ command_line.uses_optional_arg
33
+ }
34
+ end
35
+
36
+ # Perform the command.
37
+ def execute
38
+ pp @user_choices
39
+ end
40
+ end
41
+
42
+
43
+ if $0 == __FILE__
44
+ S4tUtils.with_pleasant_exceptions do
45
+ ArgNotingCommand.new.execute
46
+ end
47
+ end
@@ -0,0 +1,63 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+ require 'pp'
9
+ require 'user-choices'
10
+
11
+ class MultipleSourcesExample < UserChoices::Command
12
+ include UserChoices
13
+
14
+ # Here are the four sources currently available.
15
+ #
16
+ # EnvironmentSource is initialized with a prefix. If a choice is
17
+ # named "foo" and the prefix is "ms_", then the value of
18
+ # ENV["ms_foo"] initializes user_choices[:foo].
19
+ #
20
+ # YamlConfigFileSource reads from a given YAML file. The choices in the
21
+ # config file have the same spelling as the choice name (without the
22
+ # colon that makes the choice name a symbol).
23
+ #
24
+ # XmlConfigFileSource reads from a given XML file. The choices in the
25
+ # config file have the same spelling as the choice name (without the
26
+ # colon that makes the choice name a symbol).
27
+ #
28
+ # CommandLineSource uses the command line (including the argument list).
29
+ # Much of the initialization is done with a block attached to add_choice.
30
+ #
31
+ # Sources are added in descending order of precedence.
32
+
33
+ def add_sources(builder)
34
+ builder.add_source(CommandLineSource, :usage,
35
+ "Usage: ruby #{$0} [options] names...")
36
+ builder.add_source(EnvironmentSource, :with_prefix, "ms_")
37
+ builder.add_source(YamlConfigFileSource, :from_file, "ms-config.yml")
38
+ builder.add_source(XmlConfigFileSource, :from_file, "ms-config.xml")
39
+ end
40
+
41
+ def add_choices(builder)
42
+ builder.add_choice(:ordinary_choice,
43
+ :default => 'default') { | command_line |
44
+ command_line.uses_option("-o", "--ordinary-choice CHOICE",
45
+ "CHOICE can be any string.")
46
+ }
47
+
48
+ builder.add_choice(:names) { | command_line |
49
+ command_line.uses_arglist
50
+ }
51
+ end
52
+
53
+ def execute
54
+ pp @user_choices
55
+ end
56
+ end
57
+
58
+
59
+ if $0 == __FILE__
60
+ S4tUtils.with_pleasant_exceptions do
61
+ MultipleSourcesExample.new.execute
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+
9
+ require 'pp'
10
+ require 'user-choices'
11
+
12
+ class ArgNotingCommand < UserChoices::Command
13
+
14
+ def add_sources(builder)
15
+ builder.add_source(UserChoices::CommandLineSource, :usage,
16
+ "Usage: ruby #{$0} [options] infile outfile")
17
+
18
+ end
19
+
20
+ def add_choices(builder)
21
+ builder.add_choice(:args, :length => 2) { | command_line |
22
+ command_line.uses_arglist
23
+ }
24
+ end
25
+
26
+ # postprocess_user_choices gives the program the opportunity to
27
+ # do something about choices immediately after they are made. This method
28
+ # runs only once per invocation, whereas the execute() method can
29
+ # execute several times. This method will often set instance variables.
30
+ def postprocess_user_choices
31
+ @user_choices[:infile] = @user_choices[:args][0]
32
+ @user_choices[:outfile] = @user_choices[:args][1]
33
+ end
34
+
35
+ def execute
36
+ pp @user_choices
37
+ end
38
+ end
39
+
40
+
41
+ if $0 == __FILE__
42
+ S4tUtils.with_pleasant_exceptions do
43
+ ArgNotingCommand.new.execute
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+
9
+ require 'pp'
10
+ require 'user-choices'
11
+
12
+ class SwitchExample < UserChoices::Command
13
+
14
+ def add_sources(builder)
15
+ builder.add_source(UserChoices::CommandLineSource, :usage,
16
+ "Usage: ruby #{$0} [options] args...",
17
+ "There may be 2-4 arguments.")
18
+
19
+ end
20
+
21
+ # Switches are slightly different than options. (The difference is
22
+ # in how they're invoked, as either --switch or --no-switch.) Almost
23
+ # certainly, you want the switch to be of type :boolean and have a
24
+ # default.
25
+ def add_choices(builder)
26
+ builder.add_choice(:switch,
27
+ :default => false,
28
+ :type => :boolean) { | command_line |
29
+ command_line.uses_switch("--switch", "-s")
30
+ }
31
+
32
+ # You control the allowable length of a choice with the :length
33
+ # keyword argument. It applies to command-line arglists, lists given
34
+ # in configuration files, and the like.
35
+ builder.add_choice(:args, :length => 2..4) { | command_line |
36
+ command_line.uses_arglist
37
+ }
38
+ end
39
+
40
+ def execute
41
+ pp @user_choices
42
+ end
43
+ end
44
+
45
+
46
+ if $0 == __FILE__
47
+ S4tUtils.with_pleasant_exceptions do
48
+ SwitchExample.new.execute
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+
9
+ require 'pp'
10
+ require 'user-choices'
11
+
12
+ class TwoArgExample < UserChoices::Command
13
+
14
+ def add_sources(builder)
15
+ builder.add_source(UserChoices::CommandLineSource, :usage,
16
+ "Usage: ruby #{$0} [options] infile outfile")
17
+
18
+ end
19
+
20
+ def add_choices(builder)
21
+ # You can specify an exact number of array elements required.
22
+ builder.add_choice(:args, :length => 2) { | command_line |
23
+ command_line.uses_arglist
24
+ }
25
+ end
26
+
27
+ def execute
28
+ pp @user_choices
29
+ end
30
+ end
31
+
32
+
33
+ if $0 == __FILE__
34
+ S4tUtils.with_pleasant_exceptions do
35
+ TwoArgExample.new.execute
36
+ end
37
+ end
@@ -0,0 +1,67 @@
1
+ ### The following adjusts the load path so that the correct version of
2
+ ### a self-contained package is found, no matter where the script is
3
+ ### run from.
4
+ require 'pathname'
5
+ $:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
6
+ require 's4t-utils/load-path-auto-adjuster'
7
+
8
+
9
+ require 'pp'
10
+ require 'user-choices'
11
+ include UserChoices
12
+
13
+ class TypesExample < Command
14
+
15
+ def add_sources(builder)
16
+ builder.add_source(CommandLineSource, :usage,
17
+ "Usage: ruby #{$0} [options] arg")
18
+ end
19
+
20
+ def add_choices(builder)
21
+ # This is how you restrict an option argument to one of a list of
22
+ # strings.
23
+ builder.add_choice(:a_or_b,
24
+ :type => ['a', 'b']) { | command_line |
25
+ command_line.uses_option("--a-or-b CHOICE",
26
+ "CHOICE is either 'a' or 'b'")
27
+ }
28
+
29
+ # This is how you insist that an option argument be an integer
30
+ # (in string form). If correctly formatted, the string is turned
31
+ # into an integer. Note that the default value can be either a
32
+ # string or an integer.
33
+ builder.add_choice(:must_be_integer,
34
+ :default => 0,
35
+ :type => :integer) { | command_line |
36
+ command_line.uses_option("--must-be-integer INT")
37
+ }
38
+
39
+ # This is how to tell the builder that the argument is a
40
+ # comma-separated list of options. The declaration is not required
41
+ # for command lines, or lists in a configuration file. Those are
42
+ # already broken out into their constituent elements in the source
43
+ # text, so the builder doesn't have to split a string at comma
44
+ # boundaries. You can declare the type if you want, though.
45
+
46
+ builder.add_choice(:option_list, :type => [:string], :length => 2) { | command_line |
47
+ command_line.uses_option("--option-list OPT,OPT",
48
+ "Comma-separated list of exactly two options.")
49
+ }
50
+
51
+
52
+ builder.add_choice(:arg) { | command_line |
53
+ command_line.uses_arg
54
+ }
55
+ end
56
+
57
+ def execute
58
+ pp @user_choices
59
+ end
60
+ end
61
+
62
+
63
+ if $0 == __FILE__
64
+ S4tUtils.with_pleasant_exceptions do
65
+ TypesExample.new.execute
66
+ end
67
+ end