Skip to content

argparse: Strange behaviour of positional arguments with store_true, store_false, or store_const action #97848

Closed as not planned
@kkarbowiak

Description

@kkarbowiak

Bug report

When store_true, store_false, or store_const is set as an action of positional argument, the behaviour is counterintuitive. Running this small program:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('pos', action='store_true')
print(parser.parse_args([]))

outputs Namespace(pos=True). Changing last line to print(parser.parse_args(['foo'])) gives an error:

usage: prog.py [-h]
prog.py: error: unrecognized arguments: foo

Your environment

  • CPython versions tested on: 3.10.2
  • Operating system and architecture: Linux x86-64

Expected behaviour

I believe there is no good reason to allow setting those actions to positional arguments. Trying to do so should result in raising an exception (as is the case when trying to set required).

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions