proxygen
parse_args.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) Facebook, Inc. and its affiliates.
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 'Argument parsing logic shared by all fbcode_builder CLI tools.'
8 
9 import argparse
10 import logging
11 
12 from shell_quoting import raw_shell, ShellQuoted
13 
14 
15 def parse_args_to_fbcode_builder_opts(add_args_fn, top_level_opts, opts, help):
16  '''
17 
18  Provides some standard arguments: --debug, --option, --shell-quoted-option
19 
20  Then, calls `add_args_fn(parser)` to add application-specific arguments.
21 
22  `opts` are first used as defaults for the various command-line
23  arguments. Then, the parsed arguments are mapped back into `opts`,
24  which then become the values for `FBCodeBuilder.option()`, to be used
25  both by the builder and by `get_steps_fn()`.
26 
27  `help` is printed in response to the `--help` argument.
28 
29  '''
30  top_level_opts = set(top_level_opts)
31 
32  parser = argparse.ArgumentParser(
33  description=help,
34  formatter_class=argparse.RawDescriptionHelpFormatter
35  )
36 
37  add_args_fn(parser)
38 
39  parser.add_argument(
40  '--option', nargs=2, metavar=('KEY', 'VALUE'), action='append',
41  default=[
42  (k, v) for k, v in opts.items()
43  if k not in top_level_opts and not isinstance(v, ShellQuoted)
44  ],
45  help='Set project-specific options. These are assumed to be raw '
46  'strings, to be shell-escaped as needed. Default: %(default)s.',
47  )
48  parser.add_argument(
49  '--shell-quoted-option', nargs=2, metavar=('KEY', 'VALUE'),
50  action='append',
51  default=[
52  (k, raw_shell(v)) for k, v in opts.items()
53  if k not in top_level_opts and isinstance(v, ShellQuoted)
54  ],
55  help='Set project-specific options. These are assumed to be shell-'
56  'quoted, and may be used in commands as-is. Default: %(default)s.',
57  )
58 
59  parser.add_argument('--debug', action='store_true', help='Log more')
60  args = parser.parse_args()
61 
62  logging.basicConfig(
63  level=logging.DEBUG if args.debug else logging.INFO,
64  format='%(levelname)s: %(message)s'
65  )
66 
67  # Map command-line args back into opts.
68  logging.debug('opts before command-line arguments: {0}'.format(opts))
69 
70  new_opts = {}
71  for key in top_level_opts:
72  val = getattr(args, key)
73  # Allow clients to unset a default by passing a value of None in opts
74  if val is not None:
75  new_opts[key] = val
76  for key, val in args.option:
77  new_opts[key] = val
78  for key, val in args.shell_quoted_option:
79  new_opts[key] = ShellQuoted(val)
80 
81  logging.debug('opts after command-line arguments: {0}'.format(new_opts))
82 
83  return new_opts
def raw_shell(s)
def parse_args_to_fbcode_builder_opts(add_args_fn, top_level_opts, opts, help)
Definition: parse_args.py:15
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
Definition: Traits.h:592