properly handle command-line arguments with spaces

This commit is contained in:
Izalia Mae 2022-05-12 12:31:46 -04:00
parent df415ebbe3
commit 2123571f3c
4 changed files with 24 additions and 13 deletions

View file

@ -1,5 +1,5 @@
__software__ = 'PyVenv'
__version__ = '0.1.4'
__version__ = '0.1.5'
from .env import PythonEnvironment

View file

@ -96,13 +96,11 @@ def cli_run(ctx, command):
if len(command) == 0:
return click.echo('Script or module name not provided')
cmd = ' '.join(command)
if Path(command[0]).is_file():
ctx.obj.run_script(cmd)
ctx.obj.run_script(command)
else:
ctx.obj.run_module(cmd)
ctx.obj.run_module(command)
@cli.command('import')

View file

@ -250,23 +250,36 @@ class PythonEnvironment(EnvBuilder):
def run_module(self, module, *args, **kwargs):
text = f'-m {module}'
if isinstance(module, (list, tuple)):
module = ['-m', *module]
for arg in args:
text += f' {arg}'
elif isinstance(module, str):
module = f'-m {module}'
return self.run_python(text)
else:
raise TypeError(f'command must be a list, tuple, or str, not {type(module).__name__}')
return self.run_python(module, *args, **kwargs)
def run_python(self, command, **kwargs):
if not self.executable.exists():
raise FileNotFoundError('Cannot find python executable. Was the environment created?')
return Process(f'{self.executable} {command}', **kwargs)
if isinstance(command, (list, tuple)):
command = [self.executable, *command]
elif isinstance(command, str):
command = f'{self.executable} {command}'
else:
raise TypeError(f'command must be a list, tuple, or str, not {type(command).__name__}')
return Process(command, **kwargs)
def run_script(self, script):
return self.run_python(script)
def run_script(self, script, *args, **kwargs):
return self.run_python(script, *args, **kwargs)
def save_config(self):

View file

@ -1,6 +1,6 @@
[metadata]
name = PyVenv
version = 0.1.4
version = 0.1.5
author = Zoey Mae
author_email = zoey@barkshark.xyz
url = https://git.barkshark.xyz/izaliamae/pyvenv