gli_aziz_light 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +17 -0
  6. data/CONTRIBUTING.md +23 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE.txt +201 -0
  9. data/ObjectModel.graffle +1191 -0
  10. data/README.rdoc +109 -0
  11. data/Rakefile +126 -0
  12. data/bin/gli +59 -0
  13. data/bin/report_on_rake_results +10 -0
  14. data/bin/test_all_rubies.sh +6 -0
  15. data/features/gli_executable.feature +90 -0
  16. data/features/gli_init.feature +232 -0
  17. data/features/step_definitions/gli_executable_steps.rb +18 -0
  18. data/features/step_definitions/gli_init_steps.rb +11 -0
  19. data/features/step_definitions/todo_steps.rb +88 -0
  20. data/features/support/env.rb +53 -0
  21. data/features/todo.feature +413 -0
  22. data/features/todo_legacy.feature +128 -0
  23. data/gli.cheat +95 -0
  24. data/gli.gemspec +34 -0
  25. data/gli.rdoc +73 -0
  26. data/lib/gli.rb +35 -0
  27. data/lib/gli/app.rb +286 -0
  28. data/lib/gli/app_support.rb +341 -0
  29. data/lib/gli/command.rb +171 -0
  30. data/lib/gli/command_finder.rb +41 -0
  31. data/lib/gli/command_line_option.rb +34 -0
  32. data/lib/gli/command_line_token.rb +63 -0
  33. data/lib/gli/command_support.rb +181 -0
  34. data/lib/gli/commands/compound_command.rb +42 -0
  35. data/lib/gli/commands/doc.rb +231 -0
  36. data/lib/gli/commands/help.rb +95 -0
  37. data/lib/gli/commands/help_modules/arg_name_formatter.rb +20 -0
  38. data/lib/gli/commands/help_modules/command_finder.rb +60 -0
  39. data/lib/gli/commands/help_modules/command_help_format.rb +156 -0
  40. data/lib/gli/commands/help_modules/global_help_format.rb +70 -0
  41. data/lib/gli/commands/help_modules/help_completion_format.rb +31 -0
  42. data/lib/gli/commands/help_modules/list_formatter.rb +23 -0
  43. data/lib/gli/commands/help_modules/one_line_wrapper.rb +18 -0
  44. data/lib/gli/commands/help_modules/options_formatter.rb +49 -0
  45. data/lib/gli/commands/help_modules/text_wrapper.rb +53 -0
  46. data/lib/gli/commands/help_modules/tty_only_wrapper.rb +23 -0
  47. data/lib/gli/commands/help_modules/verbatim_wrapper.rb +16 -0
  48. data/lib/gli/commands/initconfig.rb +74 -0
  49. data/lib/gli/commands/rdoc_document_listener.rb +116 -0
  50. data/lib/gli/commands/scaffold.rb +401 -0
  51. data/lib/gli/dsl.rb +226 -0
  52. data/lib/gli/exceptions.rb +71 -0
  53. data/lib/gli/flag.rb +68 -0
  54. data/lib/gli/gli_option_block_parser.rb +84 -0
  55. data/lib/gli/gli_option_parser.rb +156 -0
  56. data/lib/gli/option_parser_factory.rb +81 -0
  57. data/lib/gli/option_parsing_result.rb +21 -0
  58. data/lib/gli/options.rb +23 -0
  59. data/lib/gli/switch.rb +35 -0
  60. data/lib/gli/terminal.rb +101 -0
  61. data/lib/gli/version.rb +5 -0
  62. data/test/apps/README.md +2 -0
  63. data/test/apps/todo/Gemfile +2 -0
  64. data/test/apps/todo/README.rdoc +6 -0
  65. data/test/apps/todo/Rakefile +23 -0
  66. data/test/apps/todo/bin/todo +63 -0
  67. data/test/apps/todo/lib/todo/commands/create.rb +24 -0
  68. data/test/apps/todo/lib/todo/commands/list.rb +63 -0
  69. data/test/apps/todo/lib/todo/commands/ls.rb +47 -0
  70. data/test/apps/todo/lib/todo/commands/make.rb +52 -0
  71. data/test/apps/todo/lib/todo/version.rb +3 -0
  72. data/test/apps/todo/test/tc_nothing.rb +14 -0
  73. data/test/apps/todo/todo.gemspec +23 -0
  74. data/test/apps/todo/todo.rdoc +5 -0
  75. data/test/apps/todo_legacy/Gemfile +2 -0
  76. data/test/apps/todo_legacy/README.rdoc +6 -0
  77. data/test/apps/todo_legacy/Rakefile +23 -0
  78. data/test/apps/todo_legacy/bin/todo +61 -0
  79. data/test/apps/todo_legacy/lib/todo/commands/create.rb +24 -0
  80. data/test/apps/todo_legacy/lib/todo/commands/list.rb +63 -0
  81. data/test/apps/todo_legacy/lib/todo/commands/ls.rb +47 -0
  82. data/test/apps/todo_legacy/lib/todo/version.rb +3 -0
  83. data/test/apps/todo_legacy/test/tc_nothing.rb +14 -0
  84. data/test/apps/todo_legacy/todo.gemspec +23 -0
  85. data/test/apps/todo_legacy/todo.rdoc +5 -0
  86. data/test/apps/todo_plugins/commands/third.rb +1 -0
  87. data/test/config.yaml +10 -0
  88. data/test/fake_std_out.rb +30 -0
  89. data/test/init_simplecov.rb +8 -0
  90. data/test/option_test_helper.rb +13 -0
  91. data/test/tc_command.rb +508 -0
  92. data/test/tc_compound_command.rb +22 -0
  93. data/test/tc_doc.rb +325 -0
  94. data/test/tc_flag.rb +62 -0
  95. data/test/tc_gli.rb +773 -0
  96. data/test/tc_help.rb +387 -0
  97. data/test/tc_options.rb +43 -0
  98. data/test/tc_subcommand_parsing.rb +104 -0
  99. data/test/tc_subcommands.rb +260 -0
  100. data/test/tc_switch.rb +55 -0
  101. data/test/tc_terminal.rb +100 -0
  102. data/test/tc_verbatim_wrapper.rb +36 -0
  103. data/test/test_helper.rb +20 -0
  104. metadata +330 -0
@@ -0,0 +1,47 @@
1
+ # This is copied so I can have two slightly different versions of the same thing
2
+ desc "LS things, such as tasks or contexts"
3
+ long_desc %(
4
+ List a whole lot of things that you might be keeping track of
5
+ in your overall todo list.
6
+
7
+ This is your go-to place or finding all of the things that you
8
+ might have
9
+ stored in
10
+ your todo databases.
11
+ )
12
+ command [:ls] do |c|
13
+ c.desc "Show long form"
14
+ c.switch [:l,:long]
15
+
16
+ c.desc "List tasks"
17
+ c.long_desc %(
18
+ Lists all of your tasks that you have, in varying orders, and
19
+ all that stuff. Yes, this is long, but I need a long description.
20
+ )
21
+ c.command :tasks do |tasks|
22
+ tasks.desc "blah blah crud x whatever"
23
+ tasks.flag [:x]
24
+ tasks.action do |global,options,args|
25
+ puts "ls tasks: #{args.join(',')}"
26
+ end
27
+ end
28
+
29
+ c.desc "List contexts"
30
+ c.long_desc %(
31
+ Lists all of your contexts, which are places you might be
32
+ where you can do stuff and all that.
33
+ )
34
+ c.command :contexts do |contexts|
35
+
36
+ contexts.desc "Foobar"
37
+ contexts.switch [:f,'foobar']
38
+
39
+ contexts.desc "Blah"
40
+ contexts.switch [:b]
41
+
42
+ contexts.action do |global,options,args|
43
+ puts "ls contexts: #{args.join(',')}"
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,3 @@
1
+ module Todo
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+
3
+ class TC_testNothing < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ def test_the_truth
12
+ assert true
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # Ensure we require the local version and not one we might have installed already
2
+ require File.join([File.dirname(__FILE__),'lib','todo','version.rb'])
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'todo'
5
+ s.version = Todo::VERSION
6
+ s.author = 'Your Name Here'
7
+ s.email = 'your@email.address.com'
8
+ s.homepage = 'http://your.website.com'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'A description of your project'
11
+ # Add your other files here if you make them
12
+ s.files = %w(
13
+ bin/todo
14
+ )
15
+ s.require_paths << 'lib'
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README.rdoc','todo.rdoc']
18
+ s.rdoc_options << '--title' << 'todo' << '--main' << 'README.rdoc' << '-ri'
19
+ s.bindir = 'bin'
20
+ s.executables << 'todo'
21
+ s.add_development_dependency('rake')
22
+ s.add_development_dependency('rdoc')
23
+ end
@@ -0,0 +1,5 @@
1
+ = todo
2
+
3
+ Generate this with
4
+ todo rdoc
5
+ After you have described your command line interface
@@ -0,0 +1 @@
1
+ command :third do |c| c.action { |g,o,a| puts "third: #{a.join(',')}" } end
@@ -0,0 +1,10 @@
1
+ ---
2
+ commands:
3
+ :command:
4
+ :g: bar
5
+ :s: true
6
+ :other_command:
7
+ :g: foobar
8
+ :f: barfoo
9
+ :f: foo
10
+ :bleorgh: true
@@ -0,0 +1,30 @@
1
+ class FakeStdOut
2
+ attr_reader :strings
3
+
4
+ def initialize
5
+ @strings = []
6
+ end
7
+
8
+ def puts(string=nil)
9
+ @strings << string unless string.nil?
10
+ end
11
+
12
+ def write(x)
13
+ puts(x)
14
+ end
15
+
16
+ def printf(*args)
17
+ puts(Kernel.printf(*args))
18
+ end
19
+
20
+ # Returns true if the regexp matches anything in the output
21
+ def contained?(regexp)
22
+ strings.find{ |x| x =~ regexp }
23
+ end
24
+
25
+ def flush; end
26
+
27
+ def to_s
28
+ @strings.join("\n")
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/test"
5
+ end
6
+ rescue LoadError
7
+ # Don't care
8
+ end
@@ -0,0 +1,13 @@
1
+ module OptionTestHelper
2
+ def name_should_be(name)
3
+ lambda {
4
+ assert_equal(name,@cli_option.name)
5
+ }
6
+ end
7
+
8
+ def aliases_should_be(aliases)
9
+ lambda {
10
+ assert_equal(aliases,@cli_option.aliases)
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,508 @@
1
+ require 'test_helper'
2
+ require 'tempfile'
3
+
4
+ class TC_testCommand < Clean::Test::TestCase
5
+ include TestHelper
6
+ def setup
7
+ @fake_stdout = FakeStdOut.new
8
+ @fake_stderr = FakeStdOut.new
9
+ @original_stdout = $stdout
10
+ $stdout = @fake_stdout
11
+ @original_stderr = $stderr
12
+ $stderr = @fake_stderr
13
+ ENV.delete('GLI_DEBUG')
14
+ create_app
15
+ end
16
+
17
+ def teardown
18
+ $stdout = @original_stdout
19
+ $stderr = @original_stderr
20
+ FileUtils.rm_f "cruddo.rdoc"
21
+ end
22
+
23
+ def test_names
24
+ command = GLI::Command.new(:names => [:ls,:list,:'list-them-all'],:description => "List")
25
+ assert_equal "ls, list, list-them-all",command.names
26
+ end
27
+
28
+ def test_command_sort
29
+ commands = [GLI::Command.new(:names => :foo)]
30
+ commands << GLI::Command.new(:names => :bar)
31
+ commands << GLI::Command.new(:names => :zazz)
32
+ commands << GLI::Command.new(:names => :zaz)
33
+
34
+ sorted = commands.sort
35
+ assert_equal :bar,sorted[0].name
36
+ assert_equal :foo,sorted[1].name
37
+ assert_equal :zaz,sorted[2].name
38
+ assert_equal :zazz,sorted[3].name
39
+ end
40
+
41
+ def test_basic_command
42
+ [false,true].each do |openstruct|
43
+ end
44
+ [true].each do |openstruct|
45
+ create_app(openstruct)
46
+ openstruct_message = openstruct ? ", with use_openstruct" : ""
47
+ args_args = [%w(-g basic -v -c foo bar baz quux), %w(-g basic -v --configure=foo bar baz quux)]
48
+ args_args.each do |args|
49
+ args_orig = args.clone
50
+ @app.run(args)
51
+ assert_equal('true',@glob,"For args #{args_orig}#{openstruct_message}")
52
+ assert_equal('true',@glob_long_form,"For args #{args_orig}#{openstruct_message}")
53
+ assert_equal('true',@verbose,"For args #{args_orig}#{openstruct_message}")
54
+ assert_equal('false',@glob_verbose,"For args #{args_orig}#{openstruct_message}")
55
+ assert_equal('foo',@configure,"For args #{args_orig}#{openstruct_message}")
56
+ assert_equal(%w(bar baz quux),@args,"For args #{args_orig}#{openstruct_message}")
57
+ assert(@pre_called,"Pre block should have been called for args #{args_orig}#{openstruct_message}")
58
+ assert(@post_called,"Post block should have been called for args #{args_orig}#{openstruct_message}")
59
+ assert(!@error_called,"Error block should not have been called for args #{args_orig}#{openstruct_message}")
60
+ end
61
+ end
62
+ end
63
+
64
+ def test_around_filter
65
+ @around_block_called = false
66
+ @app.around do |global_options, command, options, arguments, code|
67
+ @around_block_called = true
68
+ code.call
69
+ end
70
+ @app.run(['bs'])
71
+ assert(@around_block_called, "Wrapper block should have been called")
72
+ end
73
+
74
+ def test_around_filter_can_be_skipped
75
+ # Given
76
+ @around_block_called = false
77
+ @action_called = false
78
+ @app.skips_around
79
+ @app.command :skips_around_filter do |c|
80
+ c.action do |g,o,a|
81
+ @action_called = true
82
+ end
83
+ end
84
+
85
+ @app.command :uses_around_filter do |c|
86
+ c.action do |g,o,a|
87
+ @action_called = true
88
+ end
89
+ end
90
+
91
+ @app.around do |global_options, command, options, arguments, code|
92
+ @around_block_called = true
93
+ code.call
94
+ end
95
+
96
+ # When
97
+ exit_status = @app.run(['skips_around_filter'])
98
+
99
+ # Then
100
+ assert_equal 0,exit_status
101
+ assert(!@around_block_called, "Wrapper block should have been skipped")
102
+ assert(@action_called,"Action should have been called")
103
+
104
+ # When
105
+ @around_block_called = false
106
+ @action_called = false
107
+ exit_status = @app.run(['uses_around_filter'])
108
+
109
+ # Then
110
+ assert_equal 0, exit_status
111
+ assert(@around_block_called, "Wrapper block should have been called")
112
+ assert(@action_called,"Action should have been called")
113
+ end
114
+
115
+ def test_around_filter_can_be_nested
116
+ @calls = []
117
+
118
+ @app.around do |global_options, command, options, arguments, code|
119
+ @calls << "first_pre"
120
+ code.call
121
+ @calls << "first_post"
122
+ end
123
+
124
+ @app.around do |global_options, command, options, arguments, code|
125
+ @calls << "second_pre"
126
+ code.call
127
+ @calls << "second_post"
128
+ end
129
+
130
+ @app.around do |global_options, command, options, arguments, code|
131
+ @calls << "third_pre"
132
+ code.call
133
+ @calls << "third_post"
134
+ end
135
+
136
+ @app.command :with_multiple_around_filters do |c|
137
+ c.action do |g,o,a|
138
+ @calls << "action!"
139
+ end
140
+ end
141
+
142
+ @app.run(['with_multiple_around_filters'])
143
+
144
+ assert_equal ["first_pre", "second_pre", "third_pre", "action!", "third_post", "second_post", "first_post"], @calls
145
+ end
146
+
147
+ def test_skipping_nested_around_filters
148
+ @calls = []
149
+
150
+ @app.around do |global_options, command, options, arguments, code|
151
+ begin
152
+ @calls << "first_pre"
153
+ code.call
154
+ ensure
155
+ @calls << "first_post"
156
+ end
157
+ end
158
+
159
+ @app.around do |global_options, command, options, arguments, code|
160
+ @calls << "second_pre"
161
+ code.call
162
+ @calls << "second_post"
163
+ end
164
+
165
+ @app.around do |global_options, command, options, arguments, code|
166
+ @calls << "third_pre"
167
+ code.call
168
+ exit_now!
169
+ @calls << "third_post"
170
+ end
171
+
172
+ @app.command :with_multiple_around_filters do |c|
173
+ c.action do |g,o,a|
174
+ @calls << "action!"
175
+ end
176
+ end
177
+
178
+ @app.run(['with_multiple_around_filters'])
179
+
180
+ assert_equal ["first_pre", "second_pre", "third_pre", "action!", "first_post"], @calls
181
+ end
182
+
183
+ def test_around_filter_handles_exit_now
184
+ @around_block_called = false
185
+ @error_message = "OH NOES"
186
+ @app.around do |global_options, command, options, arguments, code|
187
+ @app.exit_now! @error_message
188
+ code.call
189
+ end
190
+ exit_code = @app.run(['bs'])
191
+ assert exit_code != 0
192
+ assert_contained(@fake_stderr,/#{@error_message}/)
193
+ assert_not_contained(@fake_stdout,/SYNOPSIS/)
194
+ end
195
+
196
+ def test_around_filter_handles_help_now
197
+ @around_block_called = false
198
+ @error_message = "OH NOES"
199
+ @app.around do |global_options, command, options, arguments, code|
200
+ @app.help_now! @error_message
201
+ code.call
202
+ end
203
+ exit_code = @app.run(['bs'])
204
+ assert exit_code != 0
205
+ assert_contained(@fake_stderr,/#{@error_message}/)
206
+ assert_contained(@fake_stdout,/SYNOPSIS/)
207
+ end
208
+
209
+ def test_error_handler_prints_that_its_skipping_when_gli_debug_is_set
210
+ ENV["GLI_DEBUG"] = 'true'
211
+ @app.on_error do
212
+ false
213
+ end
214
+ @app.command :blah do |c|
215
+ c.action do |*|
216
+ raise 'wtf'
217
+ end
218
+ end
219
+
220
+ assert_raises(RuntimeError) {
221
+ @app.run(['blah'])
222
+ }
223
+ assert_contained(@fake_stderr,/Custom error handler exited false, skipping normal error handling/)
224
+ end
225
+
226
+ def test_error_handler_should_be_called_on_help_now
227
+ @app.command :blah do |c|
228
+ c.action do |*|
229
+ help_now!
230
+ end
231
+ end
232
+ @app.run(["blah"])
233
+ assert @error_called
234
+ end
235
+
236
+ def test_error_handler_shouldnt_be_called_on_help_from_command_line
237
+ @app.command :blah do |c|
238
+ c.action do |*|
239
+ end
240
+ end
241
+ [
242
+ ["--help", "blah"],
243
+ ["blah", "--help"],
244
+ ].each do |args|
245
+ args_copy = args.clone
246
+ @app.run(args)
247
+ assert !@error_called, "for args #{args_copy.inspect}"
248
+ end
249
+ end
250
+
251
+ def test_command_skips_pre
252
+ @app.skips_pre
253
+ @app.skips_post
254
+
255
+ skips_pre_called = false
256
+ runs_pre_called = false
257
+
258
+ @app.command [:skipspre] do |c|
259
+ c.action do |g,o,a|
260
+ skips_pre_called = true
261
+ end
262
+ end
263
+
264
+ # Making sure skips_pre doesn't leak to other commands
265
+ @app.command [:runspre] do |c|
266
+ c.action do |g,o,a|
267
+ runs_pre_called = true
268
+ end
269
+ end
270
+
271
+ @app.run(['skipspre'])
272
+
273
+ assert(skips_pre_called,"'skipspre' should have been called")
274
+ assert(!@pre_called,"Pre block should not have been called")
275
+ assert(!@post_called,"Post block should not have been called")
276
+ assert(!@error_called,"Error block should not have been called")
277
+
278
+ @app.run(['runspre'])
279
+
280
+ assert(runs_pre_called,"'runspre' should have been called")
281
+ assert(@pre_called,"Pre block should not have been called")
282
+ assert(@post_called,"Post block SHOULD have been called")
283
+ assert(!@error_called,"Error block should not have been called")
284
+ end
285
+
286
+ def test_command_no_globals
287
+ args = %w(basic -c foo bar baz quux)
288
+ @app.run(args)
289
+ assert_equal('foo',@configure)
290
+ assert_equal(%w(bar baz quux),@args)
291
+ end
292
+
293
+ def test_defaults_get_set
294
+ args = %w(basic bar baz quux)
295
+ @app.run(args)
296
+ assert_equal('false',@glob)
297
+ assert_equal('false',@verbose)
298
+ assert_equal('crud',@configure)
299
+ assert_equal(%w(bar baz quux),@args)
300
+ end
301
+
302
+ def test_negatable_gets_created
303
+ @app.command [:foo] do |c|
304
+ c.action do |g,o,a|
305
+ assert !g[:blah]
306
+ end
307
+ end
308
+ exit_status = @app.run(%w(--no-blah foo))
309
+ assert_equal 0,exit_status
310
+ end
311
+
312
+ def test_arguments_are_not_frozen
313
+ @args = []
314
+
315
+
316
+ @app.command [:foo] do |c|
317
+ c.action do |g,o,a|
318
+ @args = a
319
+ end
320
+ end
321
+ exit_status = @app.run(%w(foo a b c d e).map { |arg| arg.freeze })
322
+ assert_equal 0,exit_status
323
+ assert_equal 5,@args.length,"Action block was not called"
324
+
325
+ @args.each_with_index do |arg,index|
326
+ assert !arg.frozen?,"Expected argument at index #{index} to not be frozen"
327
+ end
328
+ end
329
+
330
+ def test_no_arguments
331
+ args = %w(basic -v)
332
+ @app.run(args)
333
+ assert_equal('true',@verbose)
334
+ assert_equal('crud',@configure)
335
+ assert_equal([],@args)
336
+ end
337
+
338
+ def test_unknown_command
339
+ args = %w(blah)
340
+ @app.run(args)
341
+ assert(!@post_called)
342
+ assert(@error_called)
343
+ assert_contained(@fake_stderr,/Unknown command 'blah'/)
344
+ end
345
+
346
+ def test_unknown_global_option
347
+ args = %w(--quux basic)
348
+ @app.run(args)
349
+ assert(!@post_called)
350
+ assert(@error_called,"Expected error callback to be called")
351
+ assert_contained(@fake_stderr,/Unknown option --quux/)
352
+ end
353
+
354
+ def test_unknown_argument
355
+ args = %w(basic --quux)
356
+ @app.run(args)
357
+ assert(!@post_called)
358
+ assert(@error_called)
359
+ assert_contained(@fake_stderr,/ Unknown option --quux/)
360
+ end
361
+
362
+ def test_forgot_action_block
363
+ @app.reset
364
+ @app.command :foo do
365
+ end
366
+
367
+ ENV['GLI_DEBUG'] = 'true'
368
+ assert_raises RuntimeError do
369
+ @app.run(['foo'])
370
+ end
371
+ assert_match /Command 'foo' has no action block/,@fake_stderr.to_s
372
+ end
373
+
374
+ def test_command_create
375
+ @app.desc 'single symbol'
376
+ @app.command :single do |c|; end
377
+ command = @app.commands[:single]
378
+ assert_equal :single, command.name
379
+ assert_equal nil, command.aliases
380
+
381
+ description = 'implicit array'
382
+ @app.desc description
383
+ @app.command :foo, :bar do |c|; end
384
+ command = @app.commands[:foo]
385
+ assert_equal :foo, command.name
386
+ assert_equal [:bar], command.aliases
387
+
388
+ description = 'explicit array'
389
+ @app.desc description
390
+ @app.command [:baz, :blah] do |c|; end
391
+ command = @app.commands[:baz]
392
+ assert_equal :baz, command.name
393
+ assert_equal [:blah], command.aliases
394
+ end
395
+
396
+ def test_pre_exiting_false_causes_nonzero_exit
397
+ @app.pre { |*| false }
398
+
399
+ assert_equal 65,@app.run(["bs"]) # BSD for "input data incorrect in some way"
400
+ assert_equal 'error: preconditions failed',@fake_stderr.to_s
401
+ assert_equal '',@fake_stdout.to_s
402
+ end
403
+
404
+ def test_name_for_help_with_top_command
405
+ @app.subcommand_option_handling :normal
406
+ @app.command :remote do |c|; end
407
+ command = @app.commands[:remote]
408
+ assert_equal ["remote"], command.name_for_help
409
+ end
410
+
411
+ def test_name_for_help_with_sub_command
412
+ @app.subcommand_option_handling :normal
413
+ @app.command :remote do |c|
414
+ c.command :add do |s|; end
415
+ end
416
+ sub_command = @app.commands[:remote].commands[:add]
417
+ assert_equal ["remote", "add"], sub_command.name_for_help
418
+ end
419
+
420
+
421
+ def test_name_for_help_with_sub_sub_command
422
+ @app.subcommand_option_handling :normal
423
+ @app.command :remote do |c|
424
+ c.command :add do |s|
425
+ s.command :sub do |ss|; end
426
+ end
427
+ end
428
+ sub_command = @app.commands[:remote].commands[:add].commands[:sub]
429
+ assert_equal ["remote", "add", "sub"], sub_command.name_for_help
430
+ end
431
+
432
+ private
433
+
434
+ def assert_contained(output,regexp)
435
+ assert_not_nil output.contained?(regexp),
436
+ "Expected output to contain #{regexp.inspect}, output was:\n#{output}"
437
+ end
438
+
439
+ def assert_not_contained(output,regexp)
440
+ assert_nil output.contained?(regexp),
441
+ "Didn't expected output to contain #{regexp.inspect}, output was:\n#{output}"
442
+ end
443
+
444
+ def create_app(use_openstruct=false)
445
+ @app = CLIApp.new
446
+ @app.error_device=@fake_stderr
447
+ @app.reset
448
+
449
+ if use_openstruct
450
+ @app.use_openstruct true
451
+ end
452
+
453
+ @app.program_desc 'A super awesome program'
454
+ @app.desc 'Some Global Option'
455
+ @app.switch [:g,:global]
456
+ @app.switch :blah
457
+ @app.long_desc 'This is a very long description for a flag'
458
+ @app.flag [:y,:yes]
459
+ @pre_called = false
460
+ @post_called = false
461
+ @error_called = false
462
+ @app.pre { |g,c,o,a| @pre_called = true }
463
+ @app.post { |g,c,o,a| @post_called = true }
464
+ @app.on_error { |g,c,o,a| @error_called = true }
465
+ @glob = nil
466
+ @verbose = nil
467
+ @glob_verbose = nil
468
+ @configure = nil
469
+ @args = nil
470
+ @app.desc 'Some Basic Command that potentially has a really really really really really really really long description and stuff, but you know, who cares?'
471
+ @app.long_desc 'This is the long description: "Some Basic Command that potentially has a really really really really really really really long description and stuff, but you know, who cares?"'
472
+ @app.arg_name 'first_file second_file'
473
+ @app.command [:basic,:bs] do |c|
474
+ c.desc 'be verbose'
475
+ c.switch :v
476
+ c.desc 'configure something or other, in some way that requires a lot of verbose text and whatnot'
477
+ c.default_value 'crud'
478
+ c.flag [:c,:configure]
479
+ c.action do |global_options,options,arguments|
480
+ if use_openstruct
481
+ @glob = global_options.g ? 'true' : 'false'
482
+ @glob_long_form = global_options.global ? 'true' : 'false'
483
+ @verbose = options.v ? 'true' : 'false'
484
+ @glob_verbose = global_options.v ? 'true' : 'false'
485
+ @configure = options.c
486
+ else
487
+ @glob = global_options[:g] ? 'true' : 'false'
488
+ @glob_long_form = global_options[:global] ? 'true' : 'false'
489
+ @verbose = options[:v] ? 'true' : 'false'
490
+ @glob_verbose = global_options[:v] ? 'true' : 'false'
491
+ @configure = options[:c]
492
+ end
493
+ @args = arguments
494
+ end
495
+ end
496
+ @app.desc "Testing long help wrapping"
497
+ @app.long_desc <<-EOS
498
+ This will create a scaffold command line project that uses @app
499
+ for command line processing. Specifically, this will create
500
+ an executable ready to go, as well as a lib and test directory, all
501
+ inside the directory named for your project
502
+ EOS
503
+ @app.command [:test_wrap] do |c|
504
+ c.action {}
505
+ end
506
+ end
507
+
508
+ end