Skip to content

Commit f341425

Browse files
committed
improved error messaging when dot ran with no file path, but pattern
1 parent 8050841 commit f341425

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

src/dotcat/dotcat.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,23 @@ def parse_args(args: List[str]) -> Tuple[str, str, str, bool]:
351351
)
352352

353353

354+
def is_likely_dot_path(arg: str) -> bool:
355+
"""
356+
Determines if an argument is likely a dot path rather than a file path.
357+
358+
Args:
359+
arg: The argument to check.
360+
361+
Returns:
362+
True if the argument is likely a dot path, False otherwise.
363+
"""
364+
# If it contains dots and doesn't look like a file path
365+
if "." in arg and not os.path.exists(arg):
366+
# Check if it has multiple segments separated by dots
367+
return len(arg.split(".")) > 1
368+
return False
369+
370+
354371
def run(args: List[str] = None) -> None:
355372
"""
356373
Processes the command-line arguments and prints the value from the structured data file.
@@ -365,20 +382,42 @@ def run(args: List[str] = None) -> None:
365382
check_install()
366383
return
367384

368-
# Check if lookup_chain is provided and handle missing dot pattern
369-
if lookup_chain is None:
370-
# Check if the file exists
371-
try:
372-
if os.path.exists(filename):
373-
# File exists, but dot pattern is missing
385+
# Special case: If we have only one argument and it looks like a dot path,
386+
# treat it as the dot path rather than the file
387+
if filename is not None and lookup_chain is None and len(args) == 1:
388+
if is_likely_dot_path(filename):
389+
# Swap the arguments
390+
lookup_chain = filename
391+
filename = None
392+
# Now filename is None and lookup_chain is not None
393+
394+
# Handle cases where one of the required arguments is missing
395+
if lookup_chain is None or filename is None:
396+
if filename is not None and lookup_chain is None:
397+
# Case 1: File is provided but dot pattern is missing
398+
try:
399+
if os.path.exists(filename):
400+
# File exists, but dot pattern is missing
401+
print(
402+
f"Dot path required. Which value do you want me to look up in {filename}?"
403+
)
404+
print(f"\n$dotcat {filename} <pattern>")
405+
sys.exit(2) # Invalid usage
406+
except Exception:
407+
# If there's any error checking the file, fall back to general usage message
408+
pass
409+
elif filename is None and lookup_chain is not None:
410+
# Case 2: Dot pattern is provided but file is missing
411+
# Check if the argument looks like a dot path (contains dots)
412+
if "." in lookup_chain:
413+
# It looks like a dot path, so assume the file is missing
374414
print(
375-
f"Dot path required. Which value do you want me to look up in {filename}?"
415+
f"File path required. Which file contains the value at {lookup_chain}?"
376416
)
377-
print(f"\n$dotcat {filename} <pattern>")
417+
print(f"\n$dotcat <file> {lookup_chain}")
378418
sys.exit(2) # Invalid usage
379-
except Exception:
380-
# If there's any error checking the file, fall back to general usage message
381-
pass
419+
# Otherwise, it might be a file without an extension or something else,
420+
# so fall back to the general usage message
382421

383422
# General usage message for other cases
384423
print(USAGE)

tests/test_exec.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ def test_file_without_dot_pattern():
130130
assert "<pattern>" in actual_output
131131
assert pytest_wrapped_e.type == SystemExit
132132
assert pytest_wrapped_e.value.code == 2
133+
134+
135+
def test_dot_path_without_file():
136+
test_args = ["python.editor.tabSize"]
137+
captured_output = StringIO()
138+
sys.stdout = captured_output
139+
with pytest.raises(SystemExit) as pytest_wrapped_e:
140+
run(test_args)
141+
sys.stdout = sys.__stdout__
142+
actual_output = remove_ansi_escape_sequences(captured_output.getvalue().strip())
143+
# Check that the output contains the specific dot path and guidance
144+
assert "File path required" in actual_output
145+
assert "python.editor.tabSize" in actual_output
146+
assert pytest_wrapped_e.type == SystemExit
147+
assert pytest_wrapped_e.value.code == 2

0 commit comments

Comments
 (0)