Apply Black formatting only in regions changed since last commit
—
Command line argument parsing, configuration management, and option validation for the darker command-line tool.
Functions for creating and configuring the command-line argument parser.
def make_argument_parser(require_src: bool) -> ArgumentParser:
"""
Create the argument parser object for darker command-line interface.
Parameters:
- require_src: True to require at least one path as positional argument
Returns:
Configured ArgumentParser instance with all CLI options
"""Parse command-line arguments and merge with configuration file settings.
def parse_command_line(
argv: Optional[List[str]]
) -> Tuple[Namespace, DarkerConfig, DarkerConfig]:
"""
Parse command line arguments with configuration file defaults.
Reads configuration from pyproject.toml [tool.darker] section,
then applies command-line overrides, and returns both the parsed
arguments and the effective configuration.
Parameters:
- argv: Command line arguments list (typically sys.argv[1:], None for sys.argv[1:])
Returns:
Tuple of:
- Namespace: Parsed command line arguments
- DarkerConfig: Effective configuration combining defaults, file, and CLI
- DarkerConfig: Configuration options that differ from defaults
"""Functions for validating and processing configuration options.
def show_config_deprecations(config: DarkerConfig) -> None:
"""
Display deprecation warnings for outdated configuration options.
Parameters:
- config: Configuration dictionary to validate
"""# Source paths
darker [paths...] # Files/directories to process
# Revision control
--revision REV # Git revision to compare against
-r REV # Short form of --revision
# Output modes
--diff # Show diff instead of writing files
--stdout # Output to stdout instead of files
-d # Short form of --stdout
--check # Exit with error if changes needed
# Formatting options
--isort # Apply isort import sorting
-i # Short form of --isort
--flynt # Apply flynt f-string conversion
-f # Short form of --flynt
# Formatter selection
--formatter NAME # Choose formatter (black, ruff, none)
# Configuration
--config PATH # Path to configuration file
-c PATH # Short form of --config
# Output formatting
--color # Use colored output
--no-color # Disable colored output
# Verbosity
--verbose # Increase logging verbosity
-v # Short form of --verbose
--quiet # Decrease logging verbosity
-q # Short form of --quiet
# Black compatibility options
--line-length LENGTH # Line length for formatting
-l LENGTH # Short form of --line-length
--target-version VER # Python version target
--skip-string-normalization # Don't normalize string quotes
--skip-magic-trailing-comma # Don't use trailing commas
--preview # Enable preview style featuresfrom darker.command_line import parse_command_line
# Parse minimal arguments
args = parse_command_line(["src/", "--diff", "--check"])
print(f"Paths: {args.src}")
print(f"Show diff: {args.diff}")
print(f"Check mode: {args.check}")
# Parse with formatting options
args = parse_command_line([
"mymodule.py",
"--isort",
"--flynt",
"--formatter", "black",
"--line-length", "88",
"--color"
])from darker.command_line import parse_command_line
# Using custom config file
args = parse_command_line([
"src/",
"--config", "my-darker.toml",
"--revision", "HEAD~2"
], config_filenames=["my-darker.toml", "pyproject.toml"])
# Access parsed configuration
formatter_name = getattr(args, 'formatter', 'black')
use_isort = args.isort
line_length = getattr(args, 'line_length', 88)from darker.command_line import make_argument_parser
# Create parser that requires source paths
parser = make_argument_parser(require_src=True)
args = parser.parse_args(["src/module.py", "--diff"])
# Create parser that allows no source paths (for piped input)
parser = make_argument_parser(require_src=False)
args = parser.parse_args(["--stdin-filename", "module.py"])[tool.darker]
src = ["src", "tests"]
revision = "origin/main..."
diff = true
check = false
isort = true
flynt = false
formatter = "black"
line_length = 88
target_version = "py39"
color = truefrom darker.command_line import parse_command_line
from darker.config import DarkerConfig
# Parse with config file integration
args = parse_command_line(["--diff"])
# Configuration is merged into args namespace
config: DarkerConfig = {
'src': args.src,
'diff': args.diff,
'check': args.check,
'isort': args.isort,
'flynt': args.flynt,
'formatter': getattr(args, 'formatter', 'black'),
'line_length': getattr(args, 'line_length', 88),
}Install with Tessl CLI
npx tessl i tessl/pypi-darker@3.0.1