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

Allow disabling pattern expansion on Windows #1901

Closed
ofek opened this issue May 15, 2021 · 8 comments
Closed

Allow disabling pattern expansion on Windows #1901

ofek opened this issue May 15, 2021 · 8 comments
Milestone

Comments

@ofek
Copy link
Contributor

ofek commented May 15, 2021

TL;DR

Essentially, make this optional: #1830

Use case

We have a command that passes arbitrary arguments to be executed in other contexts such as Docker containers, servers over SSH, etc.

On Windows since Click 8.0 this breaks (depending on files in cwd, etc.) and unlike on Linux we can't just escape special characters

Implementation

Setting could be called disable_pattern_expansion, e.g.:

import click


@click.command(
    context_settings={'help_option_names': [], 'ignore_unknown_options': True, 'disable_pattern_expansion': True},
)
@click.argument('args', required=True, nargs=-1)
@click.pass_obj
def run(app, args):
    environment = app.get_environment()
    environment.send_command(args)
@ofek
Copy link
Contributor Author

ofek commented May 16, 2021

I looked into the code and it appears nested commands will have already received expanded arguments so disable_pattern_expansion will need to be of type t.Optional[t.Union[bool, t.List[str]]] and if nesting then defined on the group as a list of sub-command names to ignore.

@davidism
Copy link
Member

Can you provide an example of what goes wrong, so we can explain better in the changelog? Also, there was some discussion of escaping in the original issue, maybe it can help?

@ofek
Copy link
Contributor Author

ofek commented May 16, 2021

Basic example:

cli run external-tool *.log

now would run external-tool on the other system with expanded args if there are any log files in the current directory.

On Linux we can do:

cli run external-tool \*.log

but on Windows the Click args become ('external-tool', '\\*.log')

@davidism
Copy link
Member

Not ideal, I understand, but the original feature request discussed escaping #1096 (comment)

Python's glob module lets you use [] for quoting. (E.g., the pattern [*].txt matches the literal filename *.txt.) Not very intuitive, but it is possible, and I guess Windows users are used to weird edge cases when trying to use the cli to refer to files with * in the name?

@ofek
Copy link
Contributor Author

ofek commented May 16, 2021

Windows users are used to weird edge cases when trying to use the cli to refer to files with * in the name

Actually, us Windows users are accustomed to CLIs not supporting expansion by default 😉

Python's glob module lets you use [] for quoting

What about ~ and env vars?

I guess the point I'm trying to stress is that certain inputs can no longer be achieved in a cross-platform way

@davidism
Copy link
Member

Pinging @njsmith and @asweigart for their thoughts since they participated in the original discussion.

Due to the way the processing pipeline works, the expansion has to be done at the beginning, before any parsing of the command line is done. This would be an all or nothing toggle, either no expansion happens for any command, or it happens for every command.

@davidism davidism added this to the 8.0.1 milestone May 19, 2021
@ofek
Copy link
Contributor Author

ofek commented May 19, 2021

Thank you!!!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2021
@davidism
Copy link
Member

A follow up to this is #2195, which points out that we could assume any error in glob expansion on Windows could be treated the same as no glob expansion: keep that arg as-is. So in that case you might be able to leave expansion enabled for your app.

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

2 participants