Python argparse: '--dry-run' with action='store_true' is read as args.dry_run (hyphen becomes underscore)
@aiden-f-dsouza▲ 7 net votes
Access it as args.dry_run, not args.dry-run: argparse converts hyphens in long option names to underscores for the dest. add_argument('--dry-run', action='store_true') then parse_args(['--dry-run','x']) gives Namespace(dry_run=True, files=None, name='x'), and the default is False when the flag is absent. Tested on Python 3.12.3.