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

Click is expanding globs in a string and I don't want it to. #2499

Closed
jwebster-LANL opened this issue Apr 26, 2023 · 4 comments
Closed

Click is expanding globs in a string and I don't want it to. #2499

jwebster-LANL opened this issue Apr 26, 2023 · 4 comments

Comments

@jwebster-LANL
Copy link

jwebster-LANL commented Apr 26, 2023

I have a program that I would like to pass a string containing a path with globbed filenames in it...

>> myprogram "myfile*.txt"

This behaves differently on windows vs linux/osx

I want the files*.txt portion to come in as a string, but it appears that on Windows machines it's getting unglobbed before I can get to it...

For example, if I have files myfile1.txt, myfile2.txt, myfile3.txt, myfile4.txt

when I run >> myprogram "myfile*.txt" I get the error

Error: Got unexpected extra arguments (myfile2.txt myfile3.txt myfile4.txt)

it uses myfile1.txt as the argument, and the other 3 are considered extra

looking around I found this #1830 where you are expanding Windows globs before processing them so you can get the same expected behavior on Windows as on Linux/osx, That's great, but I specifically want to pass the string as is...
ie
myprogram "myfile*.txt" versus myprogram myfile*.txt (no quotation marks)

Is there a way to force click to read that in as a string and NOT glob it?

Environment:

  • Python version: 3.9
  • Click version: 8.1.3

Thanks

@ThiefMaster
Copy link
Member

This behaves differently on windows vs linux/osx

I believe that's working as intended because on Windows it's up to each program to expand globs, while on linux it's the shell taking care of it...

Related discussion: #1096

@davidism
Copy link
Member

davidism commented Apr 26, 2023

Duplicate of #1901, #2359, #2431, etc. You can disable automatic expansion with:

cli(windows_expand_args=False)

@jwebster-LANL
Copy link
Author

The problem is that you were initially trying to make it so that Windows would behave in the same way as linux/osx. But in linux/osx, an option with wildcards that is passed in quotation marks is automatically passed as a string, while one passed without quotation marks is globbed first.

The commands >> myprogram "myfile*.txt" (not globbed) versus >> myprogram myfile*.txt (globbed) do different things in linux. But if you pass windows_expand_args they will do the same thing - if true they both expand, if false neither expands. It's not the same as linux.

It seems to me that instead of having the programmer pass a windows_expand_args switch (which is not intuitive and will likely result in questions like mine above), it would be better to simply first check if the argument is surrounded by quotation marks, and if it is, simply pass it through as a string, and if it isn't, glob it as you currently do. That would give consistent behavior across operating systems, and result in less headaches for both the programmer and the people parsing these issues.

@davidism
Copy link
Member

davidism commented Apr 27, 2023

Thanks, but I don't plan to change this. It is not possible to check for quotations, they are not in sys.argv. Disable it and do your own processing if needed.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants