Jump to content
Sign in to follow this  
redrider81

Named CLI Parameters In Any Order

Recommended Posts

redrider81

I'm amateur. If this already exists in some better form, then please disregard and close this post. I'll proceed as though it doesn't.

There must be some reason the AutoIt CLI functionality has remained the way it is, which seems to have some limitations, but I don't know what it is.  Here is a simple array search method to add named parameter support to CLI scripts, and they can be provided in any order.  It gets the job done, and more closely emulates dos switches in the ways mentioned.

It's good enough for my current project so I'm done for now.

The big advantage is that currently, it can slide into any Command-Line script very easily.

It's so easy it's dumb.

1. Just change the number of params to match your script (5 in the example)

2. Change the names of those params in the array variable definition.

3. Customize the "usage instructions" If/Then statement.

Your app will now function using /argument "parameter" pairs in any order, rather than "parameters" in a required order.

To be converted to a UDF, it needs a lot. I think an expert could do it quickly though.

I don't even know if this should be a UDF actually.

It needs real error handling, proper usage instructions, etc. (mine are a joke).

It should probably be adjusted to deal with switches which are solitary such as "/s" for silent. 

Right now you would need to make it "/silent "yes". 

I didn't even test to see how it will handle slashes, quotes, etc. Probably breaks it.

Every parameter currently needs an argument or the whole thing breaks.

This is because I'm just skipping every other cmdLine[#] like a hack. 

I spent two hours writing something much more convoluted, splitting, trimming etc.

I ended up with this very simple and elegant, but probably unstable method, and decided to move on.

Anyone find this useful? 

Anyone want to take it and make it sweet?

Good luck.

Edit1 * Oh yeah, the named parameters are now in an multidimensional array, so you have to call them later as $cmdLineParams[0][1] - $cmdLineParams[4][1]  .

In my example, I just went back and gave them meaningful names inside the script. This is of course cosmetic and optional, probably even a waste of memory.

#include <Array.au3>
#AutoIt3Wrapper_Change2CUI=y

;Evaluate command has correct number of variables, and look for /help switch, render usage instructions either way.
If $cmdLine[1] = "/help" Or $cmdLine[0] < 10  OR $cmdLine[0] > 10 Then
    ConsoleWrite('----------------------' & @CRLF & _
    'Error: Wrong Number of Parameters, Required Parameters = 5.' & @CRLF & @CRLF &  _
    'Example:       MyService.exe /Mode "Install" /Name "Service Name" /DisplayName "My Display Name" /Description "My Description" /Path "Path to Service Executable"' & @CRLF & @CRLF & _
    '----------------------'& @CRLF)
    Exit
EndIf

;Define array that will be used to organize the variables into a nice named array
Local $cmdLineParams[5][2] = [ _
        ["/Mode", ""], _
        ["/Name", ""], _
        ["/DisplayName", ""], _
        ["/Description", ""], _
        ["/Path", ""]]

;Lookup each parameter in the array and fill in the value
For $i = 1 To $cmdLine[0]
    $indexFound = _arraySearch($cmdLineParams,$cmdLine[$i])
    $cmdLineParams[$indexfound][1] = $cmdLine[$i + 1]
    $i = $i + 1
Next

;Give all the variables descriptive names rather than referencing the array. This is optional for those not all that comfortable with arrays. 
$cliParamMode = $cmdLineParams[0][1]
$cliParamName = $cmdLineParams[1][1]
$cliParamDisplayName  = $cmdLineParams[2][1]
$cliParamDescription = $cmdLineParams[3][1]
$cliParamPath = $cmdLineParams[4][1]

;These are just debug type commands to see what the CLI actually interpreted. 
_ArrayDisplay($cmdLineParams, "$cmdLineParams")
ConsoleWrite($cliParamMode & @CRLF & $cliParamName & @CRLF & $cliParamDisplayName & @CRLF & $cliParamDescription & @CRLF & $cliParamPath)
Edited by redrider81

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  

×