Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Unified Diff: third_party/typ/typ/arg_parser.py

Issue 660133004: Roll typ to v0.8.5 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/typ/typ/__main__.py ('k') | third_party/typ/typ/cmdline.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/typ/typ/arg_parser.py
diff --git a/third_party/typ/typ/arg_parser.py b/third_party/typ/typ/arg_parser.py
index ac2d33e44a5ef52a1c1182804769ba4e98d5048e..b55dee8376d25fef5b20200956e4dc933ab3212d 100644
--- a/third_party/typ/typ/arg_parser.py
+++ b/third_party/typ/typ/arg_parser.py
@@ -246,6 +246,49 @@ class ArgumentParser(argparse.ArgumentParser):
options.append(optparse.make_option(*args, **kwargs))
return options
+ def argv_from_args(self, args):
+ default_parser = ArgumentParser(host=self._host)
+ default_args = default_parser.parse_args([])
+ argv = []
+ tests = []
+ d = vars(args)
+ for k in sorted(d.keys()):
+ v = d[k]
+ argname = _argname_from_key(k)
+ action = self._action_for_key(k)
+ action_str = _action_str(action)
+ if k == 'tests':
+ tests = v
+ continue
+ if getattr(default_args, k) == v:
+ # this arg has the default value, so skip it.
+ continue
+
+ assert action_str in ['append', 'count', 'store', 'store_true']
+ if action_str == 'append':
+ for el in v:
+ argv.append(argname)
+ argv.append(el)
+ elif action_str == 'count':
+ for _ in range(v):
+ argv.append(argname)
+ elif action_str == 'store':
+ argv.append(argname)
+ argv.append(str(v))
+ else:
+ # action_str == 'store_true'
+ argv.append(argname)
+
+ return argv + tests
+
+ def _action_for_key(self, key):
+ for action in self._actions:
+ if action.dest == key:
+ return action
+
+ assert False, ('Could not find an action for %s' # pragma: no cover
+ % key)
+
def _action_str(action):
# Access to a protected member pylint: disable=W0212
@@ -264,3 +307,7 @@ def _action_str(action):
return 'store'
if isinstance(action, argparse._StoreTrueAction):
return 'store_true'
+
+
+def _argname_from_key(key):
+ return '--' + key.replace('_', '-')
« no previous file with comments | « third_party/typ/typ/__main__.py ('k') | third_party/typ/typ/cmdline.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698