proxygen
parse_args Namespace Reference

Functions

def parse_args_to_fbcode_builder_opts (add_args_fn, top_level_opts, opts, help)
 

Function Documentation

def parse_args.parse_args_to_fbcode_builder_opts (   add_args_fn,
  top_level_opts,
  opts,
  help 
)
Provides some standard arguments: --debug, --option, --shell-quoted-option

Then, calls `add_args_fn(parser)` to add application-specific arguments.

`opts` are first used as defaults for the various command-line
arguments.  Then, the parsed arguments are mapped back into `opts`,
which then become the values for `FBCodeBuilder.option()`, to be used
both by the builder and by `get_steps_fn()`.

`help` is printed in response to the `--help` argument.

Definition at line 15 of file parse_args.py.

References folly.format(), and shell_quoting.raw_shell().

Referenced by make_docker_context.make_docker_context().

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
84 
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