qoobaa-user-choices 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +39 -0
- data/README.rdoc +7 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/examples/older/README.txt +133 -0
- data/examples/older/command-line.rb +46 -0
- data/examples/older/default-values.rb +41 -0
- data/examples/older/multiple-sources.rb +58 -0
- data/examples/older/postprocess.rb +39 -0
- data/examples/older/switches.rb +44 -0
- data/examples/older/two-args.rb +31 -0
- data/examples/older/types.rb +61 -0
- data/examples/tutorial/css/LICENSE.txt +1 -0
- data/examples/tutorial/css/bg2.gif +0 -0
- data/examples/tutorial/css/left.gif +0 -0
- data/examples/tutorial/css/left_on.gif +0 -0
- data/examples/tutorial/css/main.css +242 -0
- data/examples/tutorial/css/right.gif +0 -0
- data/examples/tutorial/css/right_on.gif +0 -0
- data/examples/tutorial/css/tvline.gif +0 -0
- data/examples/tutorial/css/von-foerster.jpg +0 -0
- data/examples/tutorial/css/von-foerster2.jpg +0 -0
- data/examples/tutorial/index.html +703 -0
- data/examples/tutorial/tutorial1.rb +41 -0
- data/examples/tutorial/tutorial2.rb +44 -0
- data/examples/tutorial/tutorial3.rb +47 -0
- data/examples/tutorial/tutorial4.rb +47 -0
- data/examples/tutorial/tutorial5.rb +35 -0
- data/examples/tutorial/tutorial6.rb +35 -0
- data/examples/tutorial/tutorial7.rb +41 -0
- data/lib/user-choices.rb +131 -0
- data/lib/user-choices/arglist-strategies.rb +179 -0
- data/lib/user-choices/builder.rb +118 -0
- data/lib/user-choices/command-line-source.rb +224 -0
- data/lib/user-choices/command.rb +42 -0
- data/lib/user-choices/conversions.rb +169 -0
- data/lib/user-choices/ruby-extensions.rb +20 -0
- data/lib/user-choices/sources.rb +278 -0
- data/lib/user-choices/version.rb +3 -0
- data/test/arglist_strategy_test.rb +42 -0
- data/test/builder_test.rb +631 -0
- data/test/command_line_source_test.rb +443 -0
- data/test/conversion_test.rb +172 -0
- data/test/source_test.rb +451 -0
- data/test/test_helper.rb +9 -0
- data/test/user_choices_slow_test.rb +276 -0
- data/user-choices.gemspec +104 -0
- metadata +122 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
This software is Copyright (C) 2007 by Brian Marick
|
2
|
+
<marick@exampler.com>. It is licensed according to "the Ruby
|
3
|
+
license". Specifically:
|
4
|
+
|
5
|
+
You can redistribute it and/or modify it under either the terms of the
|
6
|
+
GNU General Public License, version 2,
|
7
|
+
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> or the
|
8
|
+
conditions below:
|
9
|
+
|
10
|
+
1. You may make and give away verbatim copies of the source form of the
|
11
|
+
software without restriction, provided that you duplicate all of the
|
12
|
+
original copyright notices and associated disclaimers.
|
13
|
+
|
14
|
+
2. You may modify your copy of the software in any way, provided that
|
15
|
+
you do at least ONE of the following:
|
16
|
+
|
17
|
+
a) place your modifications in the Public Domain or otherwise
|
18
|
+
make them Freely Available, such as by posting said
|
19
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
20
|
+
the author to include your modifications in the software.
|
21
|
+
|
22
|
+
b) use the modified software only within your corporation or
|
23
|
+
organization.
|
24
|
+
|
25
|
+
c) make other distribution arrangements with the author.
|
26
|
+
|
27
|
+
3. You may modify and include part of the software into any other
|
28
|
+
software (possibly commercial).
|
29
|
+
|
30
|
+
4. Text or files supplied as input to or produced as output from
|
31
|
+
the software do not automatically fall under the copyright of the
|
32
|
+
software, but belong to whomever generated them, and may be sold
|
33
|
+
commercially, and may be aggregated with this software.
|
34
|
+
|
35
|
+
5. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
36
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
37
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
38
|
+
PURPOSE.
|
39
|
+
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "user-choices"
|
8
|
+
gem.summary = %Q{Unified interface to command-line, environment, and configuration files.}
|
9
|
+
gem.email = "marick@exampler.com"
|
10
|
+
gem.homepage = "http://github.com/qoobaa/user-choices"
|
11
|
+
gem.authors = ["Brian Marick"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
if File.exist?('VERSION.yml')
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "user-choices #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.7
|
@@ -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,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
require 'user-choices'
|
5
|
+
|
6
|
+
class CommandLineExample < UserChoices::Command
|
7
|
+
|
8
|
+
# The _sources_ are the various places in which the user can
|
9
|
+
# describe her choices to the program. In this case, there's
|
10
|
+
# only the command line.
|
11
|
+
|
12
|
+
def add_sources(builder)
|
13
|
+
builder.add_source(UserChoices::CommandLineSource, :usage,
|
14
|
+
"Usage: ruby #{$0} [options] names...")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Each individual choice is named with a symbol. The block
|
18
|
+
# describes the command line options corresponding to the choice, plus
|
19
|
+
# any help text for that option. The arguments to uses_options are passed
|
20
|
+
# on to OptionParser.
|
21
|
+
|
22
|
+
def add_choices(builder)
|
23
|
+
builder.add_choice(:choice) { | command_line |
|
24
|
+
command_line.uses_option("-c", "--choice CHOICE",
|
25
|
+
"CHOICE can be any string.")
|
26
|
+
}
|
27
|
+
|
28
|
+
# The argument list to the command is interpreted as another
|
29
|
+
# option. In this case, it's a list of strings.
|
30
|
+
builder.add_choice(:names) { | command_line |
|
31
|
+
command_line.uses_arglist
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Perform the command.
|
36
|
+
def execute
|
37
|
+
pp @user_choices
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
if $0 == __FILE__
|
43
|
+
S4tUtils.with_pleasant_exceptions do
|
44
|
+
CommandLineExample.new.execute
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
require 'user-choices'
|
5
|
+
|
6
|
+
class ArgNotingCommand < UserChoices::Command
|
7
|
+
|
8
|
+
def add_sources(builder)
|
9
|
+
builder.add_source(UserChoices::CommandLineSource, :usage,
|
10
|
+
"Usage: ruby #{$0} [options] [name]")
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_choices(builder)
|
14
|
+
# This example shows how you can specify a default value for an
|
15
|
+
# option.
|
16
|
+
builder.add_choice(:choice,
|
17
|
+
:default => 'default') { | command_line |
|
18
|
+
command_line.uses_option("-c", "--choice CHOICE",
|
19
|
+
"CHOICE can be any string.")
|
20
|
+
}
|
21
|
+
|
22
|
+
# uses_optional_arg allows either zero or one arguments. If an
|
23
|
+
# argument is given, it is directly the value of user_choices[key]
|
24
|
+
# (rather than being stored as a single-element array).
|
25
|
+
builder.add_choice(:name) { | command_line |
|
26
|
+
command_line.uses_optional_arg
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Perform the command.
|
31
|
+
def execute
|
32
|
+
pp @user_choices
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
if $0 == __FILE__
|
38
|
+
S4tUtils.with_pleasant_exceptions do
|
39
|
+
ArgNotingCommand.new.execute
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
require 'user-choices'
|
5
|
+
|
6
|
+
class MultipleSourcesExample < UserChoices::Command
|
7
|
+
include UserChoices
|
8
|
+
|
9
|
+
# Here are the four sources currently available.
|
10
|
+
#
|
11
|
+
# EnvironmentSource is initialized with a prefix. If a choice is
|
12
|
+
# named "foo" and the prefix is "ms_", then the value of
|
13
|
+
# ENV["ms_foo"] initializes user_choices[:foo].
|
14
|
+
#
|
15
|
+
# YamlConfigFileSource reads from a given YAML file. The choices in the
|
16
|
+
# config file have the same spelling as the choice name (without the
|
17
|
+
# colon that makes the choice name a symbol).
|
18
|
+
#
|
19
|
+
# XmlConfigFileSource reads from a given XML file. The choices in the
|
20
|
+
# config file have the same spelling as the choice name (without the
|
21
|
+
# colon that makes the choice name a symbol).
|
22
|
+
#
|
23
|
+
# CommandLineSource uses the command line (including the argument list).
|
24
|
+
# Much of the initialization is done with a block attached to add_choice.
|
25
|
+
#
|
26
|
+
# Sources are added in descending order of precedence.
|
27
|
+
|
28
|
+
def add_sources(builder)
|
29
|
+
builder.add_source(CommandLineSource, :usage,
|
30
|
+
"Usage: ruby #{$0} [options] names...")
|
31
|
+
builder.add_source(EnvironmentSource, :with_prefix, "ms_")
|
32
|
+
builder.add_source(YamlConfigFileSource, :from_file, "ms-config.yml")
|
33
|
+
builder.add_source(XmlConfigFileSource, :from_file, "ms-config.xml")
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_choices(builder)
|
37
|
+
builder.add_choice(:ordinary_choice,
|
38
|
+
:default => 'default') { | command_line |
|
39
|
+
command_line.uses_option("-o", "--ordinary-choice CHOICE",
|
40
|
+
"CHOICE can be any string.")
|
41
|
+
}
|
42
|
+
|
43
|
+
builder.add_choice(:names) { | command_line |
|
44
|
+
command_line.uses_arglist
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def execute
|
49
|
+
pp @user_choices
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
if $0 == __FILE__
|
55
|
+
S4tUtils.with_pleasant_exceptions do
|
56
|
+
MultipleSourcesExample.new.execute
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
require 'user-choices'
|
5
|
+
|
6
|
+
class ArgNotingCommand < UserChoices::Command
|
7
|
+
|
8
|
+
def add_sources(builder)
|
9
|
+
builder.add_source(UserChoices::CommandLineSource, :usage,
|
10
|
+
"Usage: ruby #{$0} [options] infile outfile")
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_choices(builder)
|
15
|
+
builder.add_choice(:args, :length => 2) { | command_line |
|
16
|
+
command_line.uses_arglist
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
# postprocess_user_choices gives the program the opportunity to
|
21
|
+
# do something about choices immediately after they are made. This method
|
22
|
+
# runs only once per invocation, whereas the execute() method can
|
23
|
+
# execute several times. This method will often set instance variables.
|
24
|
+
def postprocess_user_choices
|
25
|
+
@user_choices[:infile] = @user_choices[:args][0]
|
26
|
+
@user_choices[:outfile] = @user_choices[:args][1]
|
27
|
+
end
|
28
|
+
|
29
|
+
def execute
|
30
|
+
pp @user_choices
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
if $0 == __FILE__
|
36
|
+
S4tUtils.with_pleasant_exceptions do
|
37
|
+
ArgNotingCommand.new.execute
|
38
|
+
end
|
39
|
+
end
|