Jump to content

Exit code UDF


Recommended Posts

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).


_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")


_ExitUsing("a useful index", $iAnErrorForTheFunctionToHandle, "_CallThisFunctionInstead")

A quick example:

#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", _

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:

_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"]] )



Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...