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.' 12 from shell_quoting
import raw_shell, ShellQuoted
18 Provides some standard arguments: --debug, --option, --shell-quoted-option 20 Then, calls `add_args_fn(parser)` to add application-specific arguments. 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()`. 27 `help` is printed in response to the `--help` argument. 30 top_level_opts =
set(top_level_opts)
32 parser = argparse.ArgumentParser(
34 formatter_class=argparse.RawDescriptionHelpFormatter
40 '--option', nargs=2, metavar=(
'KEY',
'VALUE'), action=
'append',
42 (k, v)
for k, v
in opts.items()
43 if k
not in top_level_opts
and not isinstance(v, ShellQuoted)
45 help=
'Set project-specific options. These are assumed to be raw ' 46 'strings, to be shell-escaped as needed. Default: %(default)s.',
49 '--shell-quoted-option', nargs=2, metavar=(
'KEY',
'VALUE'),
52 (k,
raw_shell(v))
for k, v
in opts.items()
53 if k
not in top_level_opts
and isinstance(v, ShellQuoted)
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.',
59 parser.add_argument(
'--debug', action=
'store_true', help=
'Log more')
60 args = parser.parse_args()
63 level=logging.DEBUG
if args.debug
else logging.INFO,
64 format=
'%(levelname)s: %(message)s' 68 logging.debug(
'opts before command-line arguments: {0}'.
format(opts))
71 for key
in top_level_opts:
72 val = getattr(args, key)
76 for key, val
in args.option:
78 for key, val
in args.shell_quoted_option:
79 new_opts[key] = ShellQuoted(val)
81 logging.debug(
'opts after command-line arguments: {0}'.
format(new_opts))
def parse_args_to_fbcode_builder_opts(add_args_fn, top_level_opts, opts, help)
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)