Fixed run command to allow passing args to the called python script

This commit is contained in:
Rhet Turnbull 2022-05-13 05:50:16 -07:00
parent a89c66b3f7
commit b4dc7cfcf6

View File

@ -6,6 +6,25 @@ from runpy import run_module, run_path
import click
class RunCommand(click.Command):
"""Custom command that ignores unknown options so options can be passed to the run script"""
def make_parser(self, ctx):
"""Creates the underlying option parser for this command."""
parser = click.OptionParser(ctx)
parser.ignore_unknown_options = True
for param in self.get_params(ctx):
param.add_to_parser(parser, ctx)
return parser
def get_usage(self, ctx):
"""Returns the help for this command;
normally it would just return the usage string
but in order to pass --help on to the run script,
help for the run command is handled here"""
return self.get_help(ctx)
@click.command()
@click.argument("packages", nargs=-1, required=True)
@click.option(
@ -30,8 +49,15 @@ def uninstall(packages, yes):
run_module("pip", run_name="__main__")
@click.command(name="run")
@click.command(name="run", cls=RunCommand)
# help command passed just to keep click from intercepting help
# and allowing --help to be passed to the script being run
@click.option("--help", "-h", is_flag=True, help="Show this message and exit")
@click.argument("python_file", nargs=1, type=click.Path(exists=True))
def run(python_file):
"""Run a python file using same environment as osxphotos"""
@click.argument("args", metavar="ARGS", nargs=-1)
def run(python_file, help, args):
"""Run a python file using same environment as osxphotos.
Any args are made available to the python file."""
# drop first two arguments, which are the osxphotos script and run command
sys.argv = sys.argv[2:]
run_path(python_file, run_name="__main__")