0

I am building on an existing program, which uses absl to parse its flags. To be more concrete, I have to separate applications, which run standalone. Both of them have the same base structure:

from absl import app
from absl import flags

FLAGS = flags.FLAGS 
flags.DEFINE_integer('name', None, 'description')
# more flags
flags.mark_flag_as_required('name')

def main(argv):
   # some code using the flags

if __name__ == "__main__":
    app.run(main)

Each one by itself works perfectly.

I am now supposed to use the output of one of the apps and feed it into the other using an additional script. As they still require the flags, I have to call them in a different way:

# importing the module I want to use
import obj1 as o1
# set flags
o1.FLAGS(["--name=5"])
# call method
o1.main([])

If I just do this with one of them, this works perfectly fine. But if I am trying to use both of them in the script, the one called second complains about flags not being set, even if the method is not even called.

I do not know much about absl and how it is working, but I imagine that there is just one instance of it running which therefore requires all flags marked as required, even if they are not used.

My solution now was to add all the flags for both programs every time I want to use one of them separately. This seems to work, but is not very elegant:

# pass both flags, even though for now only the first one is required
o1.FLAGS(["--name1=5", "--name2=4"])

Here is a minimal example to reproduce:

main.py

import obj1 as o1
import obj2 as o2

def main():
    o1.FLAGS(["--runs=5"])
    o1.main([])
    o2.FLAGS(["--epochs=5"])
    o2.main([])

if __name__ == '__main__':
    main()

obj1.py

from absl import flags

FLAGS = flags.FLAGS
flags.DEFINE_integer('runs', None, 'number of simulations')
flags.mark_flag_as_required("runs")

def main(argv):
    print(FLAGS.runs)

obj2.py

from absl import flags

FLAGS = flags.FLAGS
flags.DEFINE_integer('epochs', None, 'number of training epochs')
flags.mark_flag_as_required("epochs")

def main(argv):
    print(FLAGS.epochs)

Thanks for any help!

Jonas Z.
  • 17
  • 5
wittn
  • 298
  • 5
  • 16

0 Answers0