using System.Collections.Generic;
static string progver = "1.02.1";
static int Main( string[] args )
bool returnremaining = false;
#region Parse Command Line
if ( args.Length == 0 || args[0] == "/?" )
return ShowHelp( "Too many command line arguments" );
seconds = Convert.ToInt32( args[0] );
return ShowHelp( "Invalid number of seconds ({0})", args[0] );
if ( seconds < 1 || seconds > 86400 )
return ShowHelp( "Specified number of seconds ({0}) outside allowed range", args[0] );
if ( args[1].ToUpper( ) == "/R" )
else if ( args[1] == "/?" )
return ShowHelp( "Invalid command line argument \"{0}\"", args[1] );
#endregion Parse Command Line
Console.CursorVisible = false;
Console.TreatControlCAsInput = true;
int length = seconds.ToString( ).Length;
Console.Write( new String( ' ', length ) );
while ( !Console.KeyAvailable && countdown > 0 )
Console.Write( new String( '\b', length ) );
Console.Write( new String( ' ', length ) );
Console.Write( new String( '\b', length ) );
Console.Write( "{0," + length + "}", countdown );
Console.Write( new String( '\b', length ) );
Console.Write( new String( ' ', length ) );
Console.Write( new String( '\b', length ) );
Console.CursorVisible = true;
Console.TreatControlCAsInput = false;
if ( countdown > 0 && Console.KeyAvailable )
else if ( countdown == 0 )
static int ShowHelp( params string[] errmsg )
List<string> errargs = new List<string>( errmsg );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "CountDown, Version {0}", progver );
Console.Error.WriteLine( "Count down for the specified number of seconds or until a key is pressed" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "COUNTDOWN seconds [ /R ]" );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "seconds" );
Console.Error.WriteLine( " is in the range of 1 .. 86400 (1 second .. 24 hours)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /R" );
Console.Error.Write( " return code equals the number of " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "R" );
Console.Error.WriteLine( "emaining seconds" );
Console.Error.WriteLine( " (default: return code 0 = OK, 1 = errors, 2 = key pressed)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by kingaustin247" );
Console.Error.WriteLine( "http://www.kingcraft.epizy.com" );
#endregion Display Help Text