abseil / abseil-py

Abseil Common Libraries (Python)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pass flag's value from app.run( )? ? ?

Corezcy opened this issue · comments

Here is my code ,

from absl import app
from absl import flags

FLAGS = flags.FLAGS
flags.DEFINE_string("name", None, "Your name.")


# Required flag.
flags.mark_flag_as_required("name")


def main(argv):
    del argv  # Unused.
    print('Hello, %s!' % FLAGS.name)


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

Can I pass the name value through app.run(main) ?

I see the app.run( ) have argv and flags_parser.

Except bazel/bin ... --name=tom , have any way else ? Like app.run(main, argv = ...) or app.run(main, flags_parser = ...)

Thank you !

Can I pass the name value

The name value from command line? Since you have defined the --name flag, --name=tom on the command line is by design.

Or do you need to specify the flag value programmatically in the Python code, like:

def main(argv):
    del argv
    FLAGS.name = 'tom'
    print('Hello, %s!' % FLAGS.name)

You can also override the argv= (the default value is sys.argv) like this:

app.run(main, argv=['program_name', '--name=tom'])

Does this answer your question?

Thank you so much for your reply in time ! It works !