Skip to content

Incorrect help for file_collision method without block #818

@shuuuuun

Description

@shuuuuun

Hello.

When using the file_collision method without block, displaying help will show commands (diff,merge) that should not be supported.

The reason for this behavior is that the branching is done by block_given? in the file_collision method, but not in file_collision_help.
diff,merge works only when block is passed.

options = block_given? ? "[Ynaqdhm]" : "[Ynaqh]"

when is?(:diff)
show_diff(destination, yield) if block_given?
say "Retrying..."
when is?(:merge)
if block_given? && !merge_tool.empty?
merge(destination, yield)
return nil
end

def file_collision_help #:nodoc:
<<-HELP
Y - yes, overwrite
n - no, do not overwrite
a - all, overwrite this and all others
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
m - merge, run merge tool
HELP
end

Shouldn't the behavior be branched in the file_collision_help method as well, so that unsupported commands (diff,merge) are not displayed?

I checked with thor v1.2.1 and current main branch.

A working example is below:

# thor.rb
require "thor"

class MyCLI < Thor
  desc "hello", "say hello"
  def hello
    file_path = "./foo.txt"
    if !File.exist?(file_path)
      File.write(file_path, "first write", mode: "w")
      return
    end

    content = "file_collision with no block"
    puts "=== #{content}"
    if file_collision(file_path)
      File.write(file_path, content, mode: "w")
    end

    content = "file_collision with block"
    puts "=== #{content}"
    if file_collision(file_path) { content }
      File.write(file_path, content, mode: "w")
    end
  end
end

MyCLI.start(ARGV)
$ ruby ./thor.rb hello
=== file_collision with no block
Overwrite ./foo.txt? (enter "h" for help) [Ynaqh] h
        Y - yes, overwrite
        n - no, do not overwrite
        a - all, overwrite this and all others
        q - quit, abort
        d - diff, show the differences between the old and the new
        h - help, show this help
        m - merge, run merge tool
Overwrite ./foo.txt? (enter "h" for help) [Ynaqh] y
=== file_collision with block
Overwrite ./foo.txt? (enter "h" for help) [Ynaqdhm] h
        Y - yes, overwrite
        n - no, do not overwrite
        a - all, overwrite this and all others
        q - quit, abort
        d - diff, show the differences between the old and the new
        h - help, show this help
        m - merge, run merge tool
Overwrite ./foo.txt? (enter "h" for help) [Ynaqdhm] d
- file_collision with no block
+ file_collision with block
Retrying...
Overwrite ./foo.txt? (enter "h" for help) [Ynaqdhm] y

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions