clamp 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/examples/speak CHANGED
@@ -6,6 +6,10 @@ require "clamp"
6
6
 
7
7
  class SpeakCommand < Clamp::Command
8
8
 
9
+ self.description = %{
10
+ Say something.
11
+ }
12
+
9
13
  option "--loud", :flag, "say it loud"
10
14
  option ["-n", "--iterations"], "N", "say it N times", :default => 1 do |s|
11
15
  Integer(s)
data/lib/clamp/command.rb CHANGED
@@ -115,13 +115,13 @@ module Clamp
115
115
  # @param [Array<String>] arguments command-line arguments
116
116
  # @param [Hash] context additional data the command may need
117
117
  #
118
- def run(invocation_path = $0, arguments = ARGV, context = {})
118
+ def run(invocation_path = File.basename($0), arguments = ARGV, context = {})
119
119
  begin
120
120
  new(invocation_path, context).run(arguments)
121
121
  rescue Clamp::UsageError => e
122
122
  $stderr.puts "ERROR: #{e.message}"
123
123
  $stderr.puts ""
124
- $stderr.puts "See: '#{invocation_path} --help'"
124
+ $stderr.puts "See: '#{e.command.invocation_path} --help'"
125
125
  exit(1)
126
126
  rescue Clamp::HelpWanted => e
127
127
  puts e.command.help
data/lib/clamp/help.rb CHANGED
@@ -11,6 +11,17 @@ module Clamp
11
11
 
12
12
  attr_reader :declared_usage_descriptions
13
13
 
14
+ def description=(description)
15
+ @description = description.dup
16
+ if @description =~ /^\A\n*( +)/
17
+ indent = $1
18
+ @description.gsub!(/^#{indent}/, '')
19
+ end
20
+ @description.strip!
21
+ end
22
+
23
+ attr_reader :description
24
+
14
25
  def derived_usage_description
15
26
  parts = parameters.map { |a| a.name }
16
27
  parts.unshift("[OPTIONS]") if has_options?
@@ -27,6 +38,10 @@ module Clamp
27
38
  usage_descriptions.each_with_index do |usage, i|
28
39
  help.puts " #{invocation_path} #{usage}".rstrip
29
40
  end
41
+ if description
42
+ help.puts ""
43
+ help.puts description.gsub(/^/, " ")
44
+ end
30
45
  detail_format = " %-29s %s"
31
46
  if has_parameters?
32
47
  help.puts "\nParameters:"
@@ -0,0 +1,23 @@
1
+ module Clamp
2
+
3
+ class Subcommand < Struct.new(:name, :description, :subcommand_class)
4
+
5
+ def initialize(names, description, subcommand_class)
6
+ @names = Array(names)
7
+ @description = description
8
+ @subcommand_class = subcommand_class
9
+ end
10
+
11
+ attr_reader :names, :description, :subcommand_class
12
+
13
+ def is_called?(name)
14
+ names.member?(name)
15
+ end
16
+
17
+ def help
18
+ [names.join(", "), description]
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -1,10 +1,7 @@
1
- module Clamp
2
-
3
- class Subcommand < Struct.new(:name, :description, :subcommand_class)
1
+ require 'clamp/subcommand'
4
2
 
5
- def help
6
- [name, description]
7
- end
3
+ module Clamp
4
+ class Subcommand
8
5
 
9
6
  module Declaration
10
7
 
@@ -26,7 +23,7 @@ module Clamp
26
23
  end
27
24
 
28
25
  def find_subcommand(name)
29
- recognised_subcommands.find { |sc| sc.name == name }
26
+ recognised_subcommands.find { |sc| sc.is_called?(name) }
30
27
  end
31
28
 
32
29
  def has_subcommands!
@@ -40,5 +37,4 @@ module Clamp
40
37
  end
41
38
 
42
39
  end
43
-
44
40
  end
data/lib/clamp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clamp
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.1.3".freeze
3
3
  end
@@ -52,6 +52,43 @@ describe Clamp::Command do
52
52
 
53
53
  end
54
54
 
55
+ describe "with an aliased subcommand" do
56
+
57
+ given_command "blah" do
58
+
59
+ subcommand ["say", "talk"], "Say something" do
60
+
61
+ parameter "WORD ...", "stuff to say"
62
+
63
+ def execute
64
+ puts word_list
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ it "responds to both aliases" do
72
+
73
+ @command.run(["say", "boo"])
74
+ stdout.should =~ /boo/
75
+
76
+ @command.run(["talk", "jive"])
77
+ stdout.should =~ /jive/
78
+
79
+ end
80
+
81
+ describe "#help" do
82
+
83
+ it "lists all aliases" do
84
+ @help = @command.help
85
+ @help.should =~ /say, talk .* Say something/
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
55
92
  describe "with nested subcommands" do
56
93
 
57
94
  given_command "fubar" do
@@ -385,6 +385,29 @@ describe Clamp::Command do
385
385
 
386
386
  end
387
387
 
388
+ describe "with a description" do
389
+
390
+ given_command("punt") do
391
+
392
+ self.description = <<-EOF
393
+ Punt is an example command. It doesn't do much, really.
394
+
395
+ The prefix at the beginning of this description should be normalised
396
+ to two spaces.
397
+ EOF
398
+
399
+ end
400
+
401
+ describe "#help" do
402
+
403
+ it "includes the description" do
404
+ @command.help.should =~ /^ Punt is an example command/
405
+ @command.help.should =~ /^ The prefix/
406
+ end
407
+
408
+ end
409
+
410
+ end
388
411
  describe ".run" do
389
412
 
390
413
  it "creates a new Command instance and runs it" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clamp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Williams
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-22 00:00:00 +11:00
18
+ date: 2010-11-24 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -50,6 +50,7 @@ files:
50
50
  - lib/clamp/parameter.rb
51
51
  - lib/clamp/parameter/declaration.rb
52
52
  - lib/clamp/parameter/parsing.rb
53
+ - lib/clamp/subcommand.rb
53
54
  - lib/clamp/subcommand/declaration.rb
54
55
  - lib/clamp/subcommand/execution.rb
55
56
  - lib/clamp/version.rb