natcap / invest

InVEST®: models that map and value the goods and services from nature that sustain and fulfill human life.
Apache License 2.0
155 stars 66 forks source link

CLI: export python script that includes args from a datastack #1472

Open davemfish opened 8 months ago

davemfish commented 8 months ago

invest export-py does not currently accept a datastack arg, though the function that builds the python script can take an args dict. So this should be a trivial change to add a command-line arg to the parser.

I'm wanting this feature as it is the quickest way I can think of to run a model on a given datastack and drop into a python debugger on exceptions.

davemfish commented 8 months ago

Emily pointed out:

it's a little redundant to have to provide the model name as an argument, when it's also known from the datastack. I see why that's necessary using the export_py subparser though. Another option would be to make a separate command for converting a datastack to a script - but that would be extra work for a pretty minor issue.

Another option could be to use mutually exclusive args, so either the model name is passed, or a datastack JSON. https://docs.python.org/3/library/argparse.html#mutual-exclusion

Example invest export-py --model carbon (creates a python script with args dict and all empty string arg values) invest export-py --datastack path/to/carbon.json (creates python script with args dict and values from json) invest export-py --model carbon --datastack path/to/carbon.json (argparse raises error)

One downside here is that the model name arg is no longer a simple positional arg like it is with other subcommands (run, getspec). So that could be surprising to users.

We could also probably implement our own mutually exclusive rules that would do something like, Allowed: invest export-py carbon invest export-py -d path/to/carbon.json

Not allowed: invest export-py carbon -d path/to/carbon.json

davemfish commented 8 months ago

Oops, I think I referenced this issue by mistake in an unrelated PR that closed it