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.edit() Support Editor Paths Containing Spaces #1026
Comments
I do not encounter an issue on Mac as I can escape the space and open Sublime Text 3. Releasing Issue, please feel free to take if you work specifically on Windows. |
Reverted in #1543 due to issues with quoting on Windows. See #1477 for the next fix. However, even with that fix, you're going to need to quote the command properly, just as you would have to on the command line, as it's otherwise impossible to tell if a string is a command with spaces or a command then arguments. |
What if we make a list of all the stings created by partitioning on the trailing space, testing if each is a file to find the command import pathlib
def split_command_argument(command_with_arguments):
command = command_with_arguments
arguments = ''
if not pathlib.Path(command).is_file():
# Iterate through the command prefixes split on the last space testing if each prefix is a valid file:
# "C:\Program Files\Sublime Text 3\subl.exe -h"
# "C:\Program Files\Sublime Text 3\subl.exe"
# "C:\Program Files\Sublime Text"
# "C:\Program Files\Sublime"
# "C:\Program"
for _ in range(command_with_arguments.count(' ')):
# Reverse the string to partion on the last space
reverse_command = command[::-1]
_, _, reverse_command = reverse_command.partition(' ')
command = reverse_command[::-1]
# Test if the command is a valid file
if pathlib.Path(command).is_file():
break
else:
raise FileNotFoundError(f'Cannot find editor executable in "{command_with_arguments}"')
# The arguments are whatever remains when the command is removed
arguments = command_with_arguments.replace(command, '',1 ).strip()
return command, arguments
command, arguments = split_command_argument('C:\\Program Files\\Sublime Text 3\\subl.exe -h')
print(f'"{command}" "{arguments}"') |
Environment
When using click.edit(), an editor with a space in the path throws an error. For example : "C:\Program Files\Sublime Text 3\sublime_text.exe"
If I put quotes around the path name in the subprocess.Popen call here the problem is fixed.
Before
After
The text was updated successfully, but these errors were encountered: