How to mark a flag as required only if another flag is set in absl-py?

592 views Asked by At

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?

1

There are 1 answers

0
isaactfa On BEST ANSWER

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)