Last active
December 16, 2022 14:09
-
-
Save relipse/3bcc2a25d494161eb957174f88767daa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$opts = getopt('lhc:v::', ['convert:','loop','ls', 'help']); | |
$helpstr = <<<EOT | |
Slash will convert strings/paths with / to \ and vice versa. | |
ie. try | |
C:\\Users\\relipse\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PhpStorm\\ch-0\\221.5080.224\\bin\\phpstorm64.exe | |
or | |
/c/path/to/file | |
php {$argv[0]} | |
Options: | |
-c, --convert Convert directly from | |
-l, --loop Loop (Keep prompting and converting) | |
-h, --help Show this help screen. | |
--ls List files in directory | |
EOT; | |
if (isset($opts['help']) || isset($opts['h'])){ | |
die($helpstr); | |
} | |
//ls files (list files in path, if it exists) | |
enum Verbosity: int{ | |
case NONE = 0; | |
case VERBOSE = 1; | |
case VERY_VERBOSE = 2; | |
case SUPER_VERBOSE = 3; | |
} | |
$verbosity = Verbosity::NONE; | |
if (isset($opts['v'])){ | |
$verbosity = match($opts['v']){ | |
false => Verbosity::VERBOSE, | |
'v' => Verbosity::VERY_VERBOSE, | |
'vv' => Verbosity::SUPER_VERBOSE, | |
default => Verbosity::NONE, | |
}; | |
if ($verbosity === Verbosity::NONE){ | |
echo "Invalid verbosity value: {$opts['v']}\n"; | |
exit; | |
}else{ | |
echo "Verbosity Level: ".$verbosity->value." (".$verbosity->name.")\n\n"; | |
} | |
} | |
$ls = match($verbosity){ | |
Verbosity::SUPER_VERBOSE => true, | |
default => isset($opts['ls']), | |
}; | |
$convert = null; | |
if (!empty($opts['convert'])){ | |
$convert = $opts['convert']; | |
} | |
if (!empty($opts['c'])){ | |
$convert = $opts['c']; | |
} | |
if (isset($convert)){ | |
showconvertshow($convert, $ls); | |
exit; | |
} | |
echo 'Welcome to slash which will convert / to \\ and \\ to /. Enter a path:'."\n"; | |
while(true) { | |
$line = readline(); | |
if ($line) { | |
$s = $line; | |
}else{ | |
//echo "Exiting...\n"; | |
exit; | |
} | |
showconvertshow($s, $ls); | |
if (!isset($opts['l']) && !isset($opts['loop'])){ | |
//break if not a loop | |
break; | |
} | |
} | |
function convert(string $s): string{ | |
if (strpos($s, '\\') !== false){ | |
if (strpos($s, ':\\') === 1){ | |
$s = '/'.strtolower($s[0]).substr($s, 1); | |
$s = str_replace(':\\', '\\', $s); | |
} | |
$s = str_replace('\\','/', $s); | |
}else if (strpos($s, '/') !== false){ | |
if (preg_match('#^/([a-z])/#i', $s, $matches)){ | |
$s = $matches[1].':\\'.substr($s, 3); | |
} | |
$s = str_replace('/', '\\', $s); | |
} | |
return $s; | |
} | |
function showconvertshow(string $s, bool $ls): void{ | |
global $verbosity; | |
if ($verbosity->value > 0) { | |
echo "Before: $s\n"; | |
} | |
$s1exists = false; | |
if (file_exists($s)){ | |
$s1exists = true; | |
if ($verbosity->value > 0) { | |
echo "(Path exists)\n"; | |
} | |
if ($ls){ | |
$files = array_diff(scandir($s), array('.', '..')); | |
print_r($files); | |
} | |
}else{ | |
if ($verbosity->value > 0) { | |
echo "(Path does not exist)\n"; | |
} | |
} | |
//echo "Realpath: ".realpath($s)."\n"; | |
if ($verbosity->value > 0) { | |
echo "After: "; | |
} | |
$s2 = convert($s); | |
echo $s2."\n"; | |
if (file_exists($s2)){ | |
if ($verbosity->value > 0) { | |
echo "(Path exists now)\n"; | |
if ($verbosity->value > 1) { | |
echo "Actual Path: " . realpath($s2); | |
} | |
} | |
if (!$s1exists && $ls){ | |
echo "\n"; | |
$files = array_diff(scandir($s2), array('.', '..')); | |
print_r($files); | |
} | |
}else{ | |
if ($verbosity->value > 0) { | |
echo "(Path does not exist)\n"; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment