Skip to content

Commit

Permalink
Fix incorrect behavior in argv.StoreInModule (#389)
Browse files Browse the repository at this point in the history
Also document it now that it works :)

Previously, this would always try to set attributes in the openhtf module itself, not openhtf.util.logs or whichever module was correct.
  • Loading branch information
fahhem authored and jettisonjoe committed Jul 28, 2016
1 parent 41062a2 commit 2f231b3
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions openhtf/util/argv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
"""Utilities for handling command line arguments."""
"""Utilities for handling command line arguments.
StoreInModule:
Enables emulating a gflags-esque API (flag affects global value), but one
doesn't necessarily need to use flags to set values.
Example usage:
DEFAULT_VALUE = 0
ARG_PARSER = argv.ModuleParser()
ARG_PARSER.add_argument(
'--override-value', action=argv.StoreInModule,
default=DEFAULT_VALUE, target='%s.DEFAULT_VALUE' % __name__)
Then in an entry point (main() function), use that parser as a parent:
parser = argparse.ArgumentParser(parents=[other_module.ARG_PARSER])
parser.parse_args()
"""

import argparse

Expand All @@ -19,5 +35,7 @@ def __init__(self, *args, **kwargs):
def __call__(self, parser, namespace, values, option_string=None):
if hasattr(self, '_proxy'):
values = self._proxy(parser, namespace, values)
setattr(__import__(self._tgt_mod), self._tgt_attr, values)
base, mod = self._tgt_mod.rsplit('.', 1)
module = getattr(__import__(base, fromlist=[mod]), mod)
setattr(module, self._tgt_attr, values)

0 comments on commit 2f231b3

Please sign in to comment.