Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for trim.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	internal class Trim
  9. 	{
  10. 		static readonly string progver = "1.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			int argumentscount = args.Length;
  16. 			if ( Console.IsInputRedirected )
  17. 			{
  18. 				argumentscount++;
  19. 			}
  20. 			if ( argumentscount != 1 || args.Contains( "/?" ) )
  21. 			{
  22. 				return ShowHelp( );
  23. 			}
  24.  
  25. 			string input = string.Empty;
  26. 			if ( Console.IsInputRedirected )
  27. 			{
  28. 				input = Console.In.ReadToEnd( );
  29. 			}
  30. 			else if ( File.Exists( args[0] ) )
  31. 			{
  32. 				input = File.ReadAllText( args[0] );
  33. 			}
  34. 			else
  35. 			{
  36. 				input = args[0];
  37. 			}
  38. 			string output = input.Trim( );
  39. 			Console.Write( output );
  40. 			return 0;
  41. 		}
  42.  
  43.  
  44. 		public static int ShowHelp( string errorMessage = "" )
  45. 		{
  46. 			#region Help Text
  47.  
  48. 			/*
  49. 			Trim,  Version 1.00
  50. 			Trim leading and trailing whitespace (including linefeeds) from string
  51.  
  52. 			Usage:  TRIM  textfile
  53.  
  54. 			   or:  TRIM  text
  55.  
  56. 			   or:  some_program | TRIM
  57.  
  58. 			Where:  textfile      file containing text to be trimmed
  59. 			        text          string to be trimmed
  60. 			        some_program  program whose output is to be trimmed
  61.  
  62. 			Note:   Exit code ("errorlevel") -1 in case of errors, otherwise 0.
  63.  
  64. 			Written by Rob van der Woude
  65. 			https://www.robvanderwoude.com
  66. 			*/
  67.  
  68. 			#endregion Help Text
  69.  
  70.  
  71. 			#region Error Message
  72.  
  73. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  74. 			{
  75. 				Console.Error.WriteLine( );
  76. 				Console.ForegroundColor = ConsoleColor.Red;
  77. 				Console.Error.Write( "ERROR: " );
  78. 				Console.ForegroundColor = ConsoleColor.White;
  79. 				Console.Error.WriteLine( errorMessage );
  80. 				Console.ResetColor( );
  81. 			}
  82.  
  83. 			#endregion Error Message
  84.  
  85.  
  86. 			#region Display Help
  87.  
  88. 			Console.Error.WriteLine( );
  89.  
  90. 			Console.Error.WriteLine( "Trim,  Version {0}", progver );
  91.  
  92. 			Console.Error.WriteLine( "Trim leading and trailing whitespace (including linefeeds) from string" );
  93.  
  94. 			Console.Error.WriteLine( );
  95.  
  96. 			Console.Error.Write( "Usage:  " );
  97. 			Console.ForegroundColor = ConsoleColor.White;
  98. 			Console.Error.WriteLine( "TRIM  textfile" );
  99. 			Console.ResetColor( );
  100.  
  101. 			Console.Error.WriteLine( );
  102.  
  103. 			Console.Error.Write( "   or:  " );
  104. 			Console.ForegroundColor = ConsoleColor.White;
  105. 			Console.Error.WriteLine( "TRIM  text" );
  106. 			Console.ResetColor( );
  107.  
  108. 			Console.Error.WriteLine( );
  109.  
  110. 			Console.Error.Write( "   or:  " );
  111. 			Console.ForegroundColor = ConsoleColor.White;
  112. 			Console.Error.WriteLine( "some_program | TRIM" );
  113. 			Console.ResetColor( );
  114.  
  115. 			Console.Error.WriteLine( );
  116.  
  117. 			Console.Error.Write( "Where:  " );
  118. 			Console.ForegroundColor = ConsoleColor.White;
  119. 			Console.Error.Write( "textfile" );
  120. 			Console.ResetColor( );
  121. 			Console.Error.WriteLine( "      file containing text to be trimmed" );
  122.  
  123. 			Console.ForegroundColor = ConsoleColor.White;
  124. 			Console.Error.Write( "        text" );
  125. 			Console.ResetColor( );
  126. 			Console.Error.WriteLine( "          string to be trimmed" );
  127.  
  128. 			Console.ForegroundColor = ConsoleColor.White;
  129. 			Console.Error.Write( "        some_program" );
  130. 			Console.ResetColor( );
  131. 			Console.Error.WriteLine( "  program whose output is to be trimmed" );
  132.  
  133. 			Console.Error.WriteLine( );
  134.  
  135. 			Console.Error.WriteLine( "Note:   Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." );
  136.  
  137. 			Console.Error.WriteLine( );
  138.  
  139. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  140.  
  141. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  142.  
  143. 			#endregion Display Help
  144.  
  145.  
  146. 			return -1;
  147. 		}
  148. 	}
  149. }

page last uploaded: 2021-01-27