classification
Title: Positional arguments with boolean actions behave differently
Type: Stage: test needed
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: rjeffman, terry.reedy
Priority: normal Keywords:

Created on 2020-09-12 02:38 by rjeffman, last changed 2020-09-18 22:12 by terry.reedy.

Messages (2)
msg376764 - (view) Author: Rafael Guterres Jeffman (rjeffman) Date: 2020-09-12 02:38
argparse allow the use of `store_true` and `store_false` for positional arguments, and although it is weird, it should be fine, but using either action raises a behavior I believe is wrong.

Given the following Python code:

```
import argparse

arg = argparse.ArgumentParser()
arg.add_argument("opt", action="store_false")

arg.parse_args(["-h"])
```

The output is:

```
usage: t.py [-h]

positional arguments:
  opt

optional arguments:
  -h, --help  show this help message and exit
```

Note that the positional argument is not shown in the `usage` line.

When any string parameter is given, the result is:

```
usage: t.py [-h]
t.py: error: unrecognized arguments:
```

(add to the end of the output the value of the argument.)

Even if the use of a positional value is not the best way to describe boolean parameter (optional arguments provide a much better interface for such values), if argparse is to support positional boolean values they should work as other positional arguments.

I'd suggest raising an error if store_true or store_false is used along with positional values.
msg377141 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-18 22:12
You think the behavior is wrong.  Does it disagree with the doc?  If not, this is an design change (enhancement) issue limited to future version.  If so, it might be a bug that can be backported.

However, when I run the code on Windows with an argument
f:\dev\3x>py -3.8 f:/python/a/tem3.py a b
usage: tem3.py [-h]
...

I do not get an error, but get the same usage message as without. Adding quotes on the command line makes no difference.
History
Date User Action Args
2020-09-18 22:12:09terry.reedysetnosy: + terry.reedy
title: Positional arguments which use store boolean actions do not behave as other actions. -> Positional arguments with boolean actions behave differently
messages: + msg377141

stage: test needed
2020-09-12 02:38:08rjeffmancreate