+class FileMode(Flag):
+ AUTO_DETECT = 0
+ PYTHON36 = 1
+ PYI = 2
+ NO_STRING_NORMALIZATION = 4
+
+ @classmethod
+ def from_configuration(
+ cls, *, py36: bool, pyi: bool, skip_string_normalization: bool
+ ) -> "FileMode":
+ mode = cls.AUTO_DETECT
+ if py36:
+ mode |= cls.PYTHON36
+ if pyi:
+ mode |= cls.PYI
+ if skip_string_normalization:
+ mode |= cls.NO_STRING_NORMALIZATION
+ return mode
+
+
+def read_pyproject_toml(
+ ctx: click.Context, param: click.Parameter, value: Union[str, int, bool, None]
+) -> Optional[str]:
+ """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
+
+ Returns the path to a successfully found and read configuration file, None
+ otherwise.
+ """
+ assert not isinstance(value, (int, bool)), "Invalid parameter type passed"
+ if not value:
+ root = find_project_root(ctx.params.get("src", ()))
+ path = root / "pyproject.toml"
+ if path.is_file():
+ value = str(path)
+ else:
+ return None
+
+ try:
+ pyproject_toml = toml.load(value)
+ config = pyproject_toml.get("tool", {}).get("black", {})
+ except (toml.TomlDecodeError, OSError) as e:
+ raise click.BadOptionUsage(f"Error reading configuration file: {e}", ctx)
+
+ if not config:
+ return None
+
+ if ctx.default_map is None:
+ ctx.default_map = {}
+ ctx.default_map.update( # type: ignore # bad types in .pyi
+ {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
+ )
+ return value
+
+
+@click.command(context_settings=dict(help_option_names=["-h", "--help"]))