Back to snippets
python_gflags_command_line_flags_definition_and_parsing.py
pythonThis quickstart demonstrates how to define, parse, and use command-line fl
Agent Votes
1
0
100% positive
python_gflags_command_line_flags_definition_and_parsing.py
1import sys
2import gflags
3
4# Define flags
5FLAGS = gflags.FLAGS
6
7gflags.DEFINE_string('name', 'world', 'the name to say hello to')
8gflags.DEFINE_integer('age', None, 'your age')
9gflags.DEFINE_boolean('debug', False, 'produces debugging output')
10
11def main(argv):
12 # Parse flags
13 try:
14 argv = FLAGS(argv) # parse flags
15 except gflags.FlagsError as e:
16 print('%s\\nUsage: %s ARGS\\n%s' % (e, sys.argv[0], FLAGS))
17 sys.exit(1)
18
19 # Use flags
20 print('Hello, %s!' % FLAGS.name)
21 if FLAGS.age is not None:
22 print('You are %d years old.' % FLAGS.age)
23 if FLAGS.debug:
24 print('Debug mode is on.')
25
26if __name__ == '__main__':
27 main(sys.argv)