Jump to content

Command line parameters & passing string to variable


Go to solution Solved by Nine,

Recommended Posts

Posted

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. 

  • Solution
Posted (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 by Nine
added invalid param / corrected small bug in func
Posted

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

 

Posted

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. 

 

  

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