Skip to content

Commit

Permalink
refactor: simplify version printing when parsing arguments (#284)
Browse files Browse the repository at this point in the history
* __main__: refactor arg parsing for version

* umu_test: add test for version flag
  • Loading branch information
R1kaB3rN authored Nov 24, 2024
1 parent f6a6af3 commit b093bc7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
17 changes: 3 additions & 14 deletions umu/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def parse_args() -> Namespace | tuple[str, list[str]]: # noqa: D103
opt_args: set[str] = {"--help", "-h", "--config"}
opt_args: set[str] = {"--help", "-h", "--config", "--version", "-v"}
parser: ArgumentParser = ArgumentParser(
description="Unified Linux Wine Game Launcher",
epilog=(
Expand All @@ -22,7 +22,8 @@ def parse_args() -> Namespace | tuple[str, list[str]]: # noqa: D103
parser.add_argument(
"-v",
"--version",
action="store_true",
action="version",
version=f"umu-launcher version {__version__} ({sys.version})",
help="show this version and exit",
)
parser.add_argument(
Expand All @@ -39,18 +40,6 @@ def parse_args() -> Namespace | tuple[str, list[str]]: # noqa: D103
parser.print_help(sys.stderr)
sys.exit(1)

# Show version
# Need to avoid clashes with later options (for example: wineboot -u)
# but parse_args scans the whole command line.
# So look at the first argument and see if we have -v or --version
# in sort of the same way parse_args would.
if sys.argv[1].lower().endswith(("--version", "-v")):
print(
f"umu-launcher version {__version__} ({sys.version})",
file=sys.stderr,
)
sys.exit(0)

# Winetricks
# Exit if no winetricks verbs were passed
if sys.argv[1].endswith("winetricks") and not sys.argv[2:]:
Expand Down
23 changes: 23 additions & 0 deletions umu/umu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,29 @@ def test_parse_args(self):
"Expected the test string when passed as an option",
)

def test_parse_args_version(self):
"""Test parse_args --version."""
mock_val = "foo"
opt = "version"
with patch.object(
__main__,
"parse_args",
return_value=argparse.Namespace(version=mock_val),
):
result = __main__.parse_args()
self.assertIsInstance(
result, Namespace, "Expected a Namespace from parse_arg"
)
self.assertTrue(
hasattr(result, opt),
f"Expected {result} to have attr {opt}",
)
self.assertEqual(
getattr(result, opt),
mock_val,
f"Expected {mock_val}, received {getattr(result, opt)}",
)

def test_parse_args_config(self):
"""Test parse_args --config."""
with patch.object(
Expand Down

0 comments on commit b093bc7

Please sign in to comment.