Support for multiple instances of a boolean flag in python-gflags
Switch branches/tags
Nothing to show
Latest commit e31f216 Mar 22, 2014 @adamdeprince adamdeprince Rename package
Failed to load latest commit information.
examples We never ever want to go below 0 Mar 22, 2014
gflags_multibool We never ever want to go below 0 Mar 22, 2014
.gitignore Initial checkin Mar 22, 2014
LICENSE Initial commit Mar 22, 2014 Rename package Mar 22, 2014 Rename package Mar 22, 2014


Multiple uses of the verbose flag to indicate successively greatere degrees of verbosity is a common convention in Unix utilities due to the relative ease of implementing this feature in C's getops library.

Google's GFlags library opts for a somewhat more modern approach; instead of -vvv a developer might use gflag's enum type and write --verbosity=DEBUG. This approach, however, makes Gflags incompatible with tools and conventions that specifically require the use of multiple flags. Once such instance is the Nagios alerting enviroment; it specifically requires that fully compatible modules suport four levels of verbosity ranging from no verbose flags to -vvv.

gflags_multibool makes it easy to write command line parsers that support this convention.


pip install python-gflags-multibool


To see gflags_multibool in action we first create a minimal GFLAGS based application

#!/usr/bin/env python

import gflags
import gflags_multibool

FLAGS = gflags.FLAGS

gflags_multibool.DEFINE_multibool('verbose', 0, 'Set verbosity', short_name='v')

def main(argv):
    print("Verbosity level:", FLAGS.verbose)
if __name__ == "__main__" 

And we now run this on the command line:

Verbosity level: 0

python -v -v -v 
Verbosity level: 3

python --verbose --verbose --noverbose
Verbosity level: 1