参考官方仓库:https://github.com/tensorflow/models/tree/master/official/utils/flags
测试Demo代码如下:
from absl import app as absl_app from absl import flags from official.utils.flags import core as flags_core flags.DEFINE_string(name="my_flag_a", default="aaa", help="an example flag") flags.DEFINE_string(name="my_flag_b", default="bbb", help="an other example flag") def main(_): flags_obj = flags.FLAGS print(flags_obj) print(flags_obj.my_flag_a) print(flags_obj.my_flag_b) if __name__ == "__main__": absl_app.run(main)
Terminal运行执行如下脚本:
python tensorflow_example/test_absl_flags.py --log_dir "./logs" --my_flag_a "flag_aaa"
输出结果:
tensorflow_example/test_absl_flags.py: --my_flag_a: an example flag (default: 'aaa') --my_flag_b: an other example flag (default: 'bbb') absl.app: -?,--[no]help: show this help (default: 'false') --[no]helpfull: show full help (default: 'false') -h,--[no]helpshort: show this help (default: 'false') ...... absl.logging: --[no]alsologtostderr: also log to stderr? (default: 'false') --log_dir: directory to write logfiles into ...... absl.flags: --flagfile: Insert flag definitions from the given file into the command line. (default: '') --undefok: comma-separated list of flag names that it is okay to specify on the command line even if the program does not define a flag with that name. IMPORTANT: flags in this list that have arguments MUST use the --flag=value format. (default: '') flag_aaa bbb
其中最后两行,表示flags_obj.my_flag_a为设置后的值,flags_obj.my_flag_b为默认值