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.edit() Support Editor Paths Containing Spaces #1026

Closed
heyglen opened this issue May 28, 2018 · 3 comments · Fixed by #1470 · May be fixed by #1477
Closed

click.edit() Support Editor Paths Containing Spaces #1026

heyglen opened this issue May 28, 2018 · 3 comments · Fixed by #1470 · May be fixed by #1477
Labels
bug windows Issues pertaining to the Windows environment
Milestone

Comments

@heyglen
Copy link

heyglen commented May 28, 2018

Environment

  • Python 3.6.5
  • click 6.7
  • Windows 7

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

            c = subprocess.Popen('%s "%s"' % (editor, filename),
                                 env=environ, shell=True)

After

            c = subprocess.Popen('"%s" "%s"' % (editor, filename),
                                 env=environ, shell=True)
@jcrotts jcrotts added the bug label Jun 1, 2018
@VictoryChang
Copy link

VictoryChang commented May 6, 2019

I do not encounter an issue on Mac as I can escape the space and open Sublime Text 3.
message = click.edit(
text='\n\n' + '',
editor='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl')

Releasing Issue, please feel free to take if you work specifically on Windows.

@davidism davidism added the windows Issues pertaining to the Windows environment label May 6, 2019
@davidism davidism added this to the 7.1 milestone Feb 24, 2020
@davidism
Copy link
Member

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. my command --option value arg would need to be "my command" --option value arg.

@heyglen
Copy link
Author

heyglen commented Apr 28, 2020

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}"')

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug windows Issues pertaining to the Windows environment
Projects
None yet
4 participants