Learn how to effectively manage command-line arguments in Python using `argparse`. This guide will help you create a flag that behaves like `NULL` when not used, defaults to `foo` when used without an argument, and accepts user values when specified. --- This video is based on the question https://stackoverflow.com/q/62746576/ asked by the user 'user760900' ( https://stackoverflow.com/u/13112739/ ) and on the answer https://stackoverflow.com/a/62746657/ provided by the user 'alani' ( https://stackoverflow.com/u/13596037/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Argument flag that is null when not used and default value when used without argument Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Handling Command-Line Arguments with argparse: A Guide to Custom Flags Command-line interfaces (CLI) are essential tools for developers, allowing users to interact with scripts and applications efficiently. When working with Python's argparse library, one common requirement is to create a flexible command-line argument flag that behaves differently depending on its usage. This guide will walk you through creating a flag that returns None when not specified, adopts a default value when used without an argument, and captures any user-provided value when necessary. Problem Overview You want to implement a command-line argument flag using argparse that functions as follows: When the script runs without the flag: ap.bar should be None. When the flag is used without an argument: ap.bar should set to a default value, foo. When the flag is used with a specified argument: ap.bar should reflect that argument's value. The initial setup looks like this: [[See Video to Reveal this Text or Code Snippet]] Here, the --bar flag is added, but it doesn't yet have the desired behavior. The Solution You can easily modify your argparse implementation to achieve the desired functionality by adjusting the way the argument is defined. Here’s the updated code snippet that accomplishes your goals: [[See Video to Reveal this Text or Code Snippet]] Breakdown of the Code Import the argparse Module: This allows you to use the command-line parsing functionalities provided by Python. Create a Parser Object: The ArgumentParser is where you define all your flags and arguments. Adding the Argument: --bar: This is the flag you are defining. nargs='?': This specifies that the argument can be given zero or one time. It means the argument can be absent (zero) or provided with a value (one), allowing for the flexibility you need. const='foo': When the --bar flag is used without an associated argument, it sets ap.bar to the default value, foo. Parse the Arguments: args = ap.parse_args() processes the command-line arguments. Print the Result: This outputs the value of ap.bar, which will vary based on how the script is run. Expected Outputs When you run your script with different commands, you will see the following results: No Flag: [[See Video to Reveal this Text or Code Snippet]] Flag Only: [[See Video to Reveal this Text or Code Snippet]] Flag with Argument: [[See Video to Reveal this Text or Code Snippet]] Tips and Tricks Understanding the nargs parameter is essential for creating flexible command-line interfaces. In this example, nargs='?' means that the --bar argument can accept zero or one argument, similar to how the ? regex operator functions, denoting optional matches. Final Thoughts Customizing command-line arguments with argparse can enhance your Python scripts by giving users more control and flexibility. Implementing the above solution will allow your script to handle different input scenarios gracefully. With just a few simple modifications, you can turn your command-line utility into a more user-friendly tool. Happy coding!