Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for testcommandlineargs.cs

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

  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class TestCommandLineArgs
  9. 	{
  10. 		static readonly string progver = "1.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			if ( args.Length == 0 || ( args.Length == 1 && args[0] == "/?" ) )
  16. 			{
  17. 				return ShowHelp( );
  18. 			}
  19.  
  20. 			bool founddoublequote = false;
  21. 			int argscount = 0;
  22.  
  23. 			Console.WriteLine( "Command line: {0}", Environment.CommandLine );
  24. 			Console.WriteLine( );
  25. 			Console.WriteLine( "Traditional command line parsing: {0} command line argument{1}{2}", args.Length, ( args.Length == 1 ? "" : "s" ), ( args.Length == 0 ? "" : ":" ) );
  26. 			for ( int i = 0; i < args.Length; i++ )
  27. 			{
  28. 				if ( args[i].IndexOf( "\"" ) > -1 )
  29. 				{
  30. 					founddoublequote = true;
  31. 					Console.Write( "[{0}]\t{1}", i, args[i] );
  32. 					Console.ForegroundColor = ConsoleColor.Red;
  33. 					Console.WriteLine( "\t(contains doublequote)" );
  34. 					Console.ResetColor( );
  35. 				}
  36. 				else
  37. 				{
  38. 					Console.WriteLine( "[{0}]\t{1}", i, args[i] );
  39. 				}
  40. 			}
  41. 			if ( founddoublequote )
  42. 			{
  43. 				Console.WriteLine( );
  44. 				string pattern = @"^(?<quoteexec>\""?)(?<exec>[^\""]+?)\k<quoteexec>";
  45. 				pattern += @"(?:\s+(?<quotearg0>\""?)(?<arg0>[^\""]+?)\k<quotearg0>";
  46. 				pattern += @"(?:\s+(?<quotearg1>\""?)(?<arg1>[^\""]+?)\k<quotearg1>";
  47. 				pattern += @"(?:\s+(?<quotearg2>\""?)(?<arg2>[^\""]+?)\k<quotearg2>";
  48. 				pattern += @"(?:\s+(?<quotearg3>\""?)(?<arg3>[^\""]+?)\k<quotearg3>";
  49. 				pattern += @"(?:\s+(?<quotearg4>\""?)(?<arg4>[^\""]+?)\k<quotearg4>";
  50. 				pattern += @"(?:\s+(?<quotearg5>\""?)(?<arg5>[^\""]+?)\k<quotearg5>";
  51. 				pattern += @"(?:\s+(?<quotearg6>\""?)(?<arg6>[^\""]+?)\k<quotearg6>";
  52. 				pattern += @"(?:\s+(?<quotearg7>\""?)(?<arg7>[^\""]+?)\k<quotearg7>";
  53. 				pattern += @"(?:\s+(?<quotearg8>\""?)(?<arg8>[^\""]+?)\k<quotearg8>";
  54. 				pattern += @"(?:\s+(?<quotearg9>\""?)(?<arg9>[^\""]+?)\k<quotearg9>)?)?)?)?)?)?)?)?)?)?\s*$";
  55. 				Regex regex = new Regex( pattern );
  56. 				if ( regex.IsMatch( Environment.CommandLine ) )
  57. 				{
  58. 					Match match = regex.Match( Environment.CommandLine );
  59. 					Console.WriteLine( );
  60. 					for ( int i = 4; i < 21; i += 2 )
  61. 					{
  62. 						if ( !string.IsNullOrEmpty( match.Groups[i].Value ) )
  63. 						{
  64. 							argscount = i / 2 - 1;
  65. 						}
  66. 					}
  67. 					Console.WriteLine( "Alternative command line parsing: {0} command line argument{1}{2}", argscount, ( argscount == 1 ? "" : "s" ), ( argscount == 0 ? "" : ":" ) );
  68. 					if ( !string.IsNullOrEmpty( match.Groups["arg0"].Value ) )
  69. 					{
  70. 						Console.WriteLine( "[0]\t{0}", match.Groups["arg0"].Value );
  71. 						if ( !string.IsNullOrEmpty( match.Groups["arg1"].Value ) )
  72. 						{
  73. 							Console.WriteLine( "[1]\t{0}", match.Groups["arg1"].Value );
  74. 							if ( !string.IsNullOrEmpty( match.Groups["arg2"].Value ) )
  75. 							{
  76. 								Console.WriteLine( "[2]\t{0}", match.Groups["arg2"].Value );
  77. 								if ( !string.IsNullOrEmpty( match.Groups["arg3"].Value ) )
  78. 								{
  79. 									Console.WriteLine( "[3]\t{0}", match.Groups["arg3"].Value );
  80. 									if ( !string.IsNullOrEmpty( match.Groups["arg4"].Value ) )
  81. 									{
  82. 										Console.WriteLine( "[4]\t{0}", match.Groups["arg4"].Value );
  83. 										if ( !string.IsNullOrEmpty( match.Groups["arg5"].Value ) )
  84. 										{
  85. 											Console.WriteLine( "[5]\t{0}", match.Groups["arg5"].Value );
  86. 											if ( !string.IsNullOrEmpty( match.Groups["arg6"].Value ) )
  87. 											{
  88. 												Console.WriteLine( "[6]\t{0}", match.Groups["arg6"].Value );
  89. 												if ( !string.IsNullOrEmpty( match.Groups["arg7"].Value ) )
  90. 												{
  91. 													Console.WriteLine( "[7]\t{0}", match.Groups["arg7"].Value );
  92. 													if ( !string.IsNullOrEmpty( match.Groups["arg8"].Value ) )
  93. 													{
  94. 														Console.WriteLine( "[8]\t{0}", match.Groups["arg8"].Value );
  95. 														if ( !string.IsNullOrEmpty( match.Groups["arg9"].Value ) )
  96. 														{
  97. 															Console.WriteLine( "[9]\t{0}", match.Groups["arg9"].Value );
  98. 														}
  99. 													}
  100. 												}
  101. 											}
  102. 										}
  103. 									}
  104. 								}
  105. 							}
  106. 						}
  107. 					}
  108. 				}
  109. 			}
  110. 			return argscount;
  111. 		}
  112.  
  113.  
  114. 		#region Error handling
  115.  
  116. 		static int ShowHelp( params string[] errmsg )
  117. 		{
  118. 			#region Error Message
  119.  
  120. 			if ( errmsg.Length > 0 )
  121. 			{
  122. 				List<string> errargs = new List<string>( errmsg );
  123. 				errargs.RemoveAt( 0 );
  124. 				Console.Error.WriteLine( );
  125. 				Console.ForegroundColor = ConsoleColor.Red;
  126. 				Console.Error.Write( "ERROR:\t" );
  127. 				Console.ForegroundColor = ConsoleColor.White;
  128. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  129. 				Console.ResetColor( );
  130. 			}
  131.  
  132. 			#endregion Error Message
  133.  
  134.  
  135. 			#region Help Text
  136.  
  137. 			/*
  138. 			TestCommandLineArgs.exe,  Version 1.00
  139. 			Test C# command line parsing
  140.  
  141. 			Usage:   TestCommandLineArgs.exe  [ up to 9 command line arguments ]
  142.  
  143. 			Returns: Displays each command line argument on a separate line, first
  144. 			         trying traditional method (parsing string[] args), and if that fails
  145. 			         an alternative method using a regex on Envrionment.CommandLine
  146. 			         Traditional parsing is considered unsuccessful if one of the arguments
  147. 			         contains at least one doublequote, not counting the enclosing quotes.
  148. 			         Return code -1 if no arguments, 0 if traditional parsing succeeded,
  149. 			         otherwise the number of arguments found by the alternative parser.
  150.  
  151. 			Example:
  152.  
  153. 			TestCommandLineArgs.exe "1 2 3" 4 "D:\" "C:\windows" 12 XYZ
  154.  
  155. 			Output:
  156.  
  157. 			Command line: "D:\TestCommandLineArgs.exe" "1 2 3" 4 "D:\" "C:\windows" 12 XYZ
  158.  
  159. 			Traditional command line parsing: 3 command line arguments:
  160. 			[0]     1 2 3
  161. 			[1]     4
  162. 			[2]     D:" C:\windows 12 XYZ       (contains doublequote)
  163.  
  164.  
  165. 			Alternative command line parsing: 6 command line arguments:
  166. 			[0]     1 2 3
  167. 			[1]     4
  168. 			[2]     D:\
  169. 			[3]     C:\windows
  170. 			[4]     12
  171. 			[5]     XYZ
  172.  
  173. 			Written by Rob van der Woude
  174. 			https://www.robvanderwoude.com
  175. 			*/
  176.  
  177. 			#endregion Help Text
  178.  
  179.  
  180. 			#region Display Help Text
  181.  
  182. 			Console.Error.WriteLine( );
  183.  
  184. 			Console.Error.WriteLine( "TestCommandLineArgs.exe,  Version {0}", progver );
  185.  
  186. 			Console.Error.WriteLine( "Test C# command line parsing" );
  187.  
  188. 			Console.Error.WriteLine( );
  189.  
  190. 			Console.Error.Write( "Usage:   " );
  191. 			Console.ForegroundColor = ConsoleColor.White;
  192. 			Console.Error.WriteLine( "TestCommandLineArgs.exe  [ up to 9 command line arguments ]" );
  193. 			Console.ResetColor( );
  194.  
  195. 			Console.Error.WriteLine( );
  196.  
  197. 			Console.Error.WriteLine( "Returns: Displays each command line argument on a separate line, first" );
  198.  
  199. 			Console.Error.WriteLine( "         trying traditional method (parsing string[] args), and if that fails" );
  200.  
  201. 			Console.Error.WriteLine( "         an alternative method using a regex on Envrionment.CommandLine" );
  202.  
  203. 			Console.Error.WriteLine( "         Traditional parsing is considered unsuccessful if one of the arguments" );
  204.  
  205. 			Console.Error.WriteLine( "         contains at least one doublequote, not counting the enclosing quotes." );
  206.  
  207. 			Console.Error.WriteLine( "         Return code -1 if no arguments, 0 if traditional parsing succeeded," );
  208.  
  209. 			Console.Error.WriteLine( "         otherwise the number of arguments found by the alternative parser." );
  210.  
  211. 			Console.Error.WriteLine( );
  212.  
  213. 			Console.Error.WriteLine( "Example:" );
  214.  
  215. 			Console.Error.WriteLine( );
  216.  
  217. 			Console.ForegroundColor = ConsoleColor.White;
  218. 			Console.Error.WriteLine( "TestCommandLineArgs.exe \"1 2 3\" 4 \"D:\\\" \"C:\\windows\" 12 XYZ" );
  219. 			Console.ResetColor( );
  220.  
  221. 			Console.Error.WriteLine( );
  222.  
  223. 			Console.Error.WriteLine( "Output:" );
  224.  
  225. 			Console.Error.WriteLine( );
  226.  
  227. 			Console.ForegroundColor = ConsoleColor.White;
  228. 			Console.Error.WriteLine( "Command line: \"D:\\TestCommandLineArgs.exe\" \"1 2 3\" 4 \"D:\\\" \"C:\\windows\" 12 XYZ" );
  229.  
  230. 			Console.Error.WriteLine( );
  231.  
  232. 			Console.Error.WriteLine( "Traditional command line parsing: 3 command line arguments:" );
  233.  
  234. 			Console.Error.WriteLine( "[0]     1 2 3" );
  235.  
  236. 			Console.Error.WriteLine( "[1]     4" );
  237.  
  238. 			Console.Error.Write( "[2]     D:\" C:\\windows 12 XYZ       " );
  239. 			Console.ForegroundColor = ConsoleColor.Red;
  240. 			Console.Error.WriteLine( "(contains doublequote)" );
  241. 			Console.ForegroundColor = ConsoleColor.White;
  242.  
  243. 			Console.Error.WriteLine( );
  244.  
  245. 			Console.Error.WriteLine( );
  246.  
  247. 			Console.Error.WriteLine( "Alternative command line parsing: 6 command line arguments:" );
  248.  
  249. 			Console.Error.WriteLine( "[0]     1 2 3" );
  250.  
  251. 			Console.Error.WriteLine( "[1]     4" );
  252.  
  253. 			Console.Error.WriteLine( "[2]     D:\\" );
  254.  
  255. 			Console.Error.WriteLine( "[3]     C:\\windows" );
  256.  
  257. 			Console.Error.WriteLine( "[4]     12" );
  258.  
  259. 			Console.Error.WriteLine( "[5]     XYZ" );
  260. 			Console.ResetColor( );
  261.  
  262. 			Console.Error.WriteLine( );
  263.  
  264. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  265.  
  266. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  267.  
  268. 			#endregion Display Help Text
  269.  
  270.  
  271. 			return -1;
  272. 		}
  273.  
  274. 		#endregion Error handling
  275. 	}
  276. }
  277.  

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