Sign in to follow this  
Followers 0
GoogleDude

adding command line switches to my compiled script

13 posts in this topic

I have a script that I want to be able to run for example "myscript.exe /runme" and if the runme switch it typed I want it to call a function in my script but only if the switch is called via a command line?

Can this be done with autoit?

Thanks,

GoogleDude

Share this post


Link to post
Share on other sites



I have a script that I want to be able to run for example "myscript.exe /runme" and if the runme switch it typed I want it to call a function in my script but only if the switch is called via a command line?

Can this be done with autoit?

Thanks,

GoogleDude

Didn't quite get you. Mind explain in more detail or post an example code?

Share this post


Link to post
Share on other sites

Well I dont have any sample code yet.

But I have a small script I am going to start working on here soon that simply shuts down windows. for example "shutdownwindows.exe" will shutdown windows without any user intervention, but if someone was to run "shutdownwindows.exe /dialog" it will jump to a fuinction such as "msgbox" wich will display a message that tells the user that the system is shutting down.

Hoe this helps,

GoogleDude

Share this post


Link to post
Share on other sites

Well I dont have any sample code yet.

But I have a small script I am going to start working on here soon that simply shuts down windows. for example "shutdownwindows.exe" will shutdown windows without any user intervention, but if someone was to run "shutdownwindows.exe /dialog" it will jump to a fuinction such as "msgbox" wich will display a message that tells the user that the system is shutting down.

Hoe this helps,

GoogleDude

You can run from command line "shutdown -a -t -s" just check out shutdown at cmd.

Share this post


Link to post
Share on other sites

I was just using that as an example.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Search for cmdline or cmdlineraw in the help file..:shocked:

From the top of my head:

Local $i
    For $i = 1 to $cmdline[0]
        ConsoleWrite($cmdLine[$i] & @CRLF)
    Next
    ConsoleWrite($CmdLineRaw & $CRLF)

EDIT: typo.

Edited by Uten

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Ok. I have been playing around and I kinda have a sample working but it kinda backwords as to when I want to switch command to run.

Using the code below if I type in "test.exe /123" displays the MSGBox for command line options after it does the MSGBox "this is a test box 123456" How can I get it to display the MSGBox for Command Line Options before it displays the MSGBox "This is a test box 123456"?

Right now if I just run test.exe I want it to just display the This is a test box 123456 but it does nothing. If I type test.exe /123 I want it to Display a Msgbox "hello world" then MSGBox "This is a test box 123456!"

Hope this makes sense.

if $CmdLine[0]> 0 Then
    MsgBox(1024,"","This is a test box 123456")
    $Cmdopt = StringSplit($CmdLineRaw, "/")
    for $I = "2" to $CmdLine[0] + "1"
        $Cmdopt[$I] = StringStripWS ($Cmdopt[$I], 2)
        If $Cmdopt[$I] = "123" Then MsgBox(1024,"Test","This is a test")
        If $Cmdopt[$I] = "?" Then 
            MsgBox(64,"Command Line Options","test.exe /123 /?" & @CRLF & @CRLF & "/123 = Displays a Test message dialog box" & @CRLF & @CRLF & "/? = Displays this dialog box")
            Exit
        EndIf
    Next
EndIf
Exit
Edited by GoogleDude

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Put the msgbox "123456" after the part you want to display first?

Also, this:

for $I = "2" to $CmdLine[0] + "1"oÝ÷ Ù(hºW[zØb±«­¢+Ù½ÈÀÌØí$ôÈѼÀÌØí
µ1¥¹lÁt

The + 1 you had would make it error actually. And the numbers shouldn't be encapsulated with quotation marks, or else they become strings, which might not be a big deal right now, but in the future could cause issues. Better to stop quoting them now.

Any particular reason you start $I at 2 instead of 1?

*Edit: Nevermind, I see now you're doing something weird that I didn't get at first, so a different question. Any particular reason you're manually splitting $CmdLineRaw instead of just using $CmdLine[$I] ?

Edited by Saunders

Share this post


Link to post
Share on other sites

Well I basicly took some of the source code from (ChrisControle v1.5) since it makes use of command line options and use what I would give me an Idea as to how they work. I chopped it up to make use of some sample code.

I was mainly just trying to make use of the original code so I could better understand how this all works as far as command line options go. If there is any better/cleaner way of having a sample then by all means I am all ears/eyes. Im still kinda new to this.

Thanks for the help by the way.

Original Code

;====COMMAND LINE PARAMETERS==========================================================================================
=======
;
; /h: = Hostname
; /u: = Username
; /p: = Password
; /r  = Enable RDP Protocol
; /v  = Enable VNC Protocol
; /rdpcon = RDP Console Session
; /rvncf = Remove VNC from Host First (if Running)
; /noask = Don't Ask to Install / Remove VNC
; /remvnc = Remove VNC on Exit
; /go  = Connect on Launch
; /?  = Show this dialogue
; /hide = Hide GUI
;
Func ReadCmdLne(ByRef $Cmdgo)
    If $CmdLine[0]> 0 Then
        GUICtrlSetState($RDPProt, $GUI_UNCHECKED)
        GUICtrlSetState($VNCProt, $GUI_UNCHECKED)
        GUICtrlSetState($Rdpconsle, $GUI_UNCHECKED)
        GUICtrlSetState($Remvncfst, $GUI_UNCHECKED)
        GUICtrlSetState($AutoVNC, $GUI_UNCHECKED)
        GUICtrlSetState($Remvnc, $GUI_UNCHECKED)
        $Cmdopt = StringSplit($CmdLineRaw, "/")
        for $I = "2" to $CmdLine[0] + "1"
            $Cmdopt[$I] = StringStripWS ($Cmdopt[$I], 2)
            If StringInStr($Cmdopt[$I], "h:") Then 
                $Cmdhost = StringTrimLeft($Cmdopt[$I], 2)
                GUICtrlSetData($Host, $Cmdhost, $Cmdhost)
            EndIf
            If StringInStr($Cmdopt[$I], "u:") Then 
                $Cmduser = StringTrimLeft($Cmdopt[$I], 2)
                GUICtrlSetData($User, $Cmduser)
            EndIf
            If StringInStr($Cmdopt[$I], "p:") Then 
                $Cmdpass = StringTrimLeft($Cmdopt[$I], 2)
                GUICtrlSetData($Pass, $Cmdpass)
            EndIf
            If $Cmdopt[$I] = "r" Then GUICtrlSetState($RDPProt, $GUI_CHECKED)
            If $Cmdopt[$I] = "v" Then GUICtrlSetState($VNCProt, $GUI_CHECKED)
            If $Cmdopt[$I] = "rdpcon" Then GUICtrlSetState($Rdpconsle, $GUI_CHECKED)
            If $Cmdopt[$I] = "rvncf" Then GUICtrlSetState($Remvncfst, $GUI_CHECKED)
            If $Cmdopt[$I] = "noask" Then GUICtrlSetState($AutoVNC, $GUI_CHECKED)
            If $Cmdopt[$I] = "remvnc" Then GUICtrlSetState($Remvnc, $GUI_CHECKED)
            If $Cmdopt[$I] = "go" Then $Cmdgo = "1"
            If $Cmdopt[$I] = "?" Then 
                MsgBox(64,"Command Line Options", "ChrisControl.exe /h:<host> /u:<username> /p:<password> "& @CRLF & "/r /v /rdpcon /remvnc /go" & @CRLF & @CRLF & "/h: = Hostname" & @CRLF & @CRLF &"/u: = Username" & @CRLF & @CRLF & "/p: = Password" & @CRLF & @CRLF & "/r  = Enable RDP Protocol" & @CRLF & @CRLF & "/v  = Enable VNC Protocol" & @CRLF & @CRLF & "/rdpcon = RDP Console Session" & @CRLF & @CRLF & "/rvncf = Remove VNC from Host First (if Running)" & @CRLF & @CRLF & "/noask = Don't Ask to Install / Remove VNC" & @CRLF & @CRLF & "/remvnc = Remove VNC on Exit" & @CRLF & @CRLF & "/go  = Connect on Launch" & @CRLF & @CRLF & "/?  = Show this dialogue")
                OnExit()
            EndIf
        Next
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Your looking at CMDLINE in a big way as you show. Most script do not need anything as big. But if you want big for a major script then there is a couple of Abbreviations in the full Scite4AutoIt3. If you type cmdlineselect or cmdlineswitch and press spacebar, then you get them expanded.

#region - CmdlineSelect
If $CMDLINE[0] Then
    For $i = 1 To $CMDLINE[0]
        Select
            Case $CMDLINE[$i] = '/?'
                MsgBox(0x40000, @ScriptName & ' Help', _
                        'Switches are:' & @CRLF _
                         & @CRLF & '/extract' _
                         & @CRLF & @TAB & 'Extract files to current directory' _
                         & @CRLF & '/x' _
                         & @CRLF & @TAB & '' _
                         & @CRLF & '/x' _
                         & @CRLF & @TAB & '' _
                         & @CRLF & '/x' _
                         & @CRLF & @TAB & '' _
                         & @CRLF & '/x' _
                         & @CRLF & @TAB & '')
                Exit
            Case $CMDLINE[$i] = '/extract'
                FileInstall('x', @ScriptDir & '\')
                Exit
            Case $CMDLINE[$i] = '/x'
            Case $CMDLINE[$i] = '/x'
            Case $CMDLINE[$i] = '/x'
            Case $CMDLINE[$i] = '/x'
            Case Else
                MsgBox(0x40000, 'Incorrect switch used', _
                        'Command used:' & @CRLF & $CMDLINERAW & @CRLF & _
                        @CRLF & 'Use /? for the switches available.')
                Exit
        EndSelect
    Next
EndIf
#endregion
oÝ÷ Ù«­¢+Ø(É¥½¸´
µ±¥¹MÝ¥Ñ )%ÀÌØí
51%9lÁtQ¡¸(%½ÈÀÌØí¤ôÄQ¼ÀÌØí
51%9lÁt($%MÝ¥Ñ ÀÌØí
51%9lÀÌØí¥t($$%
ÍÌäì¼üÌäì($$$%5Í  ½à ÁàÐÀÀÀÀ°MÉ¥ÁÑ9µµÀìÌäì!±ÀÌäì°|($$$$$$ÌäíMݥѡÌÉèÌäìµÀì
I1|($$$$$$µÀì
I1µÀìÌäì½áÑÉÐÌäì|($$$$$$µÀì
I1µÀìQµÀìÌäíáÑÉÐ¥±ÌѼÕÉɹХÉѽÉäÌäì|($$$$$$µÀì
I1µÀìÌäì½àÌäì|($$$$$$µÀì
I1µÀìQµÀìÌäìÌäì|($$$$$$µÀì
I1µÀìÌäì½àÌäì|($$$$$$µÀì
I1µÀìQµÀìÌäìÌäì|($$$$$$µÀì
I1µÀìÌäì½àÌäì|($$$$$$µÀì
I1µÀìQµÀìÌäìÌäì|($$$$$$µÀì
I1µÀìÌäì½àÌäì|($$$$$$µÀì
I1µÀìQµÀìÌäìÌäì¤($$$%á¥Ð($$%
ÍÌäì½áÑÉÐÌäì($$$%¥±%¹Íѱ° ÌäìüÌäì°MÉ¥ÁѥȵÀìÌäìÀäÈìÌäì¤($$$%á¥Ð($$%
ÍÌäì½àÌäì($$%
ÍÌäì½àÌäì($$%
ÍÌäì½àÌäì($$%
ÍÌäì½àÌäì($$%
ͱÍ($$$%5Í  ½à ÁàÐÀÀÀÀ°Ìäí%¹½ÉÉÐÍÝ¥Ñ ÕÍÌäì°|($$$$$$Ìäí
½µµ¹ÕÍèÌäìµÀì
I1µÀìÀÌØí
51%9IµÀì
I1µÀì|($$$$$%
I1µÀìÌäíUͼü½ÈÑ¡ÍݥѡÌÙ¥±±¸Ìäì¤($$$%á¥Ð($%¹MÝ¥Ñ (%9áÐ)¹%(¹É¥½¸oÝ÷ Ø­æjYr²¶§X¤zØb²f²vè­«&jYlr¸©µ«­¢+Ø)%ÀÌØí
51%9lÁt¹ÀÌØí
51%9lÅtôÌäì½ÉÕ¹µÌäìQ¡¸($ÀÌØíÉÕ¹µôÀÌØí
51%9lÅt)±Í(%á¥Ð)¹%()5Í  ½à À°ÌäìÌäì°ÀÌØíÉÕ¹µ¤(

:shocked:

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

But normally, something like this may do for a small script.

If $CMDLINE[0] And $CMDLINE[1] = '/runme' Then
    $runme = $CMDLINE[1]
Else
    Exit
EndIf

MsgBox(0, '', $runme)

:shocked:

In your sample what function is run in the event that no switch is given?

Sorry for thr dumb question. Im still learning here!

Edit: Never mind I figured it out. Thats what the EXIT was for. I just replaced EXIT with my function as it apears to answer my question. Thanks a whole lot yet again Everyone. But what about adding multipul swiches with different functions?

GoogleDude

Edited by GoogleDude

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

The Exit does not need to be used if no switch is passed. You can continue the script as any way that you see suitable.

If $CMDLINE[0] And $CMDLINE[1] = '/runme' Then
    $runme = $CMDLINE[1]
Else
    $runme = ''
EndIf

MsgBox(0, '', $runme)

If $runme Then
    ; we may use the switch
Else
    ; we will something else
EndIf

; Rest of script

The example is just one idea, but many variations that you can create. Just make sure you test $CMDLINE[0] for not being 0 or the number of parameters that you want or require before you use $CMDLINE[1], $CMDLINE[2] and so on or else you may get an error of not being declared for use.

Edit:

Minor code fix

Edited by MHz

Share this post


Link to post
Share on other sites

Thanks, with your much eiser sample I was able to accomplish what I was after. Thanks again.

GoogleDude

Share this post


Link to post
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
Sign in to follow this  
Followers 0