absl (Abseil Python Common Libraries)(https://abseil.io/docs/python/)是用于构建Python应用程序的Python库代码集合,它包括三个子库:app, flags, logging。
app
app是Abseil Python应用程序的通用入口点。
flags
absl.flags定义了分布式的命令行系统。flags类型包括boolean, float, integer, list, string等,通过使用DEFINE_*函数来定义,其中的*表示flags类型。
<代码示例>
编写代码test.py
from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('name', 'Jane Random', 'Your name.')
flags.DEFINE_integer('age', None, 'Your age in years.', lower_bound=0)
flags.DEFINE_boolean('debug', False, 'Produces debugging output.')
flags.DEFINE_enum('job', 'running', ['running', 'stopped'], 'Job status.')
def main(argv):
if FLAGS.debug:
print('non-flag arguments:', argv)
print('Hi', FLAGS.name)
if FLAGS.age is not None:
print('You are %d years old, and your job is %s' % (FLAGS.age, FLAGS.job))
if __name__ == '__main__':
app.run(main)
执行test.py输出结果
~/tmp$ python test.py --name=yaya --age=18 --debug=True
non-flag arguments: ['test.py']
Hi yaya
You are 18 years old, and your job is running
标签:总结,name,abseil,app,python,FLAGS,flags,age,DEFINE From: https://www.cnblogs.com/chaimy/p/17047360.html