Here's the way I do it with argparse (with multiple args):

parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f','--foo', help='Description for foo argument', required=True)
parser.add_argument('-b','--bar', help='Description for bar argument', required=True)
args = vars(parser.parse_args())

args will be a dictionary containing the arguments:

if args['foo'] == 'Hello':
    # code here

if args['bar'] == 'World':
    # code here

In your case simply add only one argument.


Editor's note: The docs say this:

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

Use positional arguments instead, e.g. as shown in @mightypile's answer.

Answer from Diego Navarro on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › howto › argparse.html
Argparse Tutorial — Python 3.14.4 documentation
author, Tshepang Mbambo,. This tutorial is intended to be a gentle introduction to argparse, the recommended command-line parsing module in the Python standard library. Concepts: Let’s show the sor...
🌐
Real Python
realpython.com › command-line-interfaces-python-argparse
Build Command-Line Interfaces With Python's argparse – Real Python
December 14, 2024 - When building Python command-line interfaces (CLI), Python’s argparse module offers a comprehensive solution. You can use argparse to create user-friendly command-line interfaces that parse arguments and options directly from the command line. This tutorial guides you through organizing CLI projects, adding arguments and options, and customizing your CLI’s behavior with argparse.
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
Source code: Lib/argparse.py Tutorial: This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial. The arg...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-argparse-to-write-command-line-programs-in-python
How To Use argparse to Write Command-Line Programs in Python | DigitalOcean
March 11, 2021 - The argparse module is a powerful ... tutorial introduced you to the foundations of argparse: you wrote a command-line interface that accepted positional and optional arguments, and exposed help text to the user....
🌐
DataCamp
datacamp.com › tutorial › python-argparse
Master Python's argparse Module: Build Better CLIs | DataCamp
December 3, 2024 - In this tutorial, learn how to parse one or more arguments from the command-line or terminal using the getopt, sys, and argparse modules. ... Learn how you can execute a Python script from the command line, and also how you can provide command line arguments to your script.
🌐
ZetCode
zetcode.com › python › argparse
Python argparse - parsing command line arguments in Python with argparse module
September 24, 2024 - Python argparse tutorial shows how to parse arguments in Python with argparse module.
Top answer
1 of 16
481

Here's the way I do it with argparse (with multiple args):

parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f','--foo', help='Description for foo argument', required=True)
parser.add_argument('-b','--bar', help='Description for bar argument', required=True)
args = vars(parser.parse_args())

args will be a dictionary containing the arguments:

if args['foo'] == 'Hello':
    # code here

if args['bar'] == 'World':
    # code here

In your case simply add only one argument.


Editor's note: The docs say this:

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

Use positional arguments instead, e.g. as shown in @mightypile's answer.

2 of 16
319

My understanding of the question is two-fold. First, the simplest possible argparse example. Of course, to be dead-simple, it's got to be a toy example, i.e. all overhead with little power, but it might get you started.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("a")
args = parser.parse_args()

if args.a == 'magic.name':
    print('You nailed it!')

But this positional argument is now required. If you leave it out when invoking this program, you'll get an error about missing arguments. This leads me to the second part of the question. You seem to want a single optional argument without a named label (the --option labels). My suggestion would be to modify the code above as follows:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("a", nargs='?')
args = parser.parse_args()

if args.a is None:
    print('I can tell that no argument was given and I can deal with that here.')
elif args.a == 'magic.name':
    print('You nailed it!')
else:
    print(args.a)

There may well be a more elegant solution, but this works and is minimalist.

Note: If you want a different default value instead of None, use the default parameter to .add_argument.

Find elsewhere
🌐
Python Module of the Week
pymotw.com › 2 › argparse
argparse – Command line option and argument parsing. - Python Module of the Week
import argparse parser = argparse.ArgumentParser(description='Short sample app') parser.add_argument('-a', action="store_true", default=False) parser.add_argument('-b', action="store", dest="b") parser.add_argument('-c', action="store", dest="c", type=int) print parser.parse_args(['-a', '-bval', '-c', '3']) There are a few ways to pass values to single character options. The example above uses two different forms, -bval and -c val. $ python argparse_short.py Namespace(a=True, b='val', c=3)
🌐
Embl-community
grp-bio-it-workshops.embl-community.io › intermediate-python › 04-argparse › index.html
Parsing Command Line Arguments – Intermediate Python
July 24, 2020 - Duplicate argparse_example3.py and add two more arguments to the parser, saving the new version as argparse_example4.py. Name the new arguments number1 and number2 and add help messages to tell the user that these are the first and second number they should provide.
🌐
Mimo
mimo.org › glossary › python › argparse
Python argparse: Syntax, Usage, and Examples
Learn how to use Python argparse to build command-line tools with flags, defaults, types, subcommands, and input validation.
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to use argparse in python to build command-line interfaces
How to Use argparse in Python | Cherry Servers
February 10, 2026 - This tutorial will provide you with an overview of command-line arguments and give detailed instructions on how to use argparse in Python.
🌐
PythonForBeginners
pythonforbeginners.com › home › argparse tutorial
Argparse Tutorial - PythonForBeginners.com
August 25, 2020 - $ python program.py --help usage: program.py [-h] echo positional arguments: echo echo the string you use here optional arguments: -h, --help show this help message and exit · Note: Argparse treats the options we give as a string, but we can change that.
🌐
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 - Once you’ve set up argparse, the next step is to define the arguments your script will accept. argparse makes it easy to work with both required and optional inputs, specify argument types, and provide default values when necessary. Before diving deep, it’s important to understand that command-line arguments fall into two main categories: Positional arguments, which are commonly known as arguments. Optional arguments, which are also known as options, flags, or switches. For instance, in python organizer.py /path/to/directory –verbose, the –verbose flag is an optional argument.
🌐
The Python Code
thepythoncode.com › article › how-to-use-argparse-in-python
How to Use the Argparse Module in Python - The Python Code
Master the argparse module in Python with this comprehensive tutorial, covering command-line applications, argument parsing, real-world examples, integration with other libraries, and best practices to create user-friendly interfaces and powerful command-line tools.
🌐
Grimoire
grimoire.carcano.ch › the grimoire of a modern linux professional › blog › pillars › scripting › python argparse tutorial – argparse howto
Python Argparse Tutorial - Argparse HowTo
October 28, 2025 - The Python Argparse Tutorial - Argparse HowTo post provides a practical example of how to parse command line parameters and options using the argparse module
🌐
DEV Community
dev.to › usooldatascience › mastering-pythons-argparse-a-comprehensive-guide-for-beginners-48fn
Mastering Python’s argparse: A Comprehensive Guide for Beginners - DEV Community
September 15, 2024 - Introduction Python’s argparse module is a powerful tool for building user-friendly... Tagged with python, cli, input, argparse.
🌐
Medium
medium.com › @tushar_aggarwal › easy-argparse-a-guide-to-handling-command-line-arguments-9cdf62ff46db
Easy argparse: A guide to handling command-line arguments | by Tushar Aggarwal | Medium
July 3, 2023 - Argparse is an indispensable tool in the Python developer’s toolkit, allowing you to create user-friendly and powerful command-line interfaces for your applications. By following this comprehensive hands-on tutorial, you can now harness the power of argparse to create sophisticated CLIs and enhance your Python applications.
🌐
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.