Skip to content

Commit

Permalink
Fix for issue with multiple -- in command line
Browse files Browse the repository at this point in the history
Differential Revision: D56317273

Pull Request resolved: #888
  • Loading branch information
andywag committed Apr 18, 2024
1 parent b3d3d39 commit 5100b5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion torchx/cli/cmd_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def _parse_component_name_and_args(
component_args = args[1:]

# Error if there are repeated command line arguments
all_options = [x for x in component_args if x.startswith("-")]
all_options = [
x
for x in component_args
if x.startswith("-") and x.strip() != "-" and x.strip() != "--"
]
arg_count = Counter(all_options)
duplicates = [arg for arg, count in arg_count.items() if count > 1]
if len(duplicates) > 0:
Expand Down
26 changes: 26 additions & 0 deletions torchx/cli/test/cmd_run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ def test_parse_component_name_and_args_no_default(self) -> None:
_parse_component_name_and_args(["utils.echo", "--msg", "hello"], sp),
)

self.assertEqual(
("utils.echo", ["--msg", "hello", "--", "--"]),
_parse_component_name_and_args(
["utils.echo", "--msg", "hello", "--", "--"], sp
),
)

self.assertEqual(
("utils.echo", ["--msg", "hello", "-", "-"]),
_parse_component_name_and_args(
["utils.echo", "--msg", "hello", "-", "-"], sp
),
)

self.assertEqual(
("utils.echo", ["--msg", "hello", "- ", "- "]),
_parse_component_name_and_args(
["utils.echo", "--msg", "hello", "- ", "- "], sp
),
)

with self.assertRaises(SystemExit):
_parse_component_name_and_args(["--"], sp)

Expand All @@ -245,6 +266,11 @@ def test_parse_component_name_and_args_no_default(self) -> None:
with self.assertRaises(SystemExit):
_parse_component_name_and_args(["--msg", "hello", "--msg", "repeate"], sp)

with self.assertRaises(SystemExit):
_parse_component_name_and_args(
["--msg ", "hello", "--msg ", "repeate"], sp
)

def test_parse_component_name_and_args_with_default(self) -> None:
sp = argparse.ArgumentParser(prog="test")
dirs = [str(self.tmpdir)]
Expand Down

0 comments on commit 5100b5d

Please sign in to comment.