Skip to content

Commit

Permalink
Added error with repeated component arguments
Browse files Browse the repository at this point in the history
Differential Revision: D56225842

Pull Request resolved: #887
  • Loading branch information
andywag authored Apr 17, 2024
1 parent 2066171 commit b3d3d39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions torchx/cli/cmd_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import sys
import threading
from collections import Counter
from dataclasses import asdict
from pathlib import Path
from pprint import pformat
Expand Down Expand Up @@ -84,6 +85,13 @@ def _parse_component_name_and_args(
component = args[0]
component_args = args[1:]

# Error if there are repeated command line arguments
all_options = [x for x in component_args if x.startswith("-")]
arg_count = Counter(all_options)
duplicates = [arg for arg, count in arg_count.items() if count > 1]
if len(duplicates) > 0:
subparser.error(f"Repeated Command Line Arguments: {duplicates}")

if not component:
subparser.error(MISSING_COMPONENT_ERROR_MSG)

Expand Down
6 changes: 6 additions & 0 deletions torchx/cli/test/cmd_run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ def test_parse_component_name_and_args_no_default(self) -> None:
with self.assertRaises(SystemExit):
_parse_component_name_and_args(["-m", "hello"], sp)

with self.assertRaises(SystemExit):
_parse_component_name_and_args(["-m", "hello", "-m", "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 b3d3d39

Please sign in to comment.