在这个例子中,我是如何误用标志的呢?在命令行上设置标志并不覆盖默认值,但当我根据需要设置标志,并且不使用默认值(注释代码)时,它就会被设置为( False)。
import gflags
from gflags import FLAGS
#gflags.DEFINE_bool('use_cache', None, '')
#gflags.MarkFlagAsRequired('use_cache')
gflags.DEFINE_bool('use_cache', True, '')
if __name__ == '__main__':
if FLAGS.use_cache == True:
print 'use_cache == True'
else:
print 'use_cache == False'。
~ python testflags.py --use_cache=False
use_cache == True
~ python testflags.py --help
use_cache == True发布于 2014-12-22 08:38:26
用于解析的调用标志(Argv)。
示例用法:
FLAGS = gflags.FLAGS
# Flag names are globally defined! So in general, we need to be
# careful to pick names that are unlikely to be used by other libraries.
# If there is a conflict, we'll get an error at import time.
gflags.DEFINE_string('name', 'Mr. President', 'your name')
gflags.DEFINE_integer('age', None, 'your age in years', lower_bound=0)
gflags.DEFINE_boolean('debug', False, 'produces debugging output')
gflags.DEFINE_enum('gender', 'male', ['male', 'female'], 'your gender')
def main(argv):
try:
argv = FLAGS(argv) # parse flags
except gflags.FlagsError, e:
print '%s\\nUsage: %s ARGS\\n%s' % (e, sys.argv[0], FLAGS)
sys.exit(1)
if FLAGS.debug: print 'non-flag arguments:', argv
print 'Happy Birthday', FLAGS.name
if FLAGS.age is not None:
print 'You are a %d year old %s' % (FLAGS.age, FLAGS.gender)
if __name__ == '__main__':
main(sys.argv)https://stackoverflow.com/questions/26475635
复制相似问题