Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Add examples to opensource version.
Browse files Browse the repository at this point in the history
	Change on 2016/09/30 by vrusinov <[email protected]>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134779382
  • Loading branch information
vrusinov committed Oct 22, 2016
1 parent 9e10759 commit 6ff6009
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/basic_usage.py
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()
34 changes: 34 additions & 0 deletions examples/command_flags.py
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()
11 changes: 11 additions & 0 deletions examples/libbar.py
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.')
7 changes: 7 additions & 0 deletions examples/libfoo.py
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.')
16 changes: 16 additions & 0 deletions examples/myscript.py
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')
22 changes: 22 additions & 0 deletions examples/use_before_parse_broken.py
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.
17 changes: 17 additions & 0 deletions examples/use_before_parse_reset_broken.py
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)
16 changes: 16 additions & 0 deletions examples/use_before_parse_variable_broken.py
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')
18 changes: 18 additions & 0 deletions examples/use_before_parse_variable_fixed.py
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')
14 changes: 14 additions & 0 deletions examples/validator.py
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')

0 comments on commit 6ff6009

Please sign in to comment.