Python Gflags

3.1.2 · deprecated · verified Wed Apr 15

Python-gflags is a Python implementation of the Google command-line flags module, enabling distributed command-line systems by allowing flags to be defined across different modules. It is currently at version 3.1.2. This library is now obsolete and no longer maintained; users are strongly encouraged to migrate to `absl-py` for continued support and new features.

Warnings

Install

Imports

Quickstart

Defines two flags (`name` and `age`) and a boolean `debug` flag. It then parses command-line arguments and prints a greeting. Run with `python your_script.py --name=Python --age=10 --debug`.

import sys
import gflags as flags

FLAGS = flags.FLAGS

flags.DEFINE_string('name', 'World', 'Your name.')
flags.DEFINE_integer('age', 30, 'Your age in years.', lower_bound=0)
flags.DEFINE_boolean('debug', False, 'Enable debug output.')

def main(argv):
    try:
        argv = FLAGS(argv)
    except flags.FlagsError as e:
        print(f'{e}\nUsage: {sys.argv[0]} ARGS\n{FLAGS}')
        sys.exit(1)

    if FLAGS.debug:
        print(f'Debug mode enabled. Non-flag arguments: {argv}')

    print(f'Hello, {FLAGS.name}! You are {FLAGS.age} years old.')

if __name__ == '__main__':
    main(sys.argv)

view raw JSON →