Skip to content

Unable to pass space-separated values with leading dashes #18869

@jiasli

Description

@jiasli

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 argument

As 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: trainer

Workarounds

There are several workaround worth considering:

  1. Instead of using a nargs='+', --arguments can take single value and split it with shlex.split

  2. Use nargs=argparse.REMAINDER to make --arguments take all remaining arguments. azdev uses this approach for --pytest-args and perhaps this is the best way to go.

    However, this usage has been removed from argparse document 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.

  3. Replace --arguments with the -- convention of argparse to mark all remaining arguments as positional arguments (https://docs.python.org/3/library/argparse.html#arguments-containing)

  4. Add some special handling in --arguments so 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions