@@ -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+
354371def 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 )
0 commit comments