Working with Command Line Arguments in Python

Command line arguments play a critical role in making Python scripts more interactive, flexible, and suitable for real-world applications. These arguments are a means by which users can influence the behavior of a script without modifying the underlying code. This capacity is especially valuable in scenarios involving automation, data processing, testing, and tool development. In Python, several modules facilitate the handling and parsing of command-line arguments, including sys, getopt, argparse, and docopt.

When a Python script is executed from the command line, additional inputs known as command-line arguments can be passed alongside. These inputs allow the script to operate on dynamic data, change its execution path, or adjust its configuration based on user specifications. Understanding how to effectively implement and use these arguments is an essential skill for any Python programmer.

In this part, we will cover the fundamentals of command line arguments in Python, explore the sys module in detail, and walk through multiple practical examples to illustrate the key concepts.

What Are Command Line Arguments?

Command line arguments are parameters supplied to a program when it is invoked. They appear after the script name in the command line interface (CLI) and are typically used to control the script’s functionality, pass input files, or define configuration values.

Basic Syntax

The general structure for passing arguments to a Python script from the CLI is as follows:

python script.py arg1 arg2 arg3

Here, script.py is the name of the script, and arg1, arg2, and arg3 are the arguments passed.

Each argument is treated as a string by default and can be accessed within the script for use in conditional logic, function parameters, file handling, and more.

Introduction to the sys Module

The sys module is a built-in Python library that provides access to variables and functions that interact directly with the Python runtime environment. Among its many features, the sys module enables the retrieval of command-line arguments using the argv list.

Understanding sys. argv

The sys. The argv list contains all the command-line arguments passed to the script. It always includes at least one element:

  • Sys.argv[0]: The name of the script. 
  • Sys. argv[1:] Remaining arguments passed to the script. 

This list can be iterated over or indexed directly to access individual arguments.

Example with the sys Module: import sys

 

print(“Script name:”, sys.argv[0])

print(“Number of arguments:”, len(sys.argv) – 1)

print(“Arguments:”, sys.argv[1:])

 

If this script is run using:

python script.py file1.txt file2.txt

The output will be:

Script name: script.py
Number of arguments: 2
Arguments: [‘file1.txt’, ‘file2.txt’]

Practical Applications of sys. argv

File Handling Example

import sys

 

if len(sys.argv) < 2:

    print(“Please provide a filename.”)

    sys.exit()

 

filename = sys.argv[1]

Try:

    with open(filename, ‘r’) as file:

        content = file.read()

        print(content)

Except FileNotFoundError:

    print(“File not found:”, filename)

 

This script accepts a filename as a command-line argument, reads the file, and prints its content.

Validating Command Line Arguments

Proper validation of command-line arguments is crucial to building robust scripts. Using the sys module alone requires manual validation, which involves checking the number and type of arguments provided.

Example: Argument Count Check

import sys

 

if len(sys.argv) != 3:

    print(“Usage: script.py arg1 arg2”)

    sys.exit()

 

arg1 = sys.argv[1]

arg2 = sys.argv[2]

print(“Arguments received:”, arg1, arg2)

 

This example ensures that exactly two arguments are passed. Otherwise, it displays usage information and exits.

Common Pitfalls When Using sys.argv

While using sys. argv is straightforward, there are common mistakes developers should be aware of:

  • Not validating the number of arguments, leading to index errors. 
  • Assuming argument types without conversion. 
  • Forgetting to handle exceptions when files or inputs are invalid. 

Type Conversion

Since all elements in sys. argv are strings, type conversion is necessary for numerical or boolean operations.

num = int(sys.argv[1])

print(“Square:”, num ** 2)

 

This snippet converts a command-line argument to an integer before performing arithmetic.

Using sys. argv with Loops and Conditions

Arguments can be processed in loops or used in conditional statements to create dynamic and responsive scripts.

Example: Batch Processing Multiple Files

import sys

 

for filename in sys.argv[1:]:

    try:

        with open(filename, ‘r’) as f:

            print(f”Contents of {filename}:”)

            print(f.read())

    Except FileNotFoundError:

        print(f”{filename} not found”)

 

This code loops through all file names passed as arguments and prints their contents, handling missing files gracefully.

When to Use the sys Module

The sys module is suitable for simple scripts where basic argument parsing is sufficient. It’s useful when:

  • You only need a few arguments. 
  • No flags or options are required. 
  • You want minimal overhead in code. 

For more complex scenarios, other modules such as getopt, argparse, or docopt may be more appropriate.

Introduction to the getopt Module in Python

As scripts become more complex, the need for structured command line argument parsing grows. While the sys module offers a simple way to access command line arguments, it cannot handle optional arguments, flags, or keyword-based options elegantly. This is where the getopt module becomes useful. The getopt module provides functionality similar to the C getopt() function, allowing developers to parse command line options and arguments in a predictable and user-friendly manner.

In this part, we will explore the capabilities of the getopt module, understand its syntax and behavior, and demonstrate practical examples to help you implement it effectively in your own Python scripts.

What is the getopt Module?

The getopt module is a standard Python library that provides a method for parsing command-line arguments. It allows scripts to accept both short-form and long-form options, similar to those used in many Unix command-line tools.

With getopt, you can specify options like -i (short form) and– input (long form), assign values to them, and manage their behavior in a structured and error-tolerant way.

Key Functions in getopt

The primary function provided by the getopt module is:

getopt.getopt(args, shortopts, longopts=[])

Parameters:

  • Args: A list of command line arguments, typically sys. argv[1:].
  • shortopts: A string containing the short option letters. If an option requires a value, it should be followed by a colon (:).
  • longopts: A list of long option names. If an option requires a value, it should be followed by an equal sign (=).

Returns:

A tuple (opts, args), where:Optss: A list of (option, value) pairs.

  • Args: The list of program arguments left after the option parsing.

Syntax and Usage

To use getopt effectively, follow these basic steps:

  1. Import the getopt and sys modules.
  2. Define the expected options.
  3. Parse the arguments using getopt.getopt().
  4. Handle each option accordingly using a loop.

Basic Example

import getopt

import sys

 

def main(argv):

    inputfile = ”

    outputfile = ”

    Try:

        opts, args = getopt.getopt(argv, “hi:o:”, [“ifile=”, “ofile=”])

    Except getopt.GetoptError:

        print(‘script.py -i <inputfile> -o <outputfile>’)

        sys.exit(2)

 

    For opt, arg in opts:

        if opt == ‘-h’:

            print(‘script.py -i <inputfile> -o <outputfile>’)

            sys.exit()

        elif opt in (“-i”, “–ifile”):

            inputfile = arg

        elif opt in (“-o”, “–ofile”):

            outputfile = arg

 

    print(‘Input file is’, inputfile)

    print(‘Output file is’, outputfile)

 

if __name__ == “__main__”:

    main(sys.argv[1:])

Explanation:

  • The script expects two options: -i or– ifile for input, and -o or– ofile for output.
  • If the arguments are invalid, an error message is printed, and the script exits.
  • Valid options set the values of inputfile and outputfile accordingly.

Running the Script

Command:

Python script.py -i input.txt -o output.txt

Output:

The input file is input.txt. The output file is output.txt

Advanced getopt Examples

As your scripts grow more sophisticated, you might need to support optional flags, validate inputs, or support a mix of options and positional arguments.

Supporting Optional Flags

import getopt

import sys

 

def main(argv):

    verbose = False

    try:

        opts, args = getopt.getopt(argv, “hv”, [“help”, “verbose”])

    Except getopt.GetoptError:

        print(‘Usage: script.py [-h|–help] [-v|–verbose]’)

        sys.exit(2)

 

    For opt, arg in opts:

        if opt in (“-h”, “–help”):

            print(‘Usage: script.py [-h|–help] [-v|–verbose]’)

            sys.exit()

        elif opt in (“-v”, “–verbose”):

            verbose = True

 

    If verbose:

        print(“Verbose mode enabled”)

 

if __name__ == “__main__”:

    main(sys.argv[1:])

This script allows toggling verbose mode with a flag. It demonstrates how boolean options can be implemented cleanly using getopt.

Error Handling and Validation

One of the strengths of getopt is that it provides automatic error handling. If an invalid option is passed, the script can immediately return an informative message.

Example: Validating Required Arguments

import sys

import getopt

 

def main(argv):

    inputfile = ”

    Try:

        opts, args = getopt.getopt(argv, “i:”, [“input=”])

    Except getopt.GetoptError:

        print(“Usage: script.py -i <inputfile>”)

        sys.exit(2)

 

    For opt, arg in opts:

        if opt in (“-i”, “–input”):

            inputfile = arg

 

    if inputfile == ”:

        print(“Input file is required. Usage: script.py -i <inputfile>”)

        sys.exit(2)

 

    print(“Processing file:”, inputfile)

 

if __name__ == “__main__”:

    main(sys.argv[1:])

This example validates that the user provided an input file and exits gracefully if the input is missing.

Mixing Options and Positional Arguments

You may sometimes need to allow both options and free-form arguments. Getopt can separate options from the remaining arguments so that you can handle both appropriately.

import sys, import getopt

 

def main(argv):

    outputfile = ”

    Try:

        opts, args = getopt.getopt(argv, “o:”, [“output=”])

    Except getopt.GetoptError:

        print(“Usage: script.py -o <outputfile> [data…]”)

        sys.exit(2)

 

    For opt, arg in opts:

        if opt in (“-o”, “–output”):

            outputfile = arg

 

    print(“Output file:”, outputfile)

    print(“Remaining arguments:”, args)

 

if __name__ == “__main__”:

    main(sys.argv[1:])

This approach is useful when a script needs to accept both structured options and flexible input values.

Common Use Cases

Configuration Management

Use getopt to allow users to define configuration files or settings from the CLI. This is often seen in tools that can run in different environments.

File Conversion Utilities

Scripts that convert data formats (e.g., CSV to JSON) often use getopt to specify input and output files, formats, or options like delimiters.

Batch Processing Scripts

Command line arguments can help automate batch operations, such as renaming files, resizing images, or processing logs.

Limitations of getopt

Despite its advantages, getopt is not without drawbacks:

  • Syntax is more verbose than argparse.
  • Error messages are less descriptive.
  • Lacks built-in help and usage text generation.

In cases where user-friendliness and scalability are priorities, consider using argparse or docopt instead.

Here is Part 3 focused on the argparse module with detailed explanations, examples, and best practices. It uses clear H2 headings throughout.

Introduction to the argparse Module in Python

While the getopt module is useful for basic command line argument parsing, modern Python scripts often require a more powerful and user-friendly solution. The argparse module is the recommended standard for this task. It allows developers to define complex argument structures, generate help messages automatically, validate inputs, and much more.

This part explores the argparse module in depth, explaining its core features, demonstrating practical examples, and offering guidance on writing effective CLI programs.

Why Use argparse?

The argparse module improves upon older modules like getopt by:

  • Providing an object-oriented API for argument parsing 
  • Supporting positional arguments, optional arguments, flags, and subcommands 
  • Automatically generating help and usage messages. 
  • Providing built-in validation and type conversion 
  • Allowing customization of argument parsing behavior and error handling 

Using argparse makes your scripts easier to use, more maintainable, and professional-looking.

Getting Started with argparse

To begin using argparse, import the module and create an ArgumentParser object:

import argparse

 

parser = argparse.ArgumentParser(description=”Example script using argparse.”)

 

This creates a parser that will handle command line arguments and display a description when the user requests help.

Adding Arguments

Arguments are added using the add_argument() method. This method supports many parameters to define argument behavior.

Positional Arguments

Positional arguments are mandatory and identified by their position:

parser.add_argument(“input”, help=”Input file name”)

parser.add_argument(“output”, help=”Output file name”)

 

Running the script without these arguments will display an error.

Optional Arguments

Optional arguments usually start with or . They can have default values and be flags, or take values:

parser.add_argument(“-v”, “–verbose”, action=”store_true”, help=”Enable verbose output”)

parser.add_argument(“-t”, “–timeout”, type=int, default=30, help=”Timeout in seconds”)

 

Flags and Boolean Options

Using action=”store_true” or action=”store_false” creates flags that toggle Boolean variables:

parser.add_argument(“–debug”, action=”store_true”, help=”Enable debug mode”)

 

Specifying Data Types

argparse automatically converts arguments to the specified types:

parser.add_argument(“–count”, type=int, help=”Number of retries”)

 

If the conversion fails, an error is displayed.

Parsing Arguments

After defining arguments, parse them with:

Args = parser.parse_args()

 

This returns an object where each argument is an attribute:

print(args.input)

print(args.verbose)

 

Example: Simple argparse Script

import argparse

 

parser = argparse.ArgumentParser(description=”Copy a file from input to output.”)

 

parser.add_argument(“input”, help=”Input file path”)

parser.add_argument(“output”, help=”Output file path”)

parser.add_argument(“-v”, “–verbose”, action=”store_true”, help=”Enable verbose mode”)

 

args = parser.parse_args()

 

If args. Verbose:

    print(f”Copying from {args.input} to {args.output}…”)

 

# Imagine file copying logic here

 

If args. verbose:

    print(“Copy complete.”)

 

Run:

python script.py source.txt dest.txt -v

 

Advanced Argument Features

Choices

Limit arguments to a fixed set of values:

parser.add_argument(“–mode”, choices=[“fast”, “slow”], default=”fast”, help=”Mode of operation”)

 

Required Optional Arguments

Normally, optional arguments are not required, but you can enforce this:

parser.add_argument(“-c”, “–config”, required=True, help=”Config file path”)

 

Multiple Values

Use nargs to accept multiple values:

parser.add_argument(“files”, nargs=”+”, help=”One or more input files”)

 

Default Values

Set defaults if no value is provided:

parser.add_argument(“–loglevel”, default=”INFO”, help=”Logging level”)

 

Subparsers: Handling Subcommands

argparse supports subcommands, useful for git-style CLI tools: Parser = argparse.ArgumentParser()

subparsers = parser.add_subparsers(dest=”command”)

 

# Create a subcommand “add”

parser_add = subparsers.add_parser(“add”)

parser_add.add_argument(“file”, help=”File to add”)

 

# Create a subcommand “remove”

parser_remove = subparsers.add_parser(“remove”)

parser_remove.add_argument(“file”, help=”File to remove”)

 

args = parser.parse_args()

 

if args.command == “add”:

    print(f”Adding {args.file}”)

elif args.command == “remove”:

    print(f”Removing {args.file}”)

Else:

    parser.print_help()

 

Run commands like:

python script.py add myfile.txt

python script.py remove myfile.txt

 

Customizing Help and Error Messages

You can customize help messages globally or per argument:

Parser = argparse.ArgumentParser(

    description “My program”,

    epilog “Thank you for using this program.”

)

 

parser.add_argument(“-q”, “–quiet”, action=”store_true”, help=”Suppress output”)

 

Use parser.error(“message”) to raise custom errors.

Handling Argument Conflicts

Sometimes arguments conflict. You can use mutually_exclusive_group() to enforce exclusivity:

group = parser.add_mutually_exclusive_group()

group.add_argument(“-a”, action=”store_true”)

group.add_argument(“-b”, action=”store_true”)

 

This prevents both -a and -b from being specified together.

Best Practices with argparse

  • Provide descriptive help messages for all arguments. 
  • Use type conversions to validate input early. 
  • Use subparsers to organize complex command-line interfaces. 
  • Leverage choices to restrict input options. 
  • Use default values to simplify usage. 
  • Keep user experience in mind when designing argument names and options. 

Real-world Examples

File Processing Script

import argparse

 

parser = argparse.ArgumentParser(description=”Process files”)

 

parser.add_argument(“files”, nargs=”+”, help=”Files to process”)

parser.add_argument(“-o”, “–output”, required=True, help=”Output directory”)

parser.add_argument(“-f”, “–format”, choices=[“csv”, “json”], default=”csv”, help=”Output format”)

parser.add_argument(“-v”, “–verbose”, action=”store_true”, help=”Verbose output”)

 

args = parser.parse_args()

 

If args. verbose:

    print(f”Processing {len(args.files)} files.”)

 

For a file in args.files:

    If args .verbose:

        print(f”Processing file {file}…”)

 

# Processing logic here

 

If arg s.verbose:

    print(f”Files saved to {args.output} in {args.format} format.”)

 

Network Utility

import argparse

 

parser = argparse.ArgumentParser(description=”Network ping utility”)

 

parser.add_argument(“host”, help=”Host to ping”)

parser.add_argument(“-c”, “–count”, type=int, default=4, help=”Number of packets to send”)

parser.add_argument(“-t”, “–timeout”, type=int, default=2, help=”Timeout in seconds”)

 

args = parser.parse_args()

 

print(f”Pinging {args.host} {args.count} times with {args.timeout}s timeout”)

 

Handling Complex Argument Types

You can define custom types by providing a function to type:

def parse_range(s):

    parts = s.split(‘-‘)

    if len(parts) != 2:

        raise argparse.ArgumentTypeError(“Range must be in format start-end”)

    start, end = map(int, parts)

    return range(start, end + 1)

 

parser.add_argument(“–range”, type=parse_range, help=”Range of values”)

 

The argparse module is a powerful, flexible, and user-friendly solution for command line argument parsing in Python. It supports everything from simple scripts with a few flags to complex applications with multiple subcommands and intricate input validation.

Mastering argparse will greatly improve the usability and professionalism of your Python command-line tools.

Introduction to docopt: Simplifying CLI Creation with Python

While argparse offers comprehensive and flexible command line parsing, it requires defining arguments programmatically, which can sometimes feel verbose or complicated for simpler or rapidly developed tools. This is where docopt shines.

The docopt module allows you to design command-line interfaces by writing a usage pattern in the program’s docstring. From this pattern, docopt automatically parses the command line arguments, validates input, and generates help messages.

This part explores docopt in detail, discusses when and how to use it, compares it with other parsing modules, shares practical advice, and offers troubleshooting strategies.

What is docopt?

Docopt is a Python library that creates command-line interfaces based on the conventions and layout of the usage documentation. It reads a specially formatted docstring and interprets it to understand what arguments and options the program expects.

Instead of writing multiple add_argument calls, you simply write the command’s usage as text. docopt then parses the command line based on this text.

This approach promotes documentation-first design: you write your CLI’s usage and options clearly in the docstring, and your parser follows.

Installing docopt

To use docopt, you need to install it first. Since it is not included in the Python standard library, install it via pip:

bash

CopyEdit

pip install docopt

 

After installation, you can import it into your scripts.

Writing Your First Docopt Script

Here’s an example showing the simplest use of docopt:

python

CopyEdit

“””

Usage:

  myscript.py greet <name>

  myscript.py (-h | –help)

  myscript.py-version

 

Options:

  -h– help     Show this help message.

  –version     Show version.

“””

 

from docopt import docopt

 

if __name__ == “__main__”:

    arguments = docopt(__doc__, version=”myscript 1.0″)

    print(arguments)

 

How This Works

  • The docstring defines the usage patterns and options. 
  • Docopt uses this to parse command-line arguments. 
  • The returned arguments are a dictionary mapping options and parameters to values. 
  • Running python myscript.py greet Alice would output: 

python

CopyEdit

{‘greet’: True, ‘<name>’: ‘Alice’, ‘–help’: False, ‘–version’: False}

 

  • Running with -h o— -help prints the usage message. 
  • Running with– version prints version information. 

Understanding Docopt Usage Patterns

The usage section is the heart of docopt. Here you specify all commands, options, and arguments.

Basic Syntax Rules

  • Each command or argument is listed in usage lines. 
  • Square brackets [ ] denote optional items. 
  • Ellipsis means “repeat zero or more times.” 
  • Vertical bar | indicates “or” between options. 
  • Angle brackets < > are used for positional arguments. 
  • Options usually start with or . 

Example:

less

CopyEdit

Usage:

  myscript.py upload <file> [–force]

  myscript.py download <file> [–output=<dir>]

  myscript.py -h | –help

 

This defines two commands with their arguments and options.

Options Section: Explaining Flags and Arguments

Below the usage section, the options describe what the flags mean:

bash

CopyEdit

Options:

  -h– help       Show help message

  –force         Overwrite existing files

  –output=<dir>  Specify output directory

 

The options section is used for help messages and for docopt to understand option types and defaults.

Advanced Features of docopt

Default Values

You can set default values by specifying them in the options section with an equals sign:

ini

CopyEdit

–count=<n>    Number of times to repeat [default: 1]

 

Argument Types and Validation

docopt treats everything as strings by default, but you can manually convert types in your code after parsing.

Combining Multiple Options and Commands

You can express complex command line interfaces involving multiple commands, options, and flags, all in one usage block.

Example:

lua

CopyEdit

Usage:

  tool.py add <file> [–verbose]

  tool.py remove <file> [–force]

  tool.py list [–all]

  tool.py -h | –help

 

Example: A Practical File Manager Script Using docopt

python

CopyEdit

“””

Usage:

  filemgr.py add <filename> [–verbose]

  filemgr.py remove <filename> [–force]

  filemgr.py list [–all]

  filemgr.py (-h | –help)

 

Options:

  -h– help     Show this screen.

  –verbose     Show detailed output.

  –force       Force removal without confirmation.

  –all         List all files, including hidden ones.

“””

 

from docopt import docopt

 

def main():

    args = docopt(__doc__)

    if args[‘add’]:

        if args[‘–verbose’]:

            print(f”Adding file {args[‘<filename>’]} with verbose output.”)

        Else:

            print(f”Adding file {args[‘<filename>’]}.”)

    elif args[‘remove’]:

        if args[‘–force’]:

            print(f”Force removing file {args[‘<filename>’]}.”)

        Else:

            print(f”Removing file {args[‘<filename>’]} with confirmation.”)

    elif args[‘list’]:

        if args[‘–all’]:

            print(“Listing all files including hidden.”)

        Else:

            print(“Listing visible files.”)

 

if __name__ == “__main__”:

    main()

 

How docopt Compares with argparse and getopt

Feature docopt argparse getopt
Definition Style Usage docstring-based Programmatic API Programmatic API
Ease of Use Very simple for docs-first Flexible and powerful Basic, less intuitive
Help Message Auto-generated from usage docstring Auto-generated Minimal help support
Complex CLI Supports complex CLI but needs careful docstring Supports complex CLI and subcommands Limited support
Type Conversion Manual after parsing Built-in Manual
Learning Curve Easy for users familiar with CLI docs Medium Medium

Docopt is excellent for quick development and ensuring that documentation and CLI stay in sync. Argparse is more flexible for complex requirements.

Best Practices for Using docopt

  • Write clear, consistent usage strings. 
  • Keep the usage and options section well-organized. 
  • Validate argument types explicitly after parsing. 
  • Use versioning and help options liberally. 
  • Test your usage patterns with multiple input variations. 
  • Document your CLI commands thoroughly. 

Common Troubleshooting with docopt

Problem: docopt Fails to Parse Arguments Correctly

  • Ensure your usage string exactly matches your expected commands. 
  • Check for syntax errors like missing brackets or incorrect indentation. 
  • Verify option spelling and presence in both usage and options sections. 

Problem: Help Message Not Showing as Expected

  • Include -h |– help explicitly in the usage string. 
  • Call docopt with the help=True argument if needed. 

Problem: Handling Type Conversions

  • Remember, docopt returns all arguments as strings. 
  • Convert integers, floats, or other types explicitly in your program. 

Problem: Confusing Option Defaults

  • Use [default: value] in the options section for clear defaults. 
  • Print parsed arguments to verify defaults are applied. 

Extending docopt with Custom Logic

Because docopt only parses, your program is free to implement any logic based on the parsed dictionary. This includes:

  • Advanced validation rules 
  • Conditional behavior depending on multiple options 
  • Integration with other modules or configuration files 

Real-World Use Cases for docopt

  • Small to medium CLI utilities where documentation clarity is key 
  • Rapid prototyping of command-line tools 
  • Projects where the CLI should match a well-defined specification or manual 
  • Scripts that benefit from readable, self-documenting usage 

Integrating Command Line Arguments with Other Python Code

Regardless of module choice (sys, getopt, argparse, or docopt), the parsed arguments must be integrated smoothly with your program’s logic:

  • Validate arguments thoroughly 
  • Handle errors gracefully 
  • Provide helpful feedback to the user.s 
  • Support interactive prompts if need.ed 
  • Log command line options for debugging. 

Comparing All Four Modules

Each module has its niche: Syss. argv: Simple, low-level access to CLI args, no parsing

 

  • getopt: Classic style parsing, similar to C getopt, for simple options 
  • argparse: Full-featured, modern CLI parsing with subcommands and validation 
  • docopt: Docstring-driven parsing, great for rapid development and clarity 

Summary and Final Thoughts

Mastering command line arguments in Python enhances your ability to write flexible, user-friendly scripts and applications. Whether you choose the simplicity of sys. argv, the classic style of getopt, the power of argparse, or the clarity of docopt, understanding their strengths and best practices is essential.

  • Use sys. argvgv for quick scripts or debugging. 
  • Use getopt for simple flag parsing where minimal dependencies matter. 
  • Use argparse for complex, professional-grade CLI tools with robust validation and subcommands. 
  • Use docopt for documentation-driven design and rapid prototyping with human-readable usage patterns. 

Being proficient in these tools improves the usability, maintainability, and professionalism of your Python programs.

 

img