Fix Python – How to implement the –verbose or -v option into a script?

I know the –verbose or -v from several tools and I’d like to implement this into some of my own scripts and tools.
I thought of placing:
if verbose:
print …

through my source code, so that if a user passes the -v option, the variable verbose will be set to True and the text will be printed.
Is this the right approach or is there a more com….

Fix Python – Parse config files, environment, and command-line arguments, to get a single collection of options

Python’s standard library has modules for configuration file parsing (configparser), environment variable reading (os.environ), and command-line argument parsing (argparse). I want to write a program that does all those, and also:

Has a cascade of option values:

default option values, overridden by
config file options, overridden by
environment ….

Fix Python – Python argparse command line flags without arguments

How do I add an optional flag to my command line args?
eg. so I can write
python myprog.py

or
python myprog.py -w

I tried
parser.add_argument(‘-w’)

But I just get an error message saying
Usage [-w W]
error: argument -w: expected one argument

which I take it means that it wants an argument value for the -w option. What’s the way of just acc….

Fix Python – Parsing boolean values with argparse

I would like to use argparse to parse boolean command-line arguments written as “–foo True” or “–foo False”. For example:
my_program –my_boolean_flag False

However, the following test code does not do what I would like:
import argparse
parser = argparse.ArgumentParser(description=”My parser”)
parser.add_argument(“–my_bool”, type=bool)
cmd_lin….