This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argparse shows required arguments as optional
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: khalidmammadov, rhettinger
Priority: normal Keywords:

Created on 2021-09-16 16:26 by khalidmammadov, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg401969 - (view) Author: Khalid Mammadov (khalidmammadov) * Date: 2021-09-16 16:26
Currently argparse module shows all optional arguments under "optional arguments" section of the help.
It also includes those flags/arguments that are required as well. 
This add confusion to a user and does not properly show intention
msg401971 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-16 16:34
In Python3.10, "optional arguments" has been replaced with "options".

We didn't backport the change because it risks breaking tests that rely on exact string matches.  Also, it was really a bug, it was just an unfortunate choice of words.  "Optional arguments" meant to say that it was one of the -x or --fullword style arguments which are called options, so they really are "optional arguments" as opposed to "positional arguments".  Unfortunately, the most obvious reading of "optional arguments" is as the opposite of "required arguments" which is not what was meant.
msg401987 - (view) Author: Khalid Mammadov (khalidmammadov) * Date: 2021-09-16 20:15
May I suggest to change it again so we two sections: required and optional? This can help to be more clear from usage perspective. I have changed it locally and it looks like below:

usage: myprogram.py [-h] [--foo FOO] --bar BAR

required arguments:
  --bar BAR   foo help

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO   foo help
msg401989 - (view) Author: Khalid Mammadov (khalidmammadov) * Date: 2021-09-16 20:22
This is another, larger example, where I actually stumbled on this when looking into Apache Airflow project. Below makes it confusing to see what is actually required and what not. Unless you look for square brackets. Would be it much nicer to list required ones explicitly rather than looking into options and figuring out which one is a must?

usage: airflow users create [-h] -e EMAIL -f FIRSTNAME -l LASTNAME [-p PASSWORD] -r ROLE [--use-random-password] -u USERNAME

Create a user

optional arguments:
  -h, --help            show this help message and exit
  -e EMAIL, --email EMAIL
                        Email of the user
  -f FIRSTNAME, --firstname FIRSTNAME
                        First name of the user
  -l LASTNAME, --lastname LASTNAME
                        Last name of the user
  -p PASSWORD, --password PASSWORD
                        Password of the user, required to create a user without --use-random-password
  -r ROLE, --role ROLE  Role of the user. Existing roles include Admin, User, Op, Viewer, and Public
  --use-random-password
                        Do not prompt for password. Use random string instead. Required to create a user without --password 
  -u USERNAME, --username USERNAME
                        Username of the user

examples:
To create an user with "Admin" role and username equals to "admin", run:

    $ airflow users create \
          --username admin \
          --firstname FIRST_NAME \
          --lastname LAST_NAME \
          --role Admin \
          --email admin@example.org

airflow users create command error: the following arguments are required: -e/--email, -f/--firstname, -l/--lastname, -r/--role, -u/--username, see help above.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89387
2021-09-16 20:22:27khalidmammadovsetmessages: + msg401989
2021-09-16 20:15:46khalidmammadovsetmessages: + msg401987
2021-09-16 16:34:18rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg401971

resolution: out of date
stage: resolved
2021-09-16 16:26:49khalidmammadovcreate