Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for choice.pl

(view source code of choice.pl as plain text)

  1. #! perl
  2.  
  3. $syntax = "\nChoice.pl,  Version 1.00\n";
  4. $syntax = $syntax."Perl port of MS-DOS' CHOICE command\n\n";
  5. # $syntax = $syntax."Usage:  CHOICE  [/C[:]choices]  [/N]  [/S]  [/T[:]c,nn]  [text]\n\n";
  6. $syntax = $syntax."Usage:  CHOICE  [/C[:]choices]  [/N]  [/S]  [text]\n\n";
  7. $syntax = $syntax."Where:  /C[:]choices  Specifies allowable keys. Default is YN\n";
  8. $syntax = $syntax."        /N            Do not display choices and ? at end of prompt string.\n";
  9. $syntax = $syntax."        /S            Treat choice keys as case sensitive.\n";
  10. # $syntax = $syntax."        /T[:]c,nn     Default choice to c after nn seconds\n";
  11. $syntax = $syntax."        text          Prompt string to display\n\n";
  12. $syntax = $syntax."ERRORLEVEL is set to offset of key user presses in choices.\n\n";
  13. $syntax = $syntax."Note:   Unlike its MS-DOS counterpart, this Perl port requires pressing the\n";
  14. $syntax = $syntax."        \"Enter\" key after the key of choice.\n";
  15. $syntax = $syntax."        The MS-DOS counterpart's time-out switch isn't implemented in this Perl\n";
  16. $syntax = $syntax."        port.\n\n";
  17.  
  18. $syntax = $syntax."Written by Rob van der Woude\nhttp://www.robvanderwoude.com\n";
  19.  
  20. # Set default values
  21. $case    = 0;
  22. $choices = "YN";
  23. $default = "";
  24. $dispch  = 1;
  25. $help    = 0;
  26. $text    = "";
  27. $timeout = "";
  28.  
  29. # Parse command line arguments
  30. if ( @ARGV[0] ) {
  31. 	foreach ( @ARGV ) {
  32. 		SELECT: {
  33. 			if ( $_ =~ m/^\/C:?(\w+)$/i ) {
  34. 				$choices = $1;
  35. 				last SELECT;
  36. 			}
  37. 			if ( uc( $_ ) eq "/N" ) {
  38. 				$dispch = 0;
  39. 				last SELECT;
  40. 			}
  41. 			if ( uc( $_ ) eq "/S" ) {
  42. 				$case = 1;
  43. 				last SELECT;
  44. 			}
  45. 			if ( $_ =~ m/^\/T:?(\w+),(\d+)$/i ) {
  46. 				$default = $1;
  47. 				$timeout = $2;
  48. 				last SELECT;
  49. 			}
  50. 			if ( $_ =~ m/^[^\/]+$/ ) {
  51. 				$text = "$text $_";
  52. 				last SELECT;
  53. 			}
  54. 			$help = 1;
  55. 		}
  56. 	}
  57. }
  58.  
  59. if ( $help == 1 ) {
  60. 	die $syntax;
  61. }
  62.  
  63. if ( substr( $text, 0, 1 ) == " " ) {
  64. 	$text = substr( $text, 1 );
  65. }
  66.  
  67. $prompt = $text;
  68. if ( $dispch == 1 ) {
  69. 	$prompt = $prompt."? [$choices] ";
  70. }
  71.  
  72. if ( $case == 1 ) {
  73. 	do {
  74. 		print $prompt;
  75. 		$key = getc( STDIN );
  76. 		print "\n";
  77. 	} until ( $key =~ m/[$choices]/ );
  78. 	$pos = 1 - $[ + index( $choices, $key );
  79. } else {
  80. 	do {
  81. 		print $prompt;
  82. 		$key = getc( STDIN );
  83. 		print "\n";
  84. 	} until ( $key =~ m/[$choices]/i );
  85. 	$pos = 1 - $[ + index( uc( $choices ), uc( $key ) );
  86. }
  87.  
  88. exit $pos
  89.  

page last modified: 2024-04-16; loaded in 0.0211 seconds