Skip to content

Commit 190f19a

Browse files
igneusrafaelfranca
authored andcommitted
long_desc option to disable wrapping
1 parent c3f77d5 commit 190f19a

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
@@ -65,15 +65,23 @@ def desc(usage, description, options = {})
6565

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

@@ -258,7 +266,11 @@ def command_help(shell, command_name)
258266

259267
if command.long_description
260268
shell.say "Description:"
261-
shell.print_wrapped(command.long_description, indent: 2)
269+
if command.wrap_long_description
270+
shell.print_wrapped(command.long_description, indent: 2)
271+
else
272+
shell.say command.long_description
273+
end
262274
else
263275
shell.say command.description
264276
end
@@ -535,14 +547,15 @@ def create_command(meth) #:nodoc:
535547
@usage ||= nil
536548
@desc ||= nil
537549
@long_desc ||= nil
550+
@long_desc_wrap ||= nil
538551
@hide ||= nil
539552

540553
if @usage && @desc
541554
base_class = @hide ? Thor::HiddenCommand : Thor::Command
542555
relations = {exclusive_option_names: method_exclusive_option_names,
543556
at_least_one_option_names: method_at_least_one_option_names}
544-
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options, relations)
545-
@usage, @desc, @long_desc, @method_options, @hide = nil
557+
commands[meth] = base_class.new(meth, @desc, @long_desc, @long_desc_wrap, @usage, method_options, relations)
558+
@usage, @desc, @long_desc, @long_desc_wrap, @method_options, @hide = nil
546559
@method_exclusive_option_names, @method_at_least_one_option_names = nil
547560
true
548561
elsif all_commands[meth] || meth == "method_missing"

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, :options_relation, :ancestor_name)
2+
class Command < Struct.new(:name, :description, :long_description, :wrap_long_description, :usage, :options, :options_relation, :ancestor_name)
33
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
44

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

99
def initialize_copy(other) #:nodoc:
@@ -136,7 +136,7 @@ def hidden?
136136
# A dynamic command that handles method missing scenarios.
137137
class DynamicCommand < Command
138138
def initialize(name, options = nil)
139-
super(name.to_s, "A dynamically-generated command", name.to_s, name.to_s, options)
139+
super(name.to_s, "A dynamically-generated command", name.to_s, nil, name.to_s, options)
140140
end
141141

142142
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
@@ -642,6 +642,21 @@ def shell
642642
HELP
643643
end
644644

645+
it "prints long description unwrapped if asked for" do
646+
expect(capture(:stdout) { MyScript.command_help(shell, "long_description_unwrapped") }).to eq(<<-HELP)
647+
Usage:
648+
thor my_script:long_description
649+
650+
Description:
651+
No added indentation, Inline
652+
whatespace not merged,
653+
Linebreaks preserved
654+
and
655+
indentation
656+
too
657+
HELP
658+
end
659+
645660
it "doesn't assign the long description to the next command without one" do
646661
expect(capture(:stdout) do
647662
MyScript.command_help(shell, "name_with_dashes")

0 commit comments

Comments
 (0)