Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect help for file_collision method without block #818

Closed
shuuuuun opened this issue Mar 28, 2023 · 2 comments · Fixed by #858
Closed

Incorrect help for file_collision method without block #818

shuuuuun opened this issue Mar 28, 2023 · 2 comments · Fixed by #858

Comments

@shuuuuun
Copy link
Contributor

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.

@p8
Copy link
Member

p8 commented Aug 9, 2023

Hi @shuuuuun ,
Thanks for opening this issue.
I think you are correct.
Do you want to create a PR?

@shuuuuun
Copy link
Contributor Author

@p8 Thanks for your comment!
I have created the PR. #858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants