blindwig Posted June 11, 2005 Share Posted June 11, 2005 If you're someone who likes to use command-line parameters, you'll probably appreciate this UDF. It checks the command line parameters, trying to determine the value of a parameter. For example:If you have on the command line "/user ItsMe! /password NeverGuess", you could do this:_ParameterGetValue('user')and it would return 'ItsMe!'It also works for:'/user=ItsMe''/user:ItsMe''/userItsMe'but is smart enough to avoid this:'/user /password NeverGuess'You can pass your own switch prefix, so these work too:'-user ItsMe!''.user=ItsMe!'Here's the code:CODEFunc _ParameterGetValue($Switch, $SwitchPrefix = '/', $CaseSense = 0) Dim $i, $num = $CmdLine[0], $Sw = $SwitchPrefix & $Switch, $SwLen = StringLen($Sw), $Return = '' ;First, look for the switch While $num > 0 If $CaseSense Then If StringLeft($CmdLine[$num], $SwLen) == $Sw Then ExitLoop Else If StringLeft($CmdLine[$num], $SwLen) = $Sw Then ExitLoop EndIf $num = $num - 1 WEnd If $Num > 0 Then ;First check for '/Switch=Value' situation $i = _StringInChr($CmdLine[$num], ':=,') If $i <> 0 Then $Return = StringMid($CmdLine[$num], $i + 1) ;Next check for '/SwitchValue' situation ElseIf StringLen($CmdLine[$num]) > $SwLen Then $Return = StringMid($CmdLine[$num], $SwLen + 1) ;Next check for '/Switch Value' situation ElseIf $num + 1 > $CmdLine[0] Then ; there is no value (there is no next parameter) $Return = '' ElseIf _StringInChr(StringLeft($CmdLine[$num + 1], 1), $SwitchPrefix) Then ;this must be another parameter, not a value $Return = '' Else $Return = $CmdLine[$num + 1] EndIf Else ;No switch = error SetError(1) EndIf Return $ReturnEndFuncAnd it returns with an error if the switch was not found at all, so you can also use it to detect a switch, for example if you have a switch that doesn't have a valueIt does require my _StringInChr function:http://www.autoitscript.com/forum/index.php?showtopic=12385 My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now