1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this file,
3# You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function, unicode_literals
6
7from textwrap import TextWrapper
8
9from mach.config import TYPE_CLASSES
10from mach.decorators import CommandArgument, Command
11
12
13# Interact with settings for mach.
14
15# Currently, we only provide functionality to view what settings are
16# available. In the future, this module will be used to modify settings, help
17# people create configs via a wizard, etc.
18
19
20@Command("settings", category="devenv", description="Show available config settings.")
21@CommandArgument(
22    "-l",
23    "--list",
24    dest="short",
25    action="store_true",
26    help="Show settings in a concise list",
27)
28def run_settings(command_context, short=None):
29    """List available settings."""
30    types = {v: k for k, v in TYPE_CLASSES.items()}
31    wrapper = TextWrapper(initial_indent="# ", subsequent_indent="# ")
32    for i, section in enumerate(sorted(command_context._mach_context.settings)):
33        if not short:
34            print("%s[%s]" % ("" if i == 0 else "\n", section))
35
36        for option in sorted(command_context._mach_context.settings[section]._settings):
37            meta = command_context._mach_context.settings[section].get_meta(option)
38            desc = meta["description"]
39
40            if short:
41                print("%s.%s -- %s" % (section, option, desc.splitlines()[0]))
42                continue
43
44            if option == "*":
45                option = "<option>"
46
47            if "choices" in meta:
48                value = "{%s}" % ", ".join(meta["choices"])
49            else:
50                value = "<%s>" % types[meta["type_cls"]]
51
52            print(wrapper.fill(desc))
53            print(";%s=%s" % (option, value))
54