@@ -30,7 +30,7 @@ Quick Links for ArgumentParser
3030========================= =========================================================================================================== ==================================================================================
3131Name Description Values
3232========================= =========================================================================================================== ==================================================================================
33- prog _ The name of the program Defaults to `` os.path.basename(sys.argv[0]) ``
33+ prog _ The name of the program
3434usage _ The string describing the program usage
3535description _ A brief description of what the program does
3636epilog _ Additional description of the program after the argument help
@@ -214,8 +214,8 @@ ArgumentParser objects
214214 as keyword arguments. Each parameter has its own more detailed description
215215 below, but in short they are:
216216
217- * prog _ - The name of the program (default:
218- `` os.path.basename( sys.argv[0]) ``)
217+ * prog _ - The name of the program (default: generated from the `` __main__ ``
218+ module attributes and `` sys.argv[0] ``)
219219
220220 * usage _ - The string describing the program usage (default: generated from
221221 arguments added to parser)
@@ -268,10 +268,18 @@ The following sections describe how each of these are used.
268268prog
269269^^^^
270270
271- By default, :class: `ArgumentParser ` objects use the base name
272- (see :func: `os.path.basename `) of ``sys.argv[0] `` to determine
273- how to display the name of the program in help messages. This default is almost
274- always desirable because it will make the help messages match the name that was
271+ By default, :class: `ArgumentParser ` calculates the name of the program
272+ to display in help messages depending on the way the Python inerpreter was run:
273+
274+ * The :func: `base name <os.path.basename> ` of ``sys.argv[0] `` if a file was
275+ passed as argument.
276+ * The Python interpreter name followed by ``sys.argv[0] `` if a directory or
277+ a zipfile was passed as argument.
278+ * The Python interpreter name followed by ``-m `` followed by the
279+ module or package name if the :option: `-m ` option was used.
280+
281+ This default is almost
282+ always desirable because it will make the help messages match the string that was
275283used to invoke the program on the command line. For example, consider a file
276284named ``myprogram.py `` with the following code::
277285
@@ -281,7 +289,7 @@ named ``myprogram.py`` with the following code::
281289 args = parser.parse_args()
282290
283291The help for this program will display ``myprogram.py `` as the program name
284- (regardless of where the program was invoked from):
292+ (regardless of where the program was invoked from) if it is run as a script :
285293
286294.. code-block :: shell-session
287295
@@ -299,6 +307,17 @@ The help for this program will display ``myprogram.py`` as the program name
299307 -h, --help show this help message and exit
300308 --foo FOO foo help
301309
310+ If it is executed via the :option: `-m ` option, the help will display a corresponding command line:
311+
312+ .. code-block :: shell-session
313+
314+ $ /usr/bin/python3 -m subdir.myprogram --help
315+ usage: python3 -m subdir.myprogram [-h] [--foo FOO]
316+
317+ options:
318+ -h, --help show this help message and exit
319+ --foo FOO foo help
320+
302321 To change this default behavior, another value can be supplied using the
303322``prog= `` argument to :class: `ArgumentParser `::
304323
@@ -309,7 +328,8 @@ To change this default behavior, another value can be supplied using the
309328 options:
310329 -h, --help show this help message and exit
311330
312- Note that the program name, whether determined from ``sys.argv[0] `` or from the
331+ Note that the program name, whether determined from ``sys.argv[0] ``,
332+ from the ``__main__ `` module attributes or from the
313333``prog= `` argument, is available to help messages using the ``%(prog)s `` format
314334specifier.
315335
@@ -324,6 +344,9 @@ specifier.
324344 -h, --help show this help message and exit
325345 --foo FOO foo of the myprogram program
326346
347+ .. versionchanged :: 3.14
348+ The default ``prog `` value now reflects how ``__main__ `` was actually executed,
349+ rather than always being ``os.path.basename(sys.argv[0]) ``.
327350
328351usage
329352^^^^^
0 commit comments