Jump to content



Photo

Exit code UDF


  • Please log in to reply
1 reply to this topic

#1 Gigglestick

Gigglestick

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 501 posts

Posted 29 April 2011 - 09:51 PM

I created this awhile back because I was tired of keeping track of what exit codes I'd used in various parts of my code and searching my code when I wanted to rearrange their order. I thought others else might find it useful as well.

By default, the exit code is the index of the entry in the private array (i.e. the order you registered them). You can specify an alternative (forced) exit code during registration, or afterward by calling _ExitRegister with the same index string (the first parameter).

Simply by including the UDF file, you can run your script with a command line parameter "/listexitcodes" to make it dump a list of codes and short descriptions when the script completes (not crashes, but exits). This can be disabled by calling _ExitListOnExit(False).

First:
_ExitRegister( _     "a useful index", _     ["short description for help text"], _     ["A long description of the problem, written to the console upon exiting"], _     [100], _ ; Exit code 100 - Default would have been 1 since it's the first one registered     ["_MyFunction"]) ; A function to call before exiting

Then later:
_ExitUsing("a useful index") or _ExitUsing("a useful index", $iAnErrorForTheFunctionToHandle, "_CallThisFunctionInstead")

A quick example:
AutoIt         
#include "ExitCodes.au3" _ExitRegister("show help", "Show program help", Default, 0, "_ShowHelp") _ExitRegister( _         "some other reason", _         "Something awful", _         "This is a long description about something really awful happening", _         100) If StringInStr($CmdLineRaw, "/?") or StringInStr($CmdLineRaw, "/help") Then _ExitUsing("invalid command line", "This is some additional information about why we're stopping") ; Do some stuff, and at some point: _ExitUsing("some other reason") Func _InvalidCmdFunc($sParam = Default)     If $sParam <> Default Then ConsoleWrite($sParam) EndFunc   ;==>_InvalidCmdFunc Func _ShowHelp($sParam = Default)     ConsoleWrite("Usage: " & @ScriptName & " [parameters]" & @CRLF)     If $sParam <> Default Then ConsoleWrite($sParam & @CRLF) EndFunc   ;==>_ShowHelp

A list of the functions:
_ExitCount() _ExitGetCode( "index string" ) _ExitGetFunc( "index string" ) _ExitGetIndex( code [, array] ) _ExitGetList( array [, pad [, "indent" [, "separator"]] ) _ExitGetLongDesc( "index string" ) _ExitGetShortDesc( "index string" ) _ExitListOnExit( [enabled] ) _ExitRegister( "index string" [, "short description" [, "long description" [, code [, "function"]]]] ) _ExitUnregister( "index string" ) _ExitUsing( "index string" [, param [, "function"]] )


Attached File  ExitCodes.au3   22.59K   202 downloads

Attached File  ExitCodes.zip   7.99K   208 downloads

Edited by c0deWorm, 02 May 2011 - 03:44 PM.

My UDFs: ExitCodes





#2 Allow2010

Allow2010

    Prodigy

  • Active Members
  • PipPipPip
  • 183 posts

Posted 13 August 2011 - 08:11 AM

Thanks, nice idea...might use it in one of my bigger scripts...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users