alias.php

  1. <?php
  2.  
  3. define('USER_PATH', 'C:/Users/Mat/Shortcuts');
  4.  
  5. // Grab all the CLI args except the script just run.
  6. $args = array_slice($_SERVER['argv'], 1);
  7.  
  8. // Loop over the args, fishing out any starting with a hyphen.
  9. $alias_args = array();
  10. do {
  11. $alias_name = array_shift($args);
  12.  
  13. // Does it start with a hyphen?
  14. if ((bool) $alias_name AND $alias_name[0] == '-') {
  15. $alias_args[] = $alias_name;
  16. }
  17. } while ($alias_name[0] == '-');
  18.  
  19. // Use the remaining args as the alias command.
  20. $command = "@ECHO OFF\r\n".implode(' ', $args);
  21.  
  22. // Build the path we'll save to.
  23. $alias_path = realpath(USER_PATH).'/'.$alias_name.'.cmd';
  24.  
  25. // Check if the file exists.
  26. $alias_exists = file_exists($alias_path);
  27.  
  28. // Warn unless they specified an override.
  29. if ($alias_exists AND ! in_array('-f', $alias_args)) {
  30. echo 'Alias already exists. Use -f to overwrite.', "\n";
  31. } else {
  32. // Save the alias!
  33. file_put_contents($alias_path, $command);
  34.  
  35. // Let them know what action was taken.
  36. if ($alias_exists)
  37. echo 'Updated alias', "\n";
  38. else
  39. echo 'Created new alias', "\n";
  40. }
  41.