Function Signature
Description
TheExpand() function expands the path to include the home directory if the path is prefixed with ~. If the path doesn’t start with ~, it returns the path as-is.
Parameters
The file path to expand. If it starts with
~, it will be expanded to the full home directory path.Return Values
The expanded path with
~ replaced by the home directory, or the original path if no expansion was neededAn error if:
- The path attempts to expand a user-specific home directory (e.g.,
~user/path) - The home directory cannot be detected (from
Dir())
nil on success.Behavior
Expansion Rules
- Empty path: Returns the path as-is (empty string)
- No tilde prefix: Returns the path unchanged
- Tilde only (
~): Returns the home directory - Tilde with separator (
~/or~\): Replaces~with the home directory and joins the remaining path - User-specific (
~user/): Returns an error (not supported)
Limitations
This is intentional to keep the library simple and avoid platform-specific user lookup logic.Usage Examples
Basic Path Expansion
Paths Without Tilde
Just the Tilde
Windows Paths
Error Handling
User-Specific Home Directory Error
Home Directory Detection Error
Implementation Details
Internally,Expand() uses:
Dir()to get the home directoryfilepath.Join()to properly combine the home directory with the remaining path segments