dmorand

Pass arguments to script through command line

16 posts in this topic

I have a script that I created that I just compiled as an exe. I would like to pass 3 arguments to the exe and assign them to variables I have setup. How would I go about doing this? I'm fairly new to writing scripts so sorry if this is dumb.

Share this post


Link to post
Share on other sites



I have a script that I created that I just compiled as an exe. I would like to pass 3 arguments to the exe and assign them to variables I have setup. How would I go about doing this? I'm fairly new to writing scripts so sorry if this is dumb.

Go to the help file goto index and type in Command Line Parameters and it gives you all the information about using command line parameters.

Share this post


Link to post
Share on other sites

Sorry I did search, I just couldn't find anything, I'll make sure I search better next time.

Share this post


Link to post
Share on other sites

I bet you searched on the forums, not the help file.

Yes that's exactly what I did. I haven't had good luck ever with application help files, that's why.

Share this post


Link to post
Share on other sites

Search for "CmdLine" and pick the first item on the list -> "Running Scripts".

Yes that's exactly what I did. I haven't had good luck ever with application help files, that's why.

Share this post


Link to post
Share on other sites

Sorry I did search, I just couldn't find anything, I'll make sure I search better next time.

Thanks Dmorand for taking the heat. I was almost going to send another question about this until I found your post. My problem was that I didn't know that command line was called command line so i didn't know what to search for.

Share this post


Link to post
Share on other sites

Perhaps I don't have a very good understanding of Cmdline paramaters but Can cmd lines be injected into an already running script?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Perhaps I don't have a very good understanding of Cmdline paramaters but Can cmd lines be injected into an already running script?

Not in a straightforward way.

Say you have a script alredy running.

Then you run another instance but pass it the parameters. Then the second one knows what the parameters are, it can detect that there is already a version running so then it can tell the previous version what the parameters are and close. To tell the other script what the parameters are it can use messages (my preference) or some other method. There are many other methods. For example if the script has an edit then you can use ControlCommanbd to send text to the edit, then the script can detect the change and read it. Or you could write a file with the data in it, the other script detects the file, reads it and deletes it.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

How do you loop through the args ? for example

args are

one two three four five

I need the first arg that I can ref using %1%, but then I need take two three four five and stick them together in

one variable. The total number of arguments always varies. I know %0% will count the args, but I am not sure how to make the variable I am looking for. In unix I just var=`echo $* |sed "s/arg1 //g"` just to clarify what I need to do.

Thanks

Share this post


Link to post
Share on other sites

How do you loop through the args ? for example

args are

one two three four five

I need the first arg that I can ref using %1%, but then I need take two three four five and stick them together in

one variable. The total number of arguments always varies. I know %0% will count the args, but I am not sure how to make the variable I am looking for. In unix I just var=`echo $* |sed "s/arg1 //g"` just to clarify what I need to do.

Thanks

Hello, you have to make a concatenation loop, something like this:

$OTHERARGS=""
for $CPT=2 to $CMDLINE[0]
$OTHERARGS&=$CMDLINE[$CPT]
next
msgbox(0,"test","1st arg=" & $CMDLINE[1] & @crlf & "Other args=" & $OTHERARGS)

Share this post


Link to post
Share on other sites

Six years later, this is how I accessed the arguments within my script:

$valueOne = _Args("/argumentOne", ":")
$valueTwo = _Args("/argumentTwo", ":")

ConsoleWrite($valueOne)
ConsoleWrite($valueTwo)

Func _Args($argument, $delimiter)
    If Ubound($CmdLine) > 1 Then
        For $i = 1 To UBound($CmdLine)-1 Step 1
            If StringInStr($CmdLine[$i], $argument, 0) Then
                If StringInStr($CmdLine[$i], $delimiter, 0) Then
                    $value = StringSplit($CmdLine[$i], $delimiter)
                    Return $value[2]
                EndIf
            EndIf
        Next
    EndIf
EndFunc

And this is how I added the arguments when calling the script:

"C:\install\AutoIt3.exe" "C:\ArgsTest.au3" /argumentOne:MyFirstValue /argumentTwo:MySecondValue

 

Share this post


Link to post
Share on other sites

6 years later, and the OP hasn't even been active in that 6 years. Thanks for demonstrating why we don't necro old posts...


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Six years later, this is how I accessed the arguments within my script:

Thanks, this was the first result when searching Google keywords 'pass arguments to script site:www.autoitscript.com'. I found your post extremely helpful. It saved me a bunch of time.

Edited by erblemoof

Share this post


Link to post
Share on other sites

#16 ·  Posted

Howdy,

Thanx for this small but usefull UDF.
By adding the flag 1 to stringsplit you can use more than one character as delimiter (ie. /argumentOne::"c:\temp\outputfile.txt")

Func _Args($argument, $delimiter)
    If Ubound($CmdLine) > 1 Then
        For $i = 1 To UBound($CmdLine)-1 Step 1
            If StringInStr($CmdLine[$i], $argument, 0) Then
                If StringInStr($CmdLine[$i], $delimiter, 0) Then
                    $value = StringSplit($CmdLine[$i], $delimiter, 1)
                    Return $value[$value[0]]
                EndIf
            EndIf
        Next
    EndIf
EndFunc


Cheers!

 

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