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
@@ -0,0 +1,131 @@
1
+ require 'user-choices/arglist-strategies'
2
+ require 'user-choices/builder'
3
+ require 'user-choices/command'
4
+ require 'user-choices/command-line-source'
5
+ require "user-choices/conversions"
6
+ require "user-choices/ruby-extensions"
7
+ require 'user-choices/sources'
8
+ require 'user-choices/version'
9
+
10
+ =begin rdoc
11
+
12
+ UserChoices provides a unified interface to more than one source of
13
+ user choices: the command line, environment variables, configuration
14
+ files, and the choice to use program defaults. A typical usage defines allowable choices
15
+ within the framework of a Command object:
16
+
17
+ class Example < Command
18
+
19
+ # The sources are the various places in which the user can
20
+ # describe her choices to the program.
21
+
22
+ def add_sources(builder)
23
+ builder.add_source(...)
24
+ ...
25
+ end
26
+
27
+ # Each individual choice is named with a symbol that is common
28
+ # to all sources.
29
+ def add_choices(builder)
30
+ builder.add_choice(:choice, ...) { | command_line | ... }
31
+ end
32
+
33
+ # Immediately after recording the choices, the program can
34
+ # add new (derived ones) or do any other once-per-program
35
+ # initialization.
36
+ def postprocess_user_choices
37
+ ... @user_choices ...
38
+ end
39
+
40
+ # Perform the command.
41
+ def execute
42
+ ...
43
+ end
44
+ end
45
+
46
+ ...
47
+ CommandLineExample.new.execute
48
+ ...
49
+
50
+ = Describing sources
51
+
52
+ Sources are described by ChoicesBuilder#add_source.
53
+
54
+ EnvironmentSource describes the use of environment variables as sources. The following says that all environment variables beginning with "amazon_" are choices about this program.
55
+
56
+ builder.add_source(EnvironmentSource, :with_prefix, "amazon_")
57
+
58
+ XmlConfigFileSource points to a configuration file with choices.
59
+
60
+ builder.add_source(XmlConfigFileSource, :from_file, "ms-config.xml")
61
+
62
+ CommandLineSource uses the command line options and
63
+ arguments as a source of choices. The following gives the usage line
64
+ for the script:
65
+
66
+ builder.add_source(CommandLineSource, :usage,
67
+ "Usage ruby #{$0} [options] names...")
68
+
69
+ = Describing choices
70
+
71
+ The end result of the process is a hash mapping choices to chosen
72
+ values. Choices are named by symbols. They are described by
73
+ ChoicesBuilder#add_choice. Here are simple examples that
74
+ don't involve the command line.
75
+
76
+ The first just names a choice.
77
+
78
+ builder.add_choice(:ordinary_choice)
79
+
80
+ The second gives a default value:
81
+
82
+ builder.add_choice(:ordinary_choice,
83
+ :default => "eaargh")
84
+
85
+ The second gives a default value and a type. The type is used to check the value and, if appropriate, to convert the value away from a string. Note that the default is always a string regardless of the type.
86
+
87
+ builder.add_choice(:on,
88
+ :default => "false",
89
+ :type => :boolean)
90
+
91
+ = Command line options
92
+
93
+ ChoicesBuilder#add_choice passes a
94
+ CommandLineSource object to a block. That can be used to
95
+ describe the command line. The syntax is the same as OptionParser.
96
+
97
+ In the following, <tt>ordinary_choice</tt> can be specified with either the <tt>-o</tt> or <tt>--ordinary-choice</tt> options. The strings also appear in help messages (automatically produced from <tt>ruby script --help</tt>).
98
+
99
+ builder.add_choice(:ordinary_choice,
100
+ :default => 'default') { | command_line |
101
+ command_line.uses_option("-o", "--ordinary-choice CHOICE",
102
+ "CHOICE can be any string.")
103
+ }
104
+
105
+ The command line's argument list (everything that's not an option) can
106
+ be bundled up into another choice. Here, the arguments become an array
107
+ named by <tt>:names</tt>:
108
+
109
+ builder.add_choice(:names) { | command_line |
110
+ command_line.uses_arglist
111
+ }
112
+
113
+ = Using choices
114
+
115
+ Most often, choices are used within the context of a Command object. They are stored in a hash named by instance variable <tt>@user_choices</tt> (or accessor +user_choices+).
116
+
117
+ class AffinityTripCommand < Command
118
+ ...
119
+ def execute
120
+ starting_url = @strategy.url_for(self.user_choices[:isbn])
121
+ take_trip(starting_url, self.user_choices[:trip_steps])
122
+ end
123
+
124
+ You can construct the hash directly with ChoicesBuilder#build. That's
125
+ not needed or used when using the Command object.
126
+
127
+ =end
128
+
129
+ module UserChoices
130
+ end
131
+