This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change on 2016/09/30 by vrusinov <[email protected]> ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=134779382
- Loading branch information
Showing
10 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env python | ||
"""Example for basic usage of the flags library.""" | ||
|
||
from google.apputils import app | ||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
# Flag names are globally defined! So in general, we need to be | ||
# careful to pick names that are unlikely to be used by other libraries. | ||
# If there is a conflict, we'll get an error at import time. | ||
gflags.DEFINE_string('name', 'Mr. President', 'your name') | ||
gflags.DEFINE_integer('age', None, 'your age in years', lower_bound=0) | ||
gflags.DEFINE_boolean('debug', False, 'produces debugging output') | ||
gflags.DEFINE_enum('job', 'running', ['running', 'stopped'], 'job status') | ||
|
||
|
||
def main(argv): | ||
if FLAGS.debug: | ||
print 'non-flag arguments:', argv | ||
print 'Happy Birthday', 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
"""Example for appcomands flag usage.""" | ||
|
||
from __future__ import print_function | ||
|
||
from google.apputils import appcommands | ||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
|
||
class HelloWorld(appcommands.Cmd): | ||
|
||
def __init__(self, name, flag_values, **kargs): | ||
super(HelloWorld, self).__init__(name, flag_values, kargs) | ||
|
||
# Define the new flag inside the __init__ function of the class. | ||
gflags.DEFINE_bool('world', False, 'Display world as well', | ||
flag_values=flag_values) | ||
|
||
def Run(self, argv): | ||
# Output different things depending on flag value. | ||
if FLAGS.world: | ||
print('Hello world') | ||
else: | ||
print('Hello') | ||
|
||
|
||
def main(unused_argv): | ||
appcommands.AddCmd('hello', HelloWorld, help_full='Runs hello world') | ||
|
||
|
||
if __name__ == '__main__': | ||
appcommands.Run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
"""Defines some other flags.""" | ||
|
||
import gflags | ||
|
||
gflags.DEFINE_string('bar_gfs_path', '/gfs/path', | ||
'Path to the GFS files for libbar.') | ||
gflags.DEFINE_string('email_for_bar_errors', '[email protected]', | ||
'Email address for bug reports about module libbar.') | ||
gflags.DEFINE_boolean('bar_risky_hack', False, | ||
'Turn on an experimental and buggy optimization.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python | ||
"""Defines some flags.""" | ||
|
||
import gflags | ||
|
||
gflags.DEFINE_integer('num_replicas', 3, 'Number of replicas to start') | ||
gflags.DEFINE_boolean('rpc2', True, 'Turn on the usage of RPC2.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python | ||
"""Adopts all flags from libfoo and one flag from libbar.""" | ||
|
||
import gflags | ||
from gflags.examples import libbar # pylint: disable=unused-import | ||
from gflags.examples import libfoo | ||
|
||
gflags.DEFINE_integer('num_iterations', 0, 'Number of iterations.') | ||
|
||
# Declare that all flags that are key for libfoo are | ||
# key for this module too. | ||
gflags.ADOPT_module_key_flags(libfoo) | ||
|
||
# Declare that the flag --bar_gfs_path (defined in libbar) is key | ||
# for this module. | ||
gflags.DECLARE_key_flag('bar_gfs_path') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
"""Broken due to access to flags before parsing.""" | ||
|
||
from google.apputils import app | ||
import gflags | ||
|
||
# Defines string with default value of 'default value' | ||
gflags.DEFINE_string('test_flag', 'default value', 'Test flag') | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
# Assigns value of FLAGS.test_flag (string) to MY_CONST during module execution. | ||
# Since flags were not parsed yet FLAGS.test_flag will always return default | ||
# value of the flag. | ||
MY_CONST = FLAGS.test_flag | ||
|
||
|
||
def main(_): | ||
print MY_CONST # Will ALWAYS output 'default value' | ||
|
||
if __name__ == '__main__': | ||
app.run() # Does a lot of useful stuff, including parsing flags. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
"""FLAGS.Reset() is evli.""" | ||
|
||
import unittest | ||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
|
||
class CertCheckTest(googletest.TestCase): | ||
|
||
def setUp(self): | ||
FLAGS.Reset() # Resets all flags, including stuff like test_srcdir. | ||
|
||
def testFoo(self): | ||
FLAGS.foo = 'bar' | ||
self.assertEqual(2, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python | ||
"""Example of the code that accesses flags at top of the module.""" | ||
|
||
import os | ||
import unittest | ||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
OUTPUT_DIR = os.path.join(FLAGS.my_dir, 'my_subdir') | ||
|
||
|
||
class MyTest(googletest.TestCase): | ||
|
||
def setUp(self): | ||
self.filename = os.path.join(OUTPUT_DIR, 'filename') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python | ||
"""Proposed fix.""" | ||
|
||
import os | ||
import unittest | ||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
|
||
def _get_output_dir(): | ||
return os.path.join(FLAGS.my_dir, 'my_subdir') | ||
|
||
|
||
class MyTest(googletest.TestCase): | ||
|
||
def setUp(self): | ||
self.filename = os.path.join(_get_output_dir(), 'filename') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env python | ||
"""RegisterValidator example.""" | ||
|
||
import gflags | ||
|
||
FLAGS = gflags.FLAGS | ||
|
||
gflags.DEFINE_integer('my_version', 0, 'Version number.') | ||
gflags.DEFINE_string('filename', None, 'Input file name', short_name='f') | ||
|
||
gflags.RegisterValidator('my_version', | ||
lambda value: value % 2 == 0, | ||
message='--my_version must be divisible by 2') | ||
gflags.MarkFlagAsRequired('filename') |