Jump to content

Simple command line switches.


MerkurAlex
 Share

Recommended Posts

Yes its extremely simple but it works well and comes in handy A LOT.

if $cmdline[0]<>0 then
    for $i=1 to $cmdline[0]
        Switch $cmdline[$i]
            case "/switch1";this is a simple switch that can set a variable or aynthing else you need it to in this case call a function
                dosomething()
            case "/switch2";this switch requires a value after it
                if $cmdline[0]=$i+1 or $cmdline[0]>$i+1 then 
                    msgbox("","","/switch2 = "&$cmdline[$i+1])
                Else
                    MsgBox(16,"Error","Error in the command line!")
                    Exit
                EndIf
        EndSwitch
    Next
EndIf

func dosomething()
    msgbox("","","i did something!")
endfunc

A few batch lines to test with:

sclss.exe /switch1
sclss.exe /switch2
sclss.exe /switch2 test
sclss.exe /switch2 blahblah /switch1

the first will show a msgbox the second give you an error the third will give a different msgbox with the value and the fourth is a combination of both working.

Have fun :)

[quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]

Link to comment
Share on other sites

Nice simple example:

I hope you don't mind but I modified your code in one place... :think:

If $cmdline[0] <> 0 Then
    For $i = 1 To $cmdline[0]
        Switch $cmdline[$i]
            Case "/switch1";this is a simple switch that can set a variable or aynthing else you need it to in this case call a function
                dosomething()
            Case "/switch2";this switch requires a value after it
                If $cmdline[0] >= $i + 1 Then
                    MsgBox("", "", "/switch2 = " & $cmdline[$i + 1])
                Else
                    MsgBox(16, "Error", "Error in the command line!")
                    Exit
                EndIf
        EndSwitch
    Next
EndIf

Func dosomething()
    MsgBox("", "", "i did something!")
EndFunc  ;==>dosomething

This is what I used to run it... :)

$file = @ScriptDir & "\cmdlinetest.exe"

RunWait ($file & " /switch1")
RunWait ($file & " /switch2")
RunWait ($file & " /switch2 test")
RunWait ($file & " /switch2 blahblah /switch1")

Thanks for sharing! :lmao:

Cheers,

Brett

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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