1

I have two flags f1 and f2, f1 is a boolean flag. I want to mark f2 as required whenever f1 is set to True. Is this possible with absl-py?

Priyatham
  • 2,821
  • 1
  • 19
  • 33

1 Answers1

1

You can use a validator for that:

from absl import app
from absl import flags

FLAGS = flags.FLAGS

flags.DEFINE_bool("f1", False, "some flag")
flags.DEFINE_string("f2", None, "some other flag")
flags.register_validator(
    # the flag to validate
    "f1",
    # a function that takes that flag's value and returns whether it's valid
    lambda value: not value or FLAGS.f2 is not None,
    # a message to print if it isn't
    message="if f1 is set, f2 needs to be set"
)

def main(argv):
    pass

if __name__ == '__main__':
    app.run(main)
isaactfa
  • 5,461
  • 1
  • 10
  • 24