Skip to content

Commit

Permalink
Merge pull request #739 from igneus/long_desc_nowrap
Browse files Browse the repository at this point in the history
🌧️ long_desc: new option to disable wrapping
  • Loading branch information
rafaelfranca authored May 15, 2023
2 parents d8b073c + bd50da6 commit 9b9db75
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
19 changes: 16 additions & 3 deletions lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,23 @@ def desc(usage, description, options = {})

# Defines the long description of the next command.
#
# Long description is by default indented, line-wrapped and repeated whitespace merged.
# In order to print long description verbatim, with indentation and spacing exactly
# as found in the code, use the +wrap+ option
#
# long_desc 'your very long description', wrap: false
#
# ==== Parameters
# long description<String>
# options<Hash>
#
def long_desc(long_description, options = {})
if options[:for]
command = find_and_refresh_command(options[:for])
command.long_description = long_description if long_description
else
@long_desc = long_description
@long_desc_wrap = options[:wrap] != false
end
end

Expand Down Expand Up @@ -258,7 +266,11 @@ def command_help(shell, command_name)

if command.long_description
shell.say "Description:"
shell.print_wrapped(command.long_description, indent: 2)
if command.wrap_long_description
shell.print_wrapped(command.long_description, indent: 2)
else
shell.say command.long_description
end
else
shell.say command.description
end
Expand Down Expand Up @@ -535,14 +547,15 @@ def create_command(meth) #:nodoc:
@usage ||= nil
@desc ||= nil
@long_desc ||= nil
@long_desc_wrap ||= nil
@hide ||= nil

if @usage && @desc
base_class = @hide ? Thor::HiddenCommand : Thor::Command
relations = {exclusive_option_names: method_exclusive_option_names,
at_least_one_option_names: method_at_least_one_option_names}
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options, relations)
@usage, @desc, @long_desc, @method_options, @hide = nil
commands[meth] = base_class.new(meth, @desc, @long_desc, @long_desc_wrap, @usage, method_options, relations)
@usage, @desc, @long_desc, @long_desc_wrap, @method_options, @hide = nil
@method_exclusive_option_names, @method_at_least_one_option_names = nil
true
elsif all_commands[meth] || meth == "method_missing"
Expand Down
8 changes: 4 additions & 4 deletions lib/thor/command.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Thor
class Command < Struct.new(:name, :description, :long_description, :usage, :options, :options_relation, :ancestor_name)
class Command < Struct.new(:name, :description, :long_description, :wrap_long_description, :usage, :options, :options_relation, :ancestor_name)
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/

def initialize(name, description, long_description, usage, options = nil, options_relation = nil)
super(name.to_s, description, long_description, usage, options || {}, options_relation || {})
def initialize(name, description, long_description, wrap_long_description, usage, options = nil, options_relation = nil)
super(name.to_s, description, long_description, wrap_long_description, usage, options || {}, options_relation || {})
end

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

def run(instance, args = [])
Expand Down
4 changes: 2 additions & 2 deletions spec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def command(options = {}, usage = "can_has")
options[key] = Thor::Option.parse(key, value)
end

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

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

describe "#dup" do
it "dup options hash" do
command = Thor::Command.new("can_has", nil, nil, nil, foo: true, bar: :required)
command = Thor::Command.new("can_has", nil, nil, nil, nil, foo: true, bar: :required)
command.dup.options.delete(:foo)
expect(command.options[:foo]).to be
end
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/script.thor
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ END
def name_with_dashes
end

desc "long_description", "a" * 80
long_desc <<-D, wrap: false
No added indentation, Inline
whatespace not merged,
Linebreaks preserved
and
indentation
too
D
def long_description_unwrapped
end

method_options :all => :boolean
method_option :lazy, :lazy_default => "yes"
method_option :lazy_numeric, :type => :numeric, :lazy_default => 42
Expand Down
15 changes: 15 additions & 0 deletions spec/thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,21 @@ def shell
HELP
end

it "prints long description unwrapped if asked for" do
expect(capture(:stdout) { MyScript.command_help(shell, "long_description_unwrapped") }).to eq(<<-HELP)
Usage:
thor my_script:long_description
Description:
No added indentation, Inline
whatespace not merged,
Linebreaks preserved
and
indentation
too
HELP
end

it "doesn't assign the long description to the next command without one" do
expect(capture(:stdout) do
MyScript.command_help(shell, "name_with_dashes")
Expand Down

0 comments on commit 9b9db75

Please sign in to comment.