Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for secondchoice.cs

(view source code of secondchoice.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Threading;
  5. using System.Timers;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class SecondChoice
  11. 	{
  12. 		static string progver = "1.01";
  13.  
  14.  
  15. 		#region Global Variables
  16.  
  17. 		static bool cancel = false;
  18. 		static int rc = 0;
  19. 		static string choices = "YN";
  20. 		static string defaultchoice = String.Empty;
  21.  
  22. 		#endregion Global Variables
  23.  
  24.  
  25. 		static int Main( string[] args )
  26. 		{
  27. 			#region Capture Control+Break
  28.  
  29. 			// Code to gracefully exit on Ctrl+Break by Alois Kraus
  30. 			// http://www.codeproject.com/Articles/16164/Managed-Application-Shutdown
  31. 			//
  32. 			// Note that this will NOT prevent passing on the Ctrl+Break to the hosting process, the
  33. 			// only purpose for this code is to make this program exit wit errorlevel 0 on Ctrl+Break
  34. 			Console.CancelKeyPress += delegate( object sender, ConsoleCancelEventArgs e )
  35. 			{
  36. 				if ( e.SpecialKey == ConsoleSpecialKey.ControlBreak )
  37. 				{
  38. 					e.Cancel = true;
  39. 					new Thread( delegate( )
  40. 					{
  41. 						Environment.Exit( 0 );
  42. 					} ).Start( );
  43. 				}
  44. 			};
  45.  
  46. 			#endregion Capture Control+Break
  47.  
  48.  
  49. 			#region Local variables
  50.  
  51. 			bool casesensitive = false;
  52. 			bool hidechoices = false;
  53. 			bool usedefault = false;
  54. 			bool usetimeout = false;
  55. 			bool waitingforchoices = false;
  56. 			bool waitingfordefault = false;
  57. 			bool waitingformessage = false;
  58. 			bool waitingfortimeout = false;
  59. 			string message = String.Empty;
  60. 			int timeout = -1;
  61.  
  62. 			#endregion Local variables
  63.  
  64.  
  65. 			#region Command Line Parsing
  66.  
  67. 			foreach ( string arg in args )
  68. 			{
  69. 				if ( arg == "/?" )
  70. 				{
  71. 					return ShowHelp( );
  72. 				}
  73. 			}
  74.  
  75. 			if ( args.Length > 10 )
  76. 			{
  77. 				return ShowHelp( "Too many command line arguments" );
  78. 			}
  79.  
  80. 			foreach ( string arg in args )
  81. 			{
  82. 				switch ( arg.ToUpper( ) )
  83. 				{
  84. 					// Check for command line switches separates by spaces
  85. 					case "/C":
  86. 						if ( choices.ToUpper( ) != "YN" || waitingforchoices )
  87. 						{
  88. 							return ShowHelp( "Duplicate command line switch /C" );
  89. 						}
  90. 						if ( waitingfordefault )
  91. 						{
  92. 							return ShowHelp( "/D is used, but no default choice is specified" );
  93. 						}
  94. 						if ( waitingformessage )
  95. 						{
  96. 							return ShowHelp( "/M is used, but no message is specified" );
  97. 						}
  98. 						if ( waitingfortimeout )
  99. 						{
  100. 							return ShowHelp( "/T is used, but no timeout is specified" );
  101. 						}
  102. 						waitingforchoices = true;
  103. 						break;
  104. 					case "/CS":
  105. 						if ( casesensitive )
  106. 						{
  107. 							return ShowHelp( "Duplicate command line switch /CS" );
  108. 						}
  109. 						casesensitive = true;
  110. 						break;
  111. 					case "/D":
  112. 						if ( usedefault || waitingfordefault )
  113. 						{
  114. 							return ShowHelp( "Duplicate command line switch /D" );
  115. 						}
  116. 						if ( waitingforchoices )
  117. 						{
  118. 							return ShowHelp( "/C is used, but no choices are specified" );
  119. 						}
  120. 						if ( waitingformessage )
  121. 						{
  122. 							return ShowHelp( "/M is used, but no message is specified" );
  123. 						}
  124. 						if ( waitingfortimeout )
  125. 						{
  126. 							return ShowHelp( "/T is used, but no timeout is specified" );
  127. 						}
  128. 						usedefault = true;
  129. 						waitingfordefault = true;
  130. 						break;
  131. 					case "/M":
  132. 						if ( waitingformessage )
  133. 						{
  134. 							return ShowHelp( "Duplicate command line switch /D" );
  135. 						}
  136. 						if ( waitingforchoices )
  137. 						{
  138. 							return ShowHelp( "/C is used, but no choices are specified" );
  139. 						}
  140. 						if ( waitingfordefault )
  141. 						{
  142. 							return ShowHelp( "/D is used, but no default choice is specified" );
  143. 						}
  144. 						if ( waitingfortimeout )
  145. 						{
  146. 							return ShowHelp( "/T is used, but no timeout is specified" );
  147. 						}
  148. 						waitingformessage = true;
  149. 						break;
  150. 					case "/N":
  151. 						if ( hidechoices )
  152. 						{
  153. 							return ShowHelp( "Duplicate command line switch /N" );
  154. 						}
  155. 						hidechoices = true;
  156. 						break;
  157. 					case "/T":
  158. 						if ( usetimeout || waitingfortimeout )
  159. 						{
  160. 							return ShowHelp( "Duplicate command line switch /T" );
  161. 						}
  162. 						if ( waitingforchoices )
  163. 						{
  164. 							return ShowHelp( "/C is used, but no choices are specified" );
  165. 						}
  166. 						if ( waitingfordefault )
  167. 						{
  168. 							return ShowHelp( "/D is used, but no default choice is specified" );
  169. 						}
  170. 						if ( waitingformessage )
  171. 						{
  172. 							return ShowHelp( "/M is used, but no message is specified" );
  173. 						}
  174. 						usetimeout = true;
  175. 						waitingfortimeout = true;
  176. 						break;
  177. 					default:
  178. 						// Check for values belonging to a previous switch
  179. 						if ( waitingforchoices )
  180. 						{
  181. 							choices = arg;
  182. 							waitingforchoices = false;
  183. 						}
  184. 						else if ( waitingfordefault )
  185. 						{
  186. 							defaultchoice = arg;
  187. 							waitingfordefault = false;
  188. 						}
  189. 						else if ( waitingformessage )
  190. 						{
  191. 							message = arg;
  192. 							waitingformessage = false;
  193. 						}
  194. 						else if ( waitingfortimeout )
  195. 						{
  196. 							try
  197. 							{
  198. 								timeout = Convert.ToInt32( arg );
  199. 								waitingfortimeout = false;
  200. 							}
  201. 							catch ( Exception )
  202. 							{
  203. 								return ShowHelp( "Invalid timeout value \"{0}\"", arg );
  204. 							}
  205. 						}
  206. 						else
  207. 						{
  208. 							// Check for arguments with values immediately appended, with colon or equal sign as separator
  209. 							if ( arg.Length > 3 && arg[0] == '/' && "CDMNTcdmnt".IndexOf( arg[1] ) != -1 && ( arg[2] == ':' || arg[2] == '=' ) )
  210. 							{
  211. 								switch ( arg.ToUpper( ).Substring( 0, 2 ) )
  212. 								{
  213. 									case "/C":
  214. 										if ( choices.ToUpper( ) != "YN" || waitingforchoices )
  215. 										{
  216. 											return ShowHelp( "Duplicate command line switch /C" );
  217. 										}
  218. 										choices = arg.Substring( 3 );
  219. 										break;
  220. 									case "/D":
  221. 										if ( usedefault || waitingfordefault )
  222. 										{
  223. 											return ShowHelp( "Duplicate command line switch /D" );
  224. 										}
  225. 										usedefault = true;
  226. 										defaultchoice = arg.Substring( 3 );
  227. 										break;
  228. 									case "/M":
  229. 										if ( waitingformessage )
  230. 										{
  231. 											return ShowHelp( "Duplicate command line switch /D" );
  232. 										}
  233. 										message = arg.Substring( 3 );
  234. 										break;
  235. 									case "/T":
  236. 										if ( usetimeout || waitingfortimeout )
  237. 										{
  238. 											return ShowHelp( "Duplicate command line switch /T" );
  239. 										}
  240. 										try
  241. 										{
  242. 											usetimeout = true;
  243. 											timeout = Convert.ToInt32( arg.Substring( 3 ) );
  244. 										}
  245. 										catch ( Exception )
  246. 										{
  247. 											return ShowHelp( "Invalid timeout value \"{0}\"", arg );
  248. 										}
  249. 										break;
  250. 								}
  251. 							}
  252. 							else
  253. 							{
  254. 								return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  255. 							}
  256. 						}
  257. 						break;
  258. 				}
  259. 			}
  260.  
  261. 			#endregion Command Line Parsing
  262.  
  263.  
  264. 			#region Command Line Validation
  265.  
  266. 			// Timeout requires a default value
  267. 			if ( usetimeout && !usedefault )
  268. 			{
  269. 				return ShowHelp( "If timeout is specified, default value must be specified too" );
  270. 			}
  271.  
  272. 			// If a default is specified, it must be a single character, found in the list of choices
  273. 			if ( usedefault && ( defaultchoice.Length != 1 || choices.IndexOfAny( defaultchoice.ToCharArray( ) ) == -1 ) )
  274. 			{
  275. 				return ShowHelp( "Default specified by /D must be a single character\n\tfrom the list of choices specified by /C" );
  276. 			}
  277.  
  278. 			// Less than 1 choice is useless
  279. 			if ( choices.Length < 2 )
  280. 			{
  281. 				return ShowHelp( "Specify at least 2 choices" );
  282. 			}
  283.  
  284. 			// Check for duplicates in list of choices
  285. 			Regex regex;
  286. 			string pattern = @"([\w ]).*\1";
  287. 			if ( casesensitive )
  288. 			{
  289. 				regex = new Regex( pattern );
  290. 			}
  291. 			else
  292. 			{
  293. 				regex = new Regex( pattern, RegexOptions.IgnoreCase );
  294. 			}
  295. 			if ( regex.IsMatch( choices ) )
  296. 			{
  297. 				return ShowHelp( "All choices specified by /C must be unique" );
  298. 			}
  299. 			if ( !casesensitive )
  300. 			{
  301. 				choices = choices.ToUpper( );
  302. 			}
  303.  
  304. 			if ( usetimeout && timeout > 0 )
  305. 			{
  306. 				// Check if specified timeout is in accepted range
  307. 				if ( timeout < 0 || timeout > 86400 )
  308. 				{
  309. 					return ShowHelp( "Timeout must be in range 0..86400 seconds (0..24h)" );
  310. 				}
  311. 				System.Timers.Timer timer = new System.Timers.Timer( );
  312. 				timer.Elapsed += new ElapsedEventHandler( timer_Elapsed );
  313. 				timer.Interval = timeout * 1000;
  314. 				timer.Start( );
  315. 			}
  316.  
  317. 			#endregion Command Line Validation
  318.  
  319.  
  320. 			#region Show Prompt
  321.  
  322. 			string prompt = String.Empty;
  323. 			if ( hidechoices )
  324. 			{
  325. 				if ( !String.IsNullOrWhiteSpace( message ) )
  326. 				{
  327. 					prompt = String.Format( "{0}: ", message );
  328. 				}
  329. 			}
  330. 			else
  331. 			{
  332. 				prompt = String.Format( "{0} [{1}]: ", message, choices ).TrimStart( );
  333. 			}
  334.  
  335. 			#endregion Show Prompt
  336.  
  337.  
  338. 			#region Handle Response
  339.  
  340. 			Console.Write( prompt );
  341.  
  342. 			// If timeout is zero, return default immediately
  343. 			if ( usetimeout && timeout == 0 )
  344. 			{
  345. 				Console.WriteLine( defaultchoice );
  346. 				rc = choices.IndexOf( defaultchoice[0] ) + 1;
  347. 				return rc;
  348. 			}
  349.  
  350. 			// Tell console to capture Ctrl+C as input
  351. 			Console.TreatControlCAsInput = true;
  352. 			// Wait for response
  353. 			ConsoleKeyInfo response;
  354. 			while ( !cancel )
  355. 			{
  356. 				response = Console.ReadKey( true );
  357. 				string key = response.KeyChar.ToString( );
  358. 				bool ctrlkey = ( ( ( response.Modifiers & ConsoleModifiers.Control ) != 0 ) && ( ( response.Modifiers & ConsoleModifiers.Alt ) == 0 ) && ( ( response.Modifiers & ConsoleModifiers.Shift ) == 0 ) );
  359. 				bool shiftkey = ( ( ( response.Modifiers & ConsoleModifiers.Shift ) != 0 ) && ( ( response.Modifiers & ConsoleModifiers.Alt ) == 0 ) && ( ( response.Modifiers & ConsoleModifiers.Control ) == 0 ) );
  360. 				bool nomodifiers = ( response.Modifiers == 0 );
  361. 				bool escapekey = ( ( response.Key == ConsoleKey.Escape ) && nomodifiers );
  362. 				bool enterkey = ( ( response.Key == ConsoleKey.Enter ) && nomodifiers );
  363. 				// Ctrl+C
  364. 				if ( ctrlkey && ( key == ( (char) 3 ).ToString( ) ) )
  365. 				{
  366. 					rc = 0;
  367. 					cancel = true;
  368. 					break;
  369. 				}
  370. 				// Escape
  371. 				if ( escapekey )
  372. 				{
  373. 					rc = 0;
  374. 					cancel = true;
  375. 					break;
  376. 				}
  377. 				// Case sensitive?
  378. 				if ( shiftkey || !casesensitive )
  379. 				{
  380. 					key = key.ToUpper( );
  381. 				}
  382. 				// Default on Enter
  383. 				if ( enterkey && usedefault )
  384. 				{
  385. 					rc = choices.IndexOf( defaultchoice[0] ) + 1;
  386. 					if ( rc > 0 )
  387. 					{
  388. 						Console.WriteLine( defaultchoice );
  389. 						break;
  390. 					}
  391. 				}
  392. 				// Check if valid key pressed
  393. 				rc = choices.IndexOf( key[0] ) + 1;
  394. 				if ( rc > 0 )
  395. 				{
  396. 					Console.WriteLine( key );
  397. 					break;
  398. 				}
  399. 			}
  400.  
  401. 			#endregion Handle Response
  402.  
  403.  
  404. 			return rc;
  405. 		}
  406.  
  407.  
  408. 		static int ShowHelp( params string[] errmsg )
  409. 		{
  410. 			#region Help Text
  411.  
  412. 			/*
  413. 			SecondChoice.exe,  Version 1.01
  414. 			Alternative to Windows' native CHOICE command.
  415. 			Like CHOICE, SecondChoice allows users to select one item from
  416. 			a list of choices and returns the index of the selected choice.
  417. 			Unlike CHOICE, SecondChoice also returns the default value on
  418. 			pressing the Enter key (besides after timeout expiration).
  419. 			And unlike CHOICE, SecondChoice accepts a space in the list of
  420. 			choices if that list is in doublequotes.
  421.  
  422. 			Usage:     SECONDCHOICE  [ options ]
  423.  
  424. 			Options:   /C choices    Specifies the list of Choices to be created.
  425.                                      Default list is "YN".
  426. 			           /CS           Enables Case-Sensitive choices to be selected.
  427. 			                         By default, the utility is case-insensitive.
  428. 			           /D default    Specifies the Default choice after the timeout
  429. 			                         expires or after pressing Enter.
  430. 			                         Character must be in the set of choices specified
  431. 			                         by /C option.
  432. 			           /M message    Specifies the Message to be displayed before
  433. 			                         the prompt. If not specified, the utility
  434. 			                         displays only a prompt.
  435. 			           /N            Hides the list of choices in the prompt.
  436. 			                         The message before the prompt is displayed
  437. 			                         and the choices are still enabled.
  438. 			           /T timeout    The number of seconds to pause before a default
  439. 			                         choice is made. Acceptable values are from 0 to
  440. 			                         86400 (24 hours). If 0 is specified, there will
  441. 			                         be no pause and the default choice is selected.
  442.  
  443. 			 Notes:    The ERRORLEVEL environment variable is set to the index of the
  444. 			           key that was selected from the set of choices. The first choice
  445. 			           listed returns a value of 1, the second a value of 2, and so on.
  446. 			           Invalid choices are ignored.
  447. 			           If SecondChoice detects an error, it returns ERRORLEVEL 255, if the
  448. 			           user presses ESC, CTRL+C or CTRL+BREAK, the ERRORLEVEL will be 0.
  449. 			           Pressing CTRL+BREAK will also be passed on to the hosting process.
  450. 			           Besides spaces between switches and values, colons or equal signs
  451. 			           are accepted too, e.g. /C choices or /C:choices or /C=choices are
  452. 			           all accepted.
  453. 			           SecondChoice also accepts a space as a valid choice; make sure you
  454. 			           place the list of choices in doublequotes if you want to use a space
  455. 			           in the list of choices, e.g. /C " ABC" or /C:" ABC" or /C=" ABC".
  456.  
  457. 			Examples:  SECONDCHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
  458. 			           SECONDCHOICE /T=10 /C=YNC /D=C /M="Y=yes, N=no, C or timeout=cancel"
  459. 			           SECONDCHOICE /C YN /N /D Y /M "Press Enter or Y for Yes, or N for No"
  460. 			           SECONDCHOICE /C:AB /M:"Select A for option 1 and B for option 2."
  461. 			           SECONDCHOICE /C 12 /N /M "Select 1 for option 1 and 2 for option 2."
  462.  
  463. 			Credits:   Code to gracefully exit on Ctrl+Break by Alois Kraus
  464. 			           http://codeproject.com/Articles/16164/Managed-Application-Shutdown
  465.  
  466. 			Written by Rob van der Woude
  467. 			http://www.robvanderwoude.com
  468. 			*/
  469.  
  470. 			if ( errmsg.Length > 0 )
  471. 			{
  472. 				List<string> errargs = new List<string>( errmsg );
  473. 				errargs.RemoveAt( 0 );
  474. 				Console.Error.WriteLine( );
  475. 				Console.ForegroundColor = ConsoleColor.Red;
  476. 				Console.Error.Write( "ERROR:\t" );
  477. 				Console.ForegroundColor = ConsoleColor.White;
  478. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  479. 				Console.ResetColor( );
  480. 			}
  481.  
  482. 			Console.Error.WriteLine( );
  483.  
  484. 			Console.Error.WriteLine( "SecondChoice.exe,  Version {0}", progver );
  485.  
  486. 			Console.Error.WriteLine( "Alternative to Windows' native CHOICE command." );
  487.  
  488. 			Console.Error.WriteLine( "Like CHOICE, SecondChoice allows users to select one item from" );
  489.  
  490. 			Console.Error.WriteLine( "a list of choices and returns the index of the selected choice." );
  491.  
  492. 			Console.Error.WriteLine( "Unlike CHOICE, SecondChoice also returns the default value on" );
  493.  
  494. 			Console.Error.WriteLine( "pressing the Enter key (besides after timeout expiration)." );
  495.  
  496. 			Console.Error.WriteLine( "And unlike CHOICE, SecondChoice accepts a space in the list of" );
  497.  
  498. 			Console.Error.WriteLine( "choices if that list is in doublequotes." );
  499.  
  500. 			Console.Error.WriteLine( );
  501.  
  502. 			Console.Error.Write( "Usage:     " );
  503. 			Console.ForegroundColor = ConsoleColor.White;
  504. 			Console.Error.WriteLine( "SECONDCHOICE  [ options ]" );
  505. 			Console.ResetColor( );
  506.  
  507. 			Console.Error.WriteLine( );
  508.  
  509. 			Console.Error.Write( "Options:   " );
  510. 			Console.ForegroundColor = ConsoleColor.White;
  511. 			Console.Error.Write( "/C choices" );
  512. 			Console.ResetColor( );
  513. 			Console.Error.Write( "    Specifies the list of " );
  514. 			Console.ResetColor( );
  515. 			Console.ForegroundColor = ConsoleColor.White;
  516. 			Console.Error.Write( "C" );
  517. 			Console.ResetColor( );
  518. 			Console.Error.WriteLine( "hoices to be created." );
  519.  
  520. 			Console.Error.WriteLine( "                         Default list is \"YN\"." );
  521.  
  522. 			Console.ForegroundColor = ConsoleColor.White;
  523. 			Console.Error.Write( "           /CS" );
  524. 			Console.ResetColor( );
  525. 			Console.Error.Write( "           Enables " );
  526. 			Console.ForegroundColor = ConsoleColor.White;
  527. 			Console.Error.Write( "C" );
  528. 			Console.ResetColor( );
  529. 			Console.Error.Write( "ase-" );
  530. 			Console.ForegroundColor = ConsoleColor.White;
  531. 			Console.Error.Write( "S" );
  532. 			Console.ResetColor( );
  533. 			Console.Error.WriteLine( "ensitive choices to be selected." );
  534.  
  535. 			Console.Error.WriteLine( "                         By default, the utility is case-insensitive." );
  536.  
  537. 			Console.ForegroundColor = ConsoleColor.White;
  538. 			Console.Error.Write( "           /D default" );
  539. 			Console.ResetColor( );
  540. 			Console.Error.Write( "    Specifies the " );
  541. 			Console.ForegroundColor = ConsoleColor.White;
  542. 			Console.Error.Write( "D" );
  543. 			Console.ResetColor( );
  544. 			Console.Error.WriteLine( "efault choice after the timeout" );
  545.  
  546. 			Console.Error.WriteLine( "                         expires or after pressing Enter." );
  547.  
  548. 			Console.Error.WriteLine( "                         Character must be in the set of choices specified" );
  549.  
  550. 			Console.Error.Write( "                         by " );
  551. 			Console.ForegroundColor = ConsoleColor.White;
  552. 			Console.Error.Write( "/C" );
  553. 			Console.ResetColor( );
  554. 			Console.Error.WriteLine( " option." );
  555.  
  556. 			Console.ForegroundColor = ConsoleColor.White;
  557. 			Console.Error.Write( "           /M message" );
  558. 			Console.ResetColor( );
  559. 			Console.Error.Write( "    Specifies the " );
  560. 			Console.ForegroundColor = ConsoleColor.White;
  561. 			Console.Error.Write( "M" );
  562. 			Console.ResetColor( );
  563. 			Console.Error.WriteLine( "essage to be displayed before" );
  564.  
  565. 			Console.Error.WriteLine( "                         the prompt. If not specified, the utility" );
  566.  
  567. 			Console.Error.WriteLine( "                         displays only a prompt." );
  568.  
  569. 			Console.ForegroundColor = ConsoleColor.White;
  570. 			Console.Error.Write( "           /N" );
  571. 			Console.ResetColor( );
  572. 			Console.Error.WriteLine( "            Hides the list of choices in the prompt." );
  573.  
  574. 			Console.Error.WriteLine( "                         The message before the prompt is displayed" );
  575.  
  576. 			Console.Error.WriteLine( "                         and the choices are still enabled." );
  577.  
  578. 			Console.ForegroundColor = ConsoleColor.White;
  579. 			Console.Error.Write( "           /T timeout" );
  580. 			Console.ResetColor( );
  581. 			Console.Error.WriteLine( "    The number of seconds to pause before a default" );
  582.  
  583. 			Console.Error.WriteLine( "                         choice is made. Acceptable values are from 0 to" );
  584.  
  585. 			Console.Error.WriteLine( "                         86400 (24 hours). If 0 is specified, there will" );
  586.  
  587. 			Console.Error.WriteLine( "                         be no pause and the default choice is selected." );
  588.  
  589. 			Console.Error.WriteLine( );
  590.  
  591. 			Console.Error.WriteLine( "Notes:     The ERRORLEVEL environment variable is set to the index of the" );
  592.  
  593. 			Console.Error.WriteLine( "           key that was selected from the set of choices. The first choice" );
  594.  
  595. 			Console.Error.WriteLine( "           listed returns a value of 1, the second a value of 2, and so on." );
  596.  
  597. 			Console.Error.WriteLine( "           Invalid choices are ignored." );
  598.  
  599. 			Console.Error.WriteLine( "           If SecondChoice detects an error, it returns ERRORLEVEL 255, if the" );
  600.  
  601. 			Console.Error.WriteLine( "           user presses ESC, CTRL+C or CTRL+BREAK, the ERRORLEVEL will be 0." );
  602.  
  603. 			Console.Error.WriteLine( "           Pressing CTRL+BREAK will also be passed on to the hosting process." );
  604.  
  605. 			Console.Error.WriteLine( "           Besides spaces between switches and values, colons or equal signs" );
  606.  
  607. 			Console.Error.Write( "           are accepted too, e.g. " );
  608. 			Console.ForegroundColor = ConsoleColor.White;
  609. 			Console.Error.Write( "/C choices" );
  610. 			Console.ResetColor( );
  611. 			Console.Error.Write( " or " );
  612. 			Console.ForegroundColor = ConsoleColor.White;
  613. 			Console.Error.Write( "/C:choices" );
  614. 			Console.ResetColor( );
  615. 			Console.Error.Write( " or " );
  616. 			Console.ForegroundColor = ConsoleColor.White;
  617. 			Console.Error.Write( "/C=choices" );
  618. 			Console.ResetColor( );
  619. 			Console.Error.WriteLine( " are" );
  620.  
  621. 			Console.Error.WriteLine( "           all accepted." );
  622.  
  623. 			Console.Error.WriteLine( "           SecondChoice also accepts a space as a valid choice; make sure you" );
  624.  
  625. 			Console.Error.WriteLine( "           place the list of choices in doublequotes if you want to use a space" );
  626.  
  627. 			Console.Error.Write( "           in the list of choices, e.g. " );
  628. 			Console.ForegroundColor = ConsoleColor.White;
  629. 			Console.Error.Write( "/C \" ABC\"" );
  630. 			Console.ResetColor( );
  631. 			Console.Error.Write( " or " );
  632. 			Console.ForegroundColor = ConsoleColor.White;
  633. 			Console.Error.Write( "/C:\" ABC\"" );
  634. 			Console.ResetColor( );
  635. 			Console.Error.Write( " or " );
  636. 			Console.ForegroundColor = ConsoleColor.White;
  637. 			Console.Error.Write( "/C=\" ABC\"" );
  638. 			Console.ResetColor( );
  639. 			Console.Error.WriteLine( "." );
  640.  
  641. 			Console.Error.WriteLine( );
  642.  
  643. 			Console.Error.Write( "Examples:  " );
  644. 			Console.ForegroundColor = ConsoleColor.White;
  645. 			Console.Error.WriteLine( "SECONDCHOICE /C YNC /M \"Press Y for Yes, N for No or C for Cancel.\"" );
  646.  
  647. 			Console.Error.WriteLine( "           SECONDCHOICE /T=10 /C=YNC /D=C /M=\"Y=yes, N=no, C or timeout=cancel\"" );
  648.  
  649. 			Console.Error.WriteLine( "           SECONDCHOICE /C YN /N /D Y /M \"Press Enter or Y for Yes, or N for No\"" );
  650.  
  651. 			Console.Error.WriteLine( "           SECONDCHOICE /C:AB /M:\"Select A for option 1 and B for option 2.\"" );
  652.  
  653. 			Console.Error.WriteLine( "           SECONDCHOICE /C 12 /N /M \"Select 1 for option 1 and 2 for option 2.\"" );
  654. 			Console.ResetColor( );
  655.  
  656. 			Console.Error.WriteLine( );
  657.  
  658. 			Console.Error.WriteLine( "Credits:   Code to gracefully exit on Ctrl+Break by Alois Kraus" );
  659.  
  660. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  661. 			Console.Error.WriteLine( "           http://codeproject.com/Articles/16164/Managed-Application-Shutdown" );
  662. 			Console.ResetColor( );
  663.  
  664. 			Console.Error.WriteLine( );
  665.  
  666. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  667.  
  668. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  669.  
  670. 			#endregion Help Text
  671.  
  672. 			return 255;
  673. 		}
  674.  
  675.  
  676. 		#region Event Handlers
  677.  
  678. 		public static void timer_Elapsed( object sender, System.EventArgs e )
  679. 		{
  680. 			Console.WriteLine( defaultchoice );
  681. 			rc = choices.IndexOf( defaultchoice ) + 1;
  682. 			Environment.Exit( rc );
  683. 		}
  684.  
  685. 		#endregion Event Handlers
  686. 	}
  687. }
  688.  

page last uploaded: 2017-04-06, 13:33