Logo

Programming-Idioms

  • Python
  • Scala
  • C++

Idiom #232 Read a command line boolean flag

Print "verbose is true" if the flag -v was passed to the program command line, "verbose is false" otherwise.

from sys import argv
print('verbose is', '-v' in argv[1:])
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', action='store_true', dest='verbose')
args = parser.parse_args()
print('verbose is', args.verbose)
print("verbose is ${args.contains("-v")}");

New implementation...
< >
programming-idioms.org