-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
Requirement
To run az synapse spark job submit, the user wants to pass --instance, trainer as values to --arguments.
Expected JSON:
"arguments": [
"--instance",
"trainer"
]Symptom
--arguments is defined as
| c.argument('command_line_arguments', options_list=['--arguments'], nargs='+', |
So the user tries to pass --instance, trainer separated by spaces, and the command fails
# Line breaks for legibility
az synapse spark job submit --name foo --workspace-name personalizer-trainer --spark-pool-name persbackend --main-definition-file foo --main-class-name org.personalizer.backend.PipelineManager --executors 4 --executor-size Medium
--arguments --instance trainer
argument --arguments: expected at least one argumentAs discussed in #16044 (comment), it is possible to use an equal sign = to concatenate the argument name and value for single-value argument:
az keyvault secret set -n mysecret --vault-name mykeyvault --value=-secret
This usage is not documented by
argparse: https://docs.python.org/3/library/argparse.html, https://docs.python.org/3/howto/argparse.html
However, it doesn't work for multi-value arguments:
# Line breaks for legibility
az synapse spark job submit --name foo --workspace-name personalizer-trainer --spark-pool-name persbackend --main-definition-file foo --main-class-name org.personalizer.backend.PipelineManager --executors 4 --executor-size Medium
--arguments=--instance trainer
unrecognized arguments: trainerWorkarounds
There are several workaround worth considering:
-
Instead of using a
nargs='+',--argumentscan take single value and split it withshlex.split -
Use
nargs=argparse.REMAINDERto make--argumentstake all remaining arguments.azdevuses this approach for--pytest-argsand perhaps this is the best way to go.However, this usage has been removed from
argparsedocument since Python 3.9 (https://bugs.python.org/issue17050):Since this feature is buggy, and there isn't an easy fix, we should probably remove any mention of it from the docs. We can still leave it as an undocumented legacy feature.
-
Replace
--argumentswith the--convention ofargparseto mark all remaining arguments as positional arguments (https://docs.python.org/3/library/argparse.html#arguments-containing) -
Add some special handling in
--argumentsso that dashes can be escaped, like__instance, or^--instance. But using characters like`,\may cause other trouble with shell's interpretation.
No matter which workaround we choose, it is not an easy fix.