using System; using System.IO; using System.Linq; namespace RobvanderWoude { internal class Trim { static readonly string progver = "1.00"; static int Main( string[] args ) { int argumentscount = args.Length; if ( Console.IsInputRedirected ) { argumentscount++; } if ( argumentscount != 1 || args.Contains( "/?" ) ) { return ShowHelp( ); } string input = string.Empty; if ( Console.IsInputRedirected ) { input = Console.In.ReadToEnd( ); } else if ( File.Exists( args[0] ) ) { input = File.ReadAllText( args[0] ); } else { input = args[0]; } string output = input.Trim( ); Console.Write( output ); return 0; } public static int ShowHelp( string errorMessage = "" ) { #region Help Text /* Trim, Version 1.00 Trim leading and trailing whitespace (including linefeeds) from string Usage: TRIM textfile or: TRIM text or: some_program | TRIM Where: textfile file containing text to be trimmed text string to be trimmed some_program program whose output is to be trimmed Note: Exit code ("errorlevel") -1 in case of errors, otherwise 0. Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Error Message if ( !string.IsNullOrWhiteSpace( errorMessage ) ) { Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errorMessage ); Console.ResetColor( ); } #endregion Error Message #region Display Help Console.Error.WriteLine( ); Console.Error.WriteLine( "Trim, Version {0}", progver ); Console.Error.WriteLine( "Trim leading and trailing whitespace (including linefeeds) from string" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "TRIM textfile" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( " or: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "TRIM text" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( " or: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "some_program | TRIM" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "textfile" ); Console.ResetColor( ); Console.Error.WriteLine( " file containing text to be trimmed" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " text" ); Console.ResetColor( ); Console.Error.WriteLine( " string to be trimmed" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " some_program" ); Console.ResetColor( ); Console.Error.WriteLine( " program whose output is to be trimmed" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Note: Exit code (\"errorlevel\") -1 in case of errors, otherwise 0." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help return -1; } } }