clamp 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -205,7 +205,7 @@ Clamp generates an anonymous subclass of the current class, to represent the sub
205
205
 
206
206
  ### Default subcommand
207
207
 
208
- You can mark a subcommand as "default" by using `default_subcommand` to declare it, rather than `subcommand`. Usually the SUBCOMMAND paramater is mandatory, but if a default subcommand is declared, it becomes optional.
208
+ You can mark a subcommand as "default" by using `default_subcommand` to declare it, rather than `subcommand`. Usually the SUBCOMMAND parameter is mandatory, but if a default subcommand is declared, it becomes optional.
209
209
 
210
210
  class MainCommand < Clamp::Command
211
211
 
@@ -240,7 +240,7 @@ All Clamp commands support a "`--help`" option, which outputs brief usage docume
240
240
  Options:
241
241
  --loud say it loud
242
242
  -n, --iterations N say it N times (default: 1)
243
- --help print help
243
+ -h, --help print help
244
244
 
245
245
  Contributing to Clamp
246
246
  ---------------------
@@ -19,12 +19,12 @@ module Clamp
19
19
  end
20
20
  @description.strip!
21
21
  end
22
-
22
+
23
23
  attr_reader :description
24
-
24
+
25
25
  def derived_usage_description
26
26
  parts = parameters.map { |a| a.name }
27
- parts.unshift("[OPTIONS]") if has_options?
27
+ parts.unshift("[OPTIONS]")
28
28
  parts.join(" ")
29
29
  end
30
30
 
@@ -55,11 +55,9 @@ module Clamp
55
55
  help.puts detail_format % subcommand.help
56
56
  end
57
57
  end
58
- if has_options?
59
- help.puts "\nOptions:"
60
- recognised_options.each do |option|
61
- help.puts detail_format % option.help
62
- end
58
+ help.puts "\nOptions:"
59
+ recognised_options.each do |option|
60
+ help.puts detail_format % option.help
63
61
  end
64
62
  help.string
65
63
  end
@@ -14,10 +14,6 @@ module Clamp
14
14
  define_accessors_for(option, &block)
15
15
  end
16
16
 
17
- def has_options?
18
- !documented_options.empty?
19
- end
20
-
21
17
  def find_option(switch)
22
18
  recognised_options.find { |o| o.handles?(switch) }
23
19
  end
@@ -26,8 +22,27 @@ module Clamp
26
22
  @declared_options ||= []
27
23
  end
28
24
 
29
- def documented_options
30
- ancestors.inject([]) do |options, ancestor|
25
+ def recognised_options
26
+ declare_implicit_options
27
+ effective_options
28
+ end
29
+
30
+ private
31
+
32
+ def declare_implicit_options
33
+ return nil if @implicit_options_declared
34
+ unless effective_options.find { |o| o.handles?("--help") }
35
+ help_switches = ["--help"]
36
+ help_switches.unshift("-h") unless effective_options.find { |o| o.handles?("-h") }
37
+ option help_switches, :flag, "print help" do
38
+ raise Clamp::HelpWanted.new(self)
39
+ end
40
+ end
41
+ @implicit_options_declared = true
42
+ end
43
+
44
+ def effective_options
45
+ ancestors.inject([]) do |options, ancestor|
31
46
  if ancestor.kind_of?(Clamp::Option::Declaration)
32
47
  options + ancestor.declared_options
33
48
  else
@@ -36,16 +51,6 @@ module Clamp
36
51
  end
37
52
  end
38
53
 
39
- def recognised_options
40
- documented_options + standard_options
41
- end
42
-
43
- HELP_OPTION = Clamp::Option.new("--help", :flag, "print help", :attribute_name => :help_requested)
44
-
45
- def standard_options
46
- [HELP_OPTION]
47
- end
48
-
49
54
  end
50
55
 
51
56
  end
@@ -1,3 +1,3 @@
1
1
  module Clamp
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.2.2".freeze
3
3
  end
@@ -16,7 +16,7 @@ describe Clamp::Command do
16
16
  describe "#help" do
17
17
 
18
18
  it "describes usage" do
19
- @command.help.should include("Usage:\n cmd\n")
19
+ @command.help.should =~ /^Usage:\n cmd.*\n/
20
20
  end
21
21
 
22
22
  end
@@ -54,7 +54,7 @@ describe Clamp::Command do
54
54
  end
55
55
 
56
56
  end
57
-
57
+
58
58
  describe "with explicit :attribute_name" do
59
59
 
60
60
  before do
@@ -87,7 +87,7 @@ describe Clamp::Command do
87
87
  it "sets the specified default value" do
88
88
  @command.port.should == 4321
89
89
  end
90
-
90
+
91
91
  end
92
92
 
93
93
  describe "with :default value" do
@@ -101,13 +101,13 @@ describe Clamp::Command do
101
101
  end
102
102
 
103
103
  describe "#help" do
104
-
104
+
105
105
  it "describes the default value" do
106
106
  @command.help.should include("port to listen on (default: 4321)")
107
107
  end
108
-
108
+
109
109
  end
110
-
110
+
111
111
  end
112
112
 
113
113
  describe "with a block" do
@@ -129,7 +129,7 @@ describe Clamp::Command do
129
129
  end
130
130
 
131
131
  end
132
-
132
+
133
133
  describe "with options declared" do
134
134
 
135
135
  before do
@@ -175,7 +175,7 @@ describe Clamp::Command do
175
175
  @command.flavour.should == "strawberry"
176
176
  @command.color.should == "blue"
177
177
  end
178
-
178
+
179
179
  end
180
180
 
181
181
  describe "with combined short options" do
@@ -188,9 +188,9 @@ describe Clamp::Command do
188
188
  @command.flavour.should == "strawberry"
189
189
  @command.nuts?.should == true
190
190
  end
191
-
191
+
192
192
  end
193
-
193
+
194
194
  describe "with option arguments attached using equals sign" do
195
195
 
196
196
  before do
@@ -201,9 +201,9 @@ describe Clamp::Command do
201
201
  @command.flavour.should == "strawberry"
202
202
  @command.color.should == "blue"
203
203
  end
204
-
204
+
205
205
  end
206
-
206
+
207
207
  describe "with option-like things beyond the arguments" do
208
208
 
209
209
  it "treats them as positional arguments" do
@@ -247,20 +247,40 @@ describe Clamp::Command do
247
247
 
248
248
  end
249
249
 
250
+ describe "with --help" do
251
+
252
+ it "requests help" do
253
+ lambda do
254
+ @command.parse(%w(--help))
255
+ end.should raise_error(Clamp::HelpWanted)
256
+ end
257
+
258
+ end
259
+
260
+ describe "with -h" do
261
+
262
+ it "requests help" do
263
+ lambda do
264
+ @command.parse(%w(-h))
265
+ end.should raise_error(Clamp::HelpWanted)
266
+ end
267
+
268
+ end
269
+
250
270
  describe "when option-writer raises an ArgumentError" do
251
-
271
+
252
272
  before do
253
273
  @command.class.class_eval do
254
-
274
+
255
275
  def color=(c)
256
276
  unless c == "black"
257
277
  raise ArgumentError, "sorry, we're out of #{c}"
258
278
  end
259
279
  end
260
-
280
+
261
281
  end
262
282
  end
263
-
283
+
264
284
  it "re-raises it as a UsageError" do
265
285
  lambda do
266
286
  @command.parse(%w(--color red))
@@ -286,6 +306,47 @@ describe Clamp::Command do
286
306
 
287
307
  end
288
308
 
309
+ describe "with an explicit --help option declared" do
310
+
311
+ before do
312
+ @command.class.option ["--help"], :flag, "help wanted"
313
+ end
314
+
315
+ it "does not generate implicit help option" do
316
+ lambda do
317
+ @command.parse(%w(--help))
318
+ end.should_not raise_error
319
+ @command.help.should be_true
320
+ end
321
+
322
+ it "does not recognise -h" do
323
+ lambda do
324
+ @command.parse(%w(-h))
325
+ end.should raise_error(Clamp::UsageError)
326
+ end
327
+
328
+ end
329
+
330
+ describe "with an explicit -h option declared" do
331
+
332
+ before do
333
+ @command.class.option ["-h", "--humidity"], "PERCENT", "relative humidity" do |n|
334
+ Integer(n)
335
+ end
336
+ end
337
+
338
+ it "does not map -h to help" do
339
+ @command.help.should_not =~ %r( -h[, ].*help)
340
+ end
341
+
342
+ it "still recognises --help" do
343
+ lambda do
344
+ @command.parse(%w(--help))
345
+ end.should raise_error(Clamp::HelpWanted)
346
+ end
347
+
348
+ end
349
+
289
350
  describe ".parameter" do
290
351
 
291
352
  it "declares option argument accessors" do
@@ -319,13 +380,13 @@ describe Clamp::Command do
319
380
  end
320
381
 
321
382
  describe "#help" do
322
-
383
+
323
384
  it "describes the default value" do
324
385
  @command.help.should include("direction (default: \"west\")")
325
386
  end
326
-
387
+
327
388
  end
328
-
389
+
329
390
  end
330
391
 
331
392
  describe "with a block" do
@@ -351,23 +412,23 @@ describe Clamp::Command do
351
412
  describe "with no parameters declared" do
352
413
 
353
414
  describe "#parse" do
354
-
415
+
355
416
  describe "with arguments" do
356
-
417
+
357
418
  it "raises a UsageError" do
358
419
  lambda do
359
420
  @command.parse(["crash"])
360
421
  end.should raise_error(Clamp::UsageError, "too many arguments")
361
422
  end
362
-
423
+
363
424
  end
364
-
425
+
365
426
  end
366
-
427
+
367
428
  end
368
429
 
369
430
  describe "with parameters declared" do
370
-
431
+
371
432
  before do
372
433
  @command.class.parameter "X", "x"
373
434
  @command.class.parameter "Y", "y"
@@ -375,9 +436,9 @@ describe Clamp::Command do
375
436
  end
376
437
 
377
438
  describe "#parse" do
378
-
439
+
379
440
  describe "with arguments for all parameters" do
380
-
441
+
381
442
  before do
382
443
  @command.parse(["crash", "bang", "wallop"])
383
444
  end
@@ -391,13 +452,13 @@ describe Clamp::Command do
391
452
  end
392
453
 
393
454
  describe "with insufficient arguments" do
394
-
455
+
395
456
  it "raises a UsageError" do
396
457
  lambda do
397
458
  @command.parse(["crash"])
398
459
  end.should raise_error(Clamp::UsageError, "parameter 'Y': no value provided")
399
460
  end
400
-
461
+
401
462
  end
402
463
 
403
464
  describe "with optional argument omitted" do
@@ -408,23 +469,23 @@ describe Clamp::Command do
408
469
  @command.y.should == "bang"
409
470
  @command.z.should == "ZZZ"
410
471
  end
411
-
472
+
412
473
  end
413
474
 
414
475
  describe "with too many arguments" do
415
-
476
+
416
477
  it "raises a UsageError" do
417
478
  lambda do
418
479
  @command.parse(["crash", "bang", "wallop", "kapow"])
419
480
  end.should raise_error(Clamp::UsageError, "too many arguments")
420
481
  end
421
-
482
+
422
483
  end
423
-
484
+
424
485
  end
425
-
486
+
426
487
  end
427
-
488
+
428
489
  describe "with explicit usage" do
429
490
 
430
491
  given_command("blah") do
@@ -469,11 +530,11 @@ describe Clamp::Command do
469
530
 
470
531
  self.description = <<-EOF
471
532
  Punt is an example command. It doesn't do much, really.
472
-
533
+
473
534
  The prefix at the beginning of this description should be normalised
474
535
  to two spaces.
475
536
  EOF
476
-
537
+
477
538
  end
478
539
 
479
540
  describe "#help" do
@@ -486,6 +547,7 @@ describe Clamp::Command do
486
547
  end
487
548
 
488
549
  end
550
+
489
551
  describe ".run" do
490
552
 
491
553
  it "creates a new Command instance and runs it" do
@@ -501,7 +563,7 @@ describe Clamp::Command do
501
563
  end
502
564
 
503
565
  describe "invoked with a context hash" do
504
-
566
+
505
567
  it "makes the context available within the command" do
506
568
  @command.class.class_eval do
507
569
  def execute
@@ -509,11 +571,11 @@ describe Clamp::Command do
509
571
  end
510
572
  end
511
573
  @command.class.run("xyz", [], :foo => "bar")
512
- stdout.should == "bar"
574
+ stdout.should == "bar"
513
575
  end
514
-
576
+
515
577
  end
516
-
578
+
517
579
  describe "when there's a UsageError" do
518
580
 
519
581
  before do
@@ -524,7 +586,7 @@ describe Clamp::Command do
524
586
  end
525
587
  end
526
588
 
527
- begin
589
+ begin
528
590
  @command.class.run("cmd", [])
529
591
  rescue SystemExit => e
530
592
  @system_exit = e
@@ -559,7 +621,7 @@ describe Clamp::Command do
559
621
  end
560
622
 
561
623
  describe "subclass" do
562
-
624
+
563
625
  before do
564
626
  @parent_command_class = Class.new(Clamp::Command) do
565
627
  option "--verbose", :flag, "be louder"
@@ -569,12 +631,12 @@ describe Clamp::Command do
569
631
  end
570
632
  @command = @derived_command_class.new("cmd")
571
633
  end
572
-
634
+
573
635
  it "inherits options from it's superclass" do
574
636
  @command.parse(["--verbose"])
575
637
  @command.should be_verbose
576
638
  end
577
639
 
578
640
  end
579
-
641
+
580
642
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: clamp
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Williams
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-27 00:00:00 Z
13
+ date: 2011-07-18 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: |
@@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- hash: -3288603544677427312
72
+ hash: 4567728835353449333
73
73
  segments:
74
74
  - 0
75
75
  version: "0"
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="
80
80
  - !ruby/object:Gem::Version
81
- hash: -3288603544677427312
81
+ hash: 4567728835353449333
82
82
  segments:
83
83
  - 0
84
84
  version: "0"