Parameters starting with - or -- are usually considered optional. All other parameters are positional parameters and as such required by design (like positional function arguments). It is possible to require optional arguments, but this is a bit against their design. Since they are still part of the non-positional arguments, they will still be listed under the confusing header “optional arguments” even if they are required. The missing square brackets in the usage part however show that they are indeed required.

See also the documentation:

In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line.

Note: Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.

That being said, the headers “positional arguments” and “optional arguments” in the help are generated by two argument groups in which the arguments are automatically separated into. Now, you could “hack into it” and change the name of the optional ones, but a far more elegant solution would be to create another group for “required named arguments” (or whatever you want to call them):

parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-i', '--input', help='Input file name', required=True)
parser.parse_args(['-h'])
usage: [-h] [-o OUTPUT] -i INPUT

Foo

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file name

required named arguments:
  -i INPUT, --input INPUT
                        Input file name
Answer from poke on Stack Overflow
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line. To make an option required, True can be specified for the required= keyword argument to add_argument():
Top answer
1 of 7
504

Parameters starting with - or -- are usually considered optional. All other parameters are positional parameters and as such required by design (like positional function arguments). It is possible to require optional arguments, but this is a bit against their design. Since they are still part of the non-positional arguments, they will still be listed under the confusing header “optional arguments” even if they are required. The missing square brackets in the usage part however show that they are indeed required.

See also the documentation:

In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line.

Note: Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.

That being said, the headers “positional arguments” and “optional arguments” in the help are generated by two argument groups in which the arguments are automatically separated into. Now, you could “hack into it” and change the name of the optional ones, but a far more elegant solution would be to create another group for “required named arguments” (or whatever you want to call them):

parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-i', '--input', help='Input file name', required=True)
parser.parse_args(['-h'])
usage: [-h] [-o OUTPUT] -i INPUT

Foo

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file name

required named arguments:
  -i INPUT, --input INPUT
                        Input file name
2 of 7
138

Since I prefer to list required arguments before optional, I hack around it via:

parser = argparse.ArgumentParser()
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
required.add_argument('--required_arg', required=True)
optional.add_argument('--optional_arg')
return parser.parse_args()

and this outputs:

usage: main.py [-h] --required_arg REQUIRED_ARG [--optional_arg OPTIONAL_ARG]

required arguments:
  --required_arg REQUIRED_ARG

optional arguments:
  --optional_arg OPTIONAL_ARG

I can live without -h, --help showing up in the optional arguments group.

Discussions

python - Is it better practice to set default values for optional argparse arguments? - Software Engineering Stack Exchange
I mention python's argparse as an example but this would apply to any CLI argument parser library for any language. Just generally speaking, at a high level, if there are a lot of optional arguments for my application, should these arguments' values be set to at least something, even if its a falsy value? Here's what I'm talking about. ... argparse.command_a.add_argument("-a", required... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
How to set several required arguments into an optional group?
Hello: I’m trying to use the argparse module to provide the argument parsing. In my use case, I have eight positional arguments: arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: arg1 and arg2 are required. arg3 and arg4 are a couple and optional. But if users pass in one(arg3 or arg4), they ... More on discuss.python.org
🌐 discuss.python.org
0
0
May 27, 2022
ArgParse Optional Command Line Arguments?
You don't need the equals sign. Start date is gettting the value "=", and then the script is getting additional argument of the date you actually wanted to provide More on reddit.com
🌐 r/learnpython
5
3
March 28, 2017
Argparse complains argument required when default is provided
docsDocumentation in the Doc ... Library Python modules in the Lib/ directory ... Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state. ... assignee = None closed_at = None created_at = labels = ['3.8', 'type-bug', 'library'] title = 'Argparse complains argument required when default ... More on github.com
🌐 github.com
9
February 10, 2021
🌐
Educative
educative.io › answers › what-is-argparse-required-in-python
What is argparse required in Python?
The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. required is a parameter of the ArugmentParser object’s function add_argument(). By default, the arguments of type -f or --foo are optional and can be omitted.
🌐
Python documentation
docs.python.org › 3 › howto › argparse.html
Argparse Tutorial — Python 3.14.3 documentation
Calling our program now requires us to specify an option. The parse_args() method actually returns some data from the options specified, in this case, echo. The variable is some form of ‘magic’ that argparse performs for free (i.e. no need to specify which variable that value is stored in).
🌐
GitHub
gist.github.com › abalter › 605773b34a68bb370bf84007ee55a130
Python Aargparsing Examples · GitHub
usage: [-h] [-o OUTPUT] -i INPUT Foo optional arguments: -h, --help show this help message and exit -o OUTPUT, --output OUTPUT Output file name required named arguments: -i INPUT, --input INPUT Input file name ... If the correspond parameter does not exist, the value is opposite of store_true/store_false. If the parameter exists, the value is store_true/store_false. So we use use store_true in the most situation. return false if parameter doesn’t exist; return true if parameter exists. That’s what we want! def fun4(): import argparse parser = argparse.ArgumentParser() parser.add_argument('--p1', action='store_true') parser.add_argument('--p2', action='store_true') parser.add_argument('--p3', action='store_false') cmd = '--p1' args = parser.parse_args(cmd.split()) print(args) if args.p1: print('p1 good') else: print('p1 bad') print('\n========Go!==========\n') fun4()
Find elsewhere
🌐
Python.org
discuss.python.org › python help
How to set several required arguments into an optional group? - Python Help - Discussions on Python.org
May 27, 2022 - In my use case, I have eight positional arguments: arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: arg1 and arg2 are required. arg3 and arg4 are a couple and optional. But if users pass in one(arg3 or arg4), they ...
🌐
Stackify
stackify.com › python-argparse-definition-how-to-use-and-best-practices
Python argparse: Definition, How to Use, and Best Practices - Stackify
February 4, 2025 - The Python argparse module simplifies this by providing built-in error-handling mechanisms. However, you can further customize error messages and manage graceful exits to improve the user experience. When using argparse, errors can occur due to invalid input or incorrect argument usage. Let’s look at some common errors. If a required argument isn’t provided, argparse automatically displays an error message and exits.
🌐
DataCamp
datacamp.com › tutorial › python-argparse
Master Python's argparse Module: Build Better CLIs | DataCamp
December 3, 2024 - Optional arguments in argparse are typically not necessary because they are, by definition, optional. However, there may be instances where you want an argument beginning with -- to be mandatory. To accomplish this, set the required parameter to True when adding the argument.
🌐
Python
docs.python.org › 3 › library › index.html
The Python Standard Library — Python 3.14.3 documentation
argparse — Parser for command-line options, arguments and subcommands · optparse — Parser for command line options · getpass — Portable password input · fileinput — Iterate over lines from multiple input streams · curses — Terminal ...
🌐
Python Module of the Week
pymotw.com › 3 › argparse › index.html
argparse — Command-Line Option and Argument Parsing
December 30, 2016 - For positional required arguments, option_string is always None. $ python3 argparse_custom_action.py Initializing CustomAction dest = 'a' option_strings = ['-a'] required = False Initializing CustomAction dest = 'm' nargs = '*' option_strings = ['-m'] required = False Processing CustomAction for a parser = 4315836992 values = 'value' option_string = '-a' Processing CustomAction for m parser = 4315836992 values = ['multivalue', 'second'] option_string = '-m' Namespace(a='VALUE', m=['MULTIVALUE', 'SECOND'])
🌐
Typer
typer.tiangolo.com
Typer
Typer is a library for building CLI applications that users will love using and developers will love creating. Based on Python type hints.
🌐
Python
docs.python.org › 3 › library › collections.html
collections — Container datatypes
Since fields with a default value must come after any fields without a default, the defaults are applied to the rightmost parameters. For example, if the fieldnames are ['x', 'y', 'z'] and the defaults are (1, 2), then x will be a required argument, ...
🌐
Astral
docs.astral.sh › uv › guides › scripts
Running scripts | uv
December 9, 2025 - The Python version will download if it is not installed — see the documentation on Python versions for more details. A shebang can be added to make a script executable without using uv run — this makes it easy to run scripts that are on your PATH or in the current folder. For example, create a file called greet with the following contents ... #!/usr/bin/env -S uv run --script # # /// script # requires-python = ">=3.12" # dependencies = ["httpx"] # /// import httpx print(httpx.get("https://example.com"))
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-command-line-arguments-in-python
How to add command line arguments in Python?
In [3]: run <>.ipynb usage: ipython [-h] player ipython: error: the following arguments are required: player An exception has occurred, use %tb to see the full traceback. b) If we provide more than one argument, it complains again.The program complains about getting a second argument that has not been defined. c) Only when we give the program exactly one argument will it run · 2. Create a program which will accept only two arguments - tennis players as string and grand slamt titles won by the player as integer. import argparse def get_args(): """ Function : get_args parameters used in .add_argument 1.
🌐
Python documentation
docs.python.org › 3 › whatsnew › 3.14.html
What’s new in Python 3.14
February 23, 2026 - The default value of the program name for argparse.ArgumentParser now reflects the way the Python interpreter was instructed to find the __main__ module code.
🌐
Reddit
reddit.com › r/learnpython › argparse optional command line arguments?
r/learnpython on Reddit: ArgParse Optional Command Line Arguments?
March 28, 2017 -
import argparse    
import datetime

today = datetime.datetime.today()
file_date = today - datetime.timedelta(days = 2)

parser = argparse.ArgumentParser()
parser.add_argument("--start_date") 
args = parser.parse_args()

I want to run my python script with the option to add a start_date argument. If start date is not specified then the date will be file_date by default.

I'm a little confused how argparse is working here though. I can run my file "python test_argparse.py" fine. But when I enter:

python test_argparse.py --start_date = 01/05/2017

I get:

error: unrecognized arguments: 01/05/2017

Before I look at setting the default argument as file_date, how do I call the optional start_date argument in the command line? I'm sure I'm missing something simple here.

🌐
GitHub
github.com › python › cpython › issues › 87358
Argparse complains argument required when default is provided · Issue #87358 · python/cpython
February 10, 2021 - activity = <Date 2021-05-04.23:03:59.180> actor = 'rhettinger' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = <Date 2021-02-10.10:24:37.312> creator = 'Clint Olsen' dependencies = [] files = [] hgrepos = [] issue_num = 43192 keywords = [] message_count = 8.0 messages = ['386770', '386771', '386781', '386799', '386801', '386802', '392957', '392965'] nosy_count = 4.0 nosy_names = ['rhettinger', 'eric.smith', 'paul.j3', 'Clint Olsen'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue43192' versions = ['Python 3.8'] GH-87358: Add clarification about nargs and default argparse behaviour #124094
Author   clintolsen
🌐
GeeksforGeeks
geeksforgeeks.org › python › command-line-option-and-argument-parsing-using-argparse-in-python
Command-Line Option and Argument Parsing using argparse in Python - GeeksforGeeks
July 12, 2025 - Command line arguments are those values that are passed during the calling of the program along with the calling statement. Usually, python uses sys.argv array to deal with such arguments but here we describe how it can be made more resourceful and user-friendly by employing argparse module.