Skip to content

Commit

Permalink
Basic validation for paths on command line
Browse files Browse the repository at this point in the history
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
  • Loading branch information
akaihola committed Aug 1, 2021
1 parent 82b5984 commit ea99aaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ def main(argv: List[str] = None) -> int:
"""
if argv is None:
argv = sys.argv[1:]
args, config, config_nondefault = parse_command_line(argv)
try:
args, config, config_nondefault = parse_command_line(argv)
except ArgumentError as exc_info:
sys.exit(exc_info)
logging.basicConfig(level=args.log_level)
if args.log_level == logging.INFO:
formatter = logging.Formatter("%(levelname)s: %(message)s")
Expand Down
12 changes: 11 additions & 1 deletion src/darker/command_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from argparse import ArgumentParser, Namespace
"""Command line parsing for the ``darker`` binary"""

from argparse import Action, ArgumentError, ArgumentParser, Namespace
from typing import Any, List, Optional, Text, Tuple

from darker import help as hlp
Expand Down Expand Up @@ -105,6 +107,14 @@ def parse_command_line(argv: List[str]) -> Tuple[Namespace, DarkerConfig, Darker
# This is used to find out differences between the effective configuration and
# default configuration values, and print them out in verbose mode.
parser_with_original_defaults = make_argument_parser(require_src=True)
invalid_src_paths = [src for src in args.src if src.startswith("-")]
if invalid_src_paths:
invalid_path_reprs = " ".join(repr(path) for path in invalid_src_paths)
raise ArgumentError(
Action(["src"], dest="src", metavar="PATH"),
f"PATH can't begin with a hyphen: {invalid_path_reprs}\n"
"Maybe check your shell quoting?",
)
return (
args,
get_effective_config(args),
Expand Down

0 comments on commit ea99aaf

Please sign in to comment.