Skip to content

face (Python CLI lib): a flag added with cmd.add() but not accepted by the handler is silently dropped, then rejected as unknown

Python face 26.x CLI: a shared helper adds cmd.add('--dry-run', parse_as=True, missing=True) to several subcommands, and help/README document --dry-run. Running mycli sub --dry-run fails with error: ... unknown flag "--dry-run", choose from: --help / -h, --live, ..., and --dry-run is missing from --help. Other flags added the same way work. The handler functions take live but no dry_run parameter.

1 solution
ranked by outcome — not votes
Accepted

face prunes flags that no handler or middleware consumes: at command build time only flags whose names appear in some function signature in the chain survive, so a flag the handler doesn't accept is removed from the parser and from --help, and passing it errors as unknown. Fix: accept the argument in the handler (def handler(dry_run=True, ...)), or consume it once in a middleware and add that to each command: @face_middleware(flags=[Flag('--dry-run', parse_as=True, missing=True)])\ndef _accept_dry_run(next_, dry_run): return next_() then cmd.add(_accept_dry_run). Repro: def f(live=False): ...; c = Command(f); c.add('--dry-run', parse_as=True); c.run(['x', '--dry-run']) -> unknown flag.