From 514ed606ce958cb426bbcccfc7b5ff6027f60bcc Mon Sep 17 00:00:00 2001 From: "Josef M. Gallmetzer" <64498081+galjos@users.noreply.github.com> Date: Mon, 24 Aug 2026 09:18:53 +0200 Subject: [PATCH] fix: accept --n-molecules flag in add_molecules The -n and --n-molecules aliases were passed to add_argument as a single comma-joined string, so argparse registered one malformed option. The help text advertised --n-molecules, but using it failed with unrecognized arguments and only -n worked via prefix matching. Splitting the aliases into separate option strings makes both work. --- PQAnalysis/cli/add_molecules.py | 3 +- tests/cli/test_add_molecules.py | 76 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/cli/test_add_molecules.py diff --git a/PQAnalysis/cli/add_molecules.py b/PQAnalysis/cli/add_molecules.py index 8ba267b0..e4f4732b 100644 --- a/PQAnalysis/cli/add_molecules.py +++ b/PQAnalysis/cli/add_molecules.py @@ -121,7 +121,8 @@ def add_arguments(cls, parser: _ArgumentParser) -> None: ) parser.add_argument( - "-n, --n-molecules", + "-n", + "--n-molecules", dest='n_molecules', type=int, default=1, diff --git a/tests/cli/test_add_molecules.py b/tests/cli/test_add_molecules.py new file mode 100644 index 00000000..b3921615 --- /dev/null +++ b/tests/cli/test_add_molecules.py @@ -0,0 +1,76 @@ +""" +Tests for the add_molecules CLI argument parser. +""" + +import pytest + +import PQAnalysis.cli._argument_parser as argument_parser + +from PQAnalysis.cli.add_molecules import AddMoleculesCLI + + + +class TestAddMoleculesParser: + + """ + Tests for the add_molecules argument parser. + """ + + @pytest.fixture + def parser(self, monkeypatch): + """ + An add_molecules parser with quiet parse_args side effects. + """ + monkeypatch.setattr(argument_parser, "print_header", lambda: None) + + parser = argument_parser._ArgumentParser(prog="add_molecules") + AddMoleculesCLI.add_arguments(parser) + + return parser + + def _parse(self, parser, arguments): + root_logger = argument_parser.logging.getLogger() + original_level = root_logger.level + + try: + return parser.parse_args( + ["md-01.rst", "mol.xyz", "--log-file", "off"] + arguments + ) + finally: + root_logger.setLevel(original_level) + + def test_n_molecules_option_strings(self, parser): + """ + The n_molecules argument registers both aliases separately. + """ + option_strings = [ + action.option_strings + for action in parser._actions + if action.dest == "n_molecules" + ] + + assert option_strings == [["-n", "--n-molecules"]] + + def test_n_molecules_long_flag(self, parser): + """ + The advertised --n-molecules long flag is accepted. + """ + args = self._parse(parser, ["--n-molecules", "2"]) + + assert args.n_molecules == 2 + + def test_n_molecules_short_flag(self, parser): + """ + The -n short flag is accepted. + """ + args = self._parse(parser, ["-n", "3"]) + + assert args.n_molecules == 3 + + def test_n_molecules_default(self, parser): + """ + The n_molecules argument defaults to 1. + """ + args = self._parse(parser, []) + + assert args.n_molecules == 1