Skip to content

Commit c8e3375

Browse files
committed
long_desc option to disable wrapping
1 parent 34df888 commit c8e3375

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

lib/thor.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,23 @@ def desc(usage, description, options = {})
6666

6767
# Defines the long description of the next command.
6868
#
69+
# Long description is by default indented, line-wrapped and repeated whitespace merged.
70+
# In order to print long description verbatim, with indentation and spacing exactly
71+
# as found in the code, use the +wrap+ option
72+
#
73+
# long_desc 'your very long description', wrap: false
74+
#
6975
# ==== Parameters
7076
# long description<String>
77+
# options<Hash>
7178
#
7279
def long_desc(long_description, options = {})
7380
if options[:for]
7481
command = find_and_refresh_command(options[:for])
7582
command.long_description = long_description if long_description
7683
else
7784
@long_desc = long_description
85+
@long_desc_wrap = options[:wrap] || options[:wrap].nil?
7886
end
7987
end
8088

@@ -181,7 +189,11 @@ def command_help(shell, command_name)
181189
class_options_help(shell, nil => command.options.values)
182190
if command.long_description
183191
shell.say "Description:"
184-
shell.print_wrapped(command.long_description, :indent => 2)
192+
if command.wrap_long_description
193+
shell.print_wrapped(command.long_description, :indent => 2)
194+
else
195+
shell.say command.long_description
196+
end
185197
else
186198
shell.say command.description
187199
end
@@ -416,12 +428,13 @@ def create_command(meth) #:nodoc:
416428
@usage ||= nil
417429
@desc ||= nil
418430
@long_desc ||= nil
431+
@long_desc_wrap ||= nil
419432
@hide ||= nil
420433

421434
if @usage && @desc
422435
base_class = @hide ? Thor::HiddenCommand : Thor::Command
423-
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
424-
@usage, @desc, @long_desc, @method_options, @hide = nil
436+
commands[meth] = base_class.new(meth, @desc, @long_desc, @long_desc_wrap, @usage, method_options)
437+
@usage, @desc, @long_desc, @long_desc_wrap, @method_options, @hide = nil
425438
true
426439
elsif all_commands[meth] || meth == "method_missing"
427440
true

lib/thor/command.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Thor
2-
class Command < Struct.new(:name, :description, :long_description, :usage, :options, :ancestor_name)
2+
class Command < Struct.new(:name, :description, :long_description, :wrap_long_description, :usage, :options, :ancestor_name)
33
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
44

5-
def initialize(name, description, long_description, usage, options = nil)
6-
super(name.to_s, description, long_description, usage, options || {})
5+
def initialize(name, description, long_description, wrap_long_description, usage, options = nil)
6+
super(name.to_s, description, long_description, wrap_long_description, usage, options || {})
77
end
88

99
def initialize_copy(other) #:nodoc:
@@ -127,7 +127,7 @@ def hidden?
127127
# A dynamic command that handles method missing scenarios.
128128
class DynamicCommand < Command
129129
def initialize(name, options = nil)
130-
super(name.to_s, "A dynamically-generated command", name.to_s, name.to_s, options)
130+
super(name.to_s, "A dynamically-generated command", name.to_s, nil, name.to_s, options)
131131
end
132132

133133
def run(instance, args = [])

spec/command_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def command(options = {}, usage = "can_has")
66
options[key] = Thor::Option.parse(key, value)
77
end
88

9-
@command ||= Thor::Command.new(:can_has, "I can has cheezburger", "I can has cheezburger\nLots and lots of it", usage, options)
9+
@command ||= Thor::Command.new(:can_has, "I can has cheezburger", "I can has cheezburger\nLots and lots of it", nil, usage, options)
1010
end
1111

1212
describe "#formatted_usage" do
@@ -54,7 +54,7 @@ def command(options = {}, usage = "can_has")
5454

5555
describe "#dup" do
5656
it "dup options hash" do
57-
command = Thor::Command.new("can_has", nil, nil, nil, :foo => true, :bar => :required)
57+
command = Thor::Command.new("can_has", nil, nil, nil, nil, :foo => true, :bar => :required)
5858
command.dup.options.delete(:foo)
5959
expect(command.options[:foo]).to be
6060
end

spec/fixtures/script.thor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ END
9696
def name_with_dashes
9797
end
9898

99+
desc "long_description", "a" * 80
100+
long_desc <<-D, wrap: false
101+
No added indentation, Inline
102+
whatespace not merged,
103+
Linebreaks preserved
104+
and
105+
indentation
106+
too
107+
D
108+
def long_description_unwrapped
109+
end
110+
99111
method_options :all => :boolean
100112
method_option :lazy, :lazy_default => "yes"
101113
method_option :lazy_numeric, :type => :numeric, :lazy_default => 42

spec/thor_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,21 @@ def shell
602602
HELP
603603
end
604604

605+
it "prints long description unwrapped if asked for" do
606+
expect(capture(:stdout) { MyScript.command_help(shell, "long_description_unwrapped") }).to eq(<<-HELP)
607+
Usage:
608+
thor my_script:long_description
609+
610+
Description:
611+
No added indentation, Inline
612+
whatespace not merged,
613+
Linebreaks preserved
614+
and
615+
indentation
616+
too
617+
HELP
618+
end
619+
605620
it "doesn't assign the long description to the next command without one" do
606621
expect(capture(:stdout) do
607622
MyScript.command_help(shell, "name_with_dashes")

0 commit comments

Comments
 (0)