cli: accept --json and --quiet in either position
The global output flags were only registered on the top-level parser, so `chaoscrypt info -k my.key -P keypwd --json` failed with "unrecognized arguments: --json". That is the form most people type first, since the flag reads naturally at the end of a command. Both flags are now also registered on every subcommand. They write to separate destinations (json_local / quiet_local) because argparse would otherwise let an unset subcommand flag overwrite an explicitly set global one; the two are merged in main(). Three regression tests cover the flag in both positions plus the bare ciphertext output when neither flag is given.
This commit is contained in:
@@ -548,6 +548,40 @@ class TestCLI:
|
||||
assert r.returncode == 0, r.stderr
|
||||
assert open(dst, encoding="utf-8").read() == "file cli test"
|
||||
|
||||
def test_json_flag_accepted_before_and_after_subcommand(self, tmp_path):
|
||||
"""--json must work in either position, not only before the subcommand."""
|
||||
kf = str(tmp_path / "pos.key")
|
||||
self._run(["keygen", "-k", kf, "-P", KEY_PWD])
|
||||
|
||||
for args in (
|
||||
["--json", "info", "-k", kf, "-P", KEY_PWD],
|
||||
["info", "-k", kf, "-P", KEY_PWD, "--json"],
|
||||
):
|
||||
r = self._run(args)
|
||||
assert r.returncode == 0, r.stderr
|
||||
assert json.loads(r.stdout)["ok"] is True, args
|
||||
|
||||
def test_quiet_flag_accepted_before_and_after_subcommand(self, tmp_path):
|
||||
kf = str(tmp_path / "q.key")
|
||||
self._run(["keygen", "-k", kf, "-P", KEY_PWD])
|
||||
|
||||
for args in (
|
||||
["--quiet", "encrypt", "x", "-p", USER_PWD, "-k", kf, "-P", KEY_PWD],
|
||||
["encrypt", "x", "-p", USER_PWD, "-k", kf, "-P", KEY_PWD, "--quiet"],
|
||||
):
|
||||
r = self._run(args)
|
||||
assert r.returncode == 0, r.stderr
|
||||
assert r.stdout.strip() == "", args
|
||||
|
||||
def test_plain_output_is_bare_ciphertext(self, tmp_path):
|
||||
"""Without --json the ciphertext goes to stdout on its own."""
|
||||
kf = str(tmp_path / "p.key")
|
||||
self._run(["keygen", "-k", kf, "-P", KEY_PWD])
|
||||
r = self._run(["encrypt", "x", "-p", USER_PWD, "-k", kf, "-P", KEY_PWD])
|
||||
out = r.stdout.strip()
|
||||
assert out and "ciphertext" not in out
|
||||
int(out, 16)
|
||||
|
||||
def test_chpwd(self, tmp_path):
|
||||
kf = str(tmp_path / "c.key")
|
||||
self._run(["keygen", "-k", kf, "-P", KEY_PWD])
|
||||
|
||||
Reference in New Issue
Block a user