Jump to content

Recommended Posts

Posted

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:

CODE

Func _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 $Return

EndFunc

And 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 value

It does require my _StringInChr function:

http://www.autoitscript.com/forum/index.php?showtopic=12385

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