SFSe Posted March 6, 2022 Posted March 6, 2022 I am new to AutoIt and was hoping to see if I could get assistance on accepting command line parameters and/or passing values to the script. Below is an example of what I have put together to accept a parameter to do something or if needed to read a value if there is a parameter with a "=". #include <Array.au3> If $CmdLine[0] <> 0 Then For $i = 0 To UBound($CmdLine) - 1 Select ; Parameter to do something Case $CmdLine[$i] = "/Parm1" ConsoleWrite("/Parm1 detected") ; do something ; Input value to variable Case StringRegExp($CmdLine[$i], "(?i)^/Value=") = 1 ; input value to variable $Value = _paramInput($CmdLine[$i], "(?i)^/Value=") ConsoleWrite("Intput value = "& $Value) EndSelect Next EndIf ; Function for parameter input and remove " or ' from string Func _paramInput($string, $param) $sreResult = StringRegExp($string, "^" & $param & "(.*)", 3) $srerResult = StringRegExpReplace($sreResult[0], "'|""", "") Return $srerResult EndFunc ;==>_paramInput It seems to work as expected and will keep adding additional parameters but if there is better way to accomplish this I would be very grateful to suggestions.
ad777 Posted March 6, 2022 Posted March 6, 2022 @SFSe all i know is to inc case for more fast result ex: For $i = 0 to .... Case $cmd...$i Case $cmd...$i2 .... Next or Select Case $Cmd...[$i] = .... or $Cmd...[$i+1] = ... ;;do stuff EndSelect none
Solution Nine Posted March 6, 2022 Solution Posted March 6, 2022 (edited) Here's my way of doing this : #include <Array.au3> ; parameter are in the form of /param[=value] ; the next line is just for testing purpose If Not $CmdLine[0] Then Exit Run(@AutoItExe & " " & @ScriptFullPath & " /param1=Value /param2 test") Local $sParam, $sValue For $i = 1 To $CmdLine[0] ExtractParam ($CmdLine[$i], $sParam, $sValue) Switch $sParam Case "param1" If Not $sValue Then Exit MsgBox($MB_SYSTEMMODAL, "Error", "Invalid value on param1") ; do something here MsgBox($MB_SYSTEMMODAL, "Testing", $sParam & "/" & $sValue) Case "param2" If $sValue Then Exit MsgBox($MB_SYSTEMMODAL, "Error", "there is no value on param2") ; do something here MsgBox($MB_SYSTEMMODAL, "Testing", $sParam) Case Else Exit MsgBox($MB_SYSTEMMODAL, "Error", "Invalid parameter") EndSwitch Next Func ExtractParam($string, ByRef $param, ByRef $value) $param = "" $value = "" Local $aResult = StringRegExp($string, "\/([^=]*)=?(.*)", 1) If IsArray($aResult) Then $param = StringLower($aResult[0]) If UBound($aResult) = 2 Then $value = $aResult[1] EndIf EndFunc Edited March 6, 2022 by Nine added invalid param / corrected small bug in func “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted March 6, 2022 Posted March 6, 2022 Personally I've been using the following udf for several years in my scripts.
Nine Posted March 6, 2022 Posted March 6, 2022 @Subz as per UDF : Quote Also, please note that this UDF can NOT parse arguments in the format (key=value). Example: script.exe key=value IT WILL NOT WORK “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted March 6, 2022 Posted March 6, 2022 Maybe read the OP incorrectly, thought if the value had an equals sign in it for example: script.exe --Key Value=Example _CmdLine_Get("Key") would return "Value=Example" Normally to get key=value, I'd just use the following instead without the equal sign. script.exe --Key1 Value1 --Key2 Value2
SFSe Posted March 6, 2022 Author Posted March 6, 2022 Thank you everyone for the assistance this has been very helpful and informative. @ad777 I will keep that in mind if I require the script to run faster. @Nine This is much cleaner than what I was doing and works great! @Subz Thanks for sharing the CmdLine UDF, even though it could not parse the string with an "=" for the value it is still worth saving as another nice way to get a command line value.
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