diff --git a/osxphotos/cli/install_uninstall_run.py b/osxphotos/cli/install_uninstall_run.py index 478b1084..2fe17b37 100644 --- a/osxphotos/cli/install_uninstall_run.py +++ b/osxphotos/cli/install_uninstall_run.py @@ -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__")