Support for multiple instances of a boolean flag in python-gflags
Python
Switch branches/tags
Nothing to show
Latest commit e31f216 Mar 22, 2014 @adamdeprince adamdeprince Rename package
Permalink
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
README.md Rename package Mar 22, 2014
setup.py Rename package Mar 22, 2014

README.md

gflags_multibool

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.

Installation

pip install python-gflags-multibool

Example

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

#!/usr/bin/env python
# demo_multibool.py

import gflags
import gflags_multibool

FLAGS = gflags.FLAGS

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

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

And we now run this on the command line:

python demo_multibool.py 
Verbosity level: 0

python demo_mutlibool.py -v -v -v 
Verbosity level: 3

python demo_multibool.py --verbose --verbose --noverbose
Verbosity level: 1