Jump to content
Sign in to follow this  
Prab

Command line parameters

Recommended Posts

Prab

Is there a better way to handle this?

If($CmdLine[0] < 1) Then
    $name = InputBox("Workstation", "Input the property tag of a workstation", "")
    If (@error) Then Exit
    Call($method, $name)
Else;has commandline
    For $index = 1 to $CmdLine[0];do all the commands
        If($CmdLine[$index] == "NoPop") Then
            $popup = 0
            ContinueLoop
        EndIf
        If($CmdLine[$index] == "Fast") Then
            $method = "Fast"
            ContinueLoop
        EndIf
        If($CmdLine[$index] == "Hide") Then
            Opt("TrayIconHide", 1)
            ContinueLoop
        EndIf
;allow text files
        If (StringInStr($CmdLine[$index], ".txt")) Then
            $file = FileOpen($CmdLine[$index], 0)
            If $file = -1 Then
                MsgBox(0, "Error", "Unable to open input file.")
                Exit
            EndIf
            While (1)
                $line = FileReadLine($file)
                If @error = -1 Then;end of file
                    ExitLoop
                EndIf
                If($line == "NoPop") Then
                    $popup = 0
                    ContinueLoop
                EndIf
                If($line == "Fast") Then
                    $method = "Fast"
                    ContinueLoop
                EndIf
                Call($method, $line)
            Wend
            FileClose($file)
        Else;allow workstation names
            Call($method, $CmdLine[$index])
        EndIf
    Next
EndIf

It seems like I have lot of redundant code.

Also I would like to have more similar to other programs. For example usually -Fast which would also equal -F, -F=true, -Fast=true, etc. Maybe sombody has made a library that handles this that I could not find when I searched. I appreciate any help.

Edited by Prab

Share this post


Link to post
Share on other sites
PsaltyDS

Is there a better way to handle this?

If($CmdLine[0] < 1) Then
    $name = InputBox("Workstation", "Input the property tag of a workstation", "")
    If (@error) Then Exit
    Call($method, $name)
Else;has commandline
    For $index = 1 to $CmdLine[0];do all the commands
        If($CmdLine[$index] == "NoPop") Then
            $popup = 0
            ContinueLoop
        EndIf
        If($CmdLine[$index] == "Fast") Then
            $method = "Fast"
            ContinueLoop
        EndIf
        If($CmdLine[$index] == "Hide") Then
            Opt("TrayIconHide", 1)
            ContinueLoop
        EndIf
;allow text files
        If (StringInStr($CmdLine[$index], ".txt")) Then
            $file = FileOpen($CmdLine[$index], 0)
            If $file = -1 Then
                MsgBox(0, "Error", "Unable to open input file.")
                Exit
            EndIf
            While (1)
                $line = FileReadLine($file)
                If @error = -1 Then;end of file
                    ExitLoop
                EndIf
                If($line == "NoPop") Then
                    $popup = 0
                    ContinueLoop
                EndIf
                If($line == "Fast") Then
                    $method = "Fast"
                    ContinueLoop
                EndIf
                Call($method, $line)
            Wend
            FileClose($file)
        Else;allow workstation names
            Call($method, $CmdLine[$index])
        EndIf
    Next
EndIf

It seems like I have lot of redundant code.

Also I would like to have more similar to other programs. For example usually -Fast which would also equal -F, -F=true, -Fast=true, etc. Maybe sombody has made a library that handles this that I could not find when I searched. I appreciate any help.

You could use Switch/Case/EndSwitch instead of the nested If/Else/EndIf, and take the repeated part out to a Func of its own:

If ($CmdLine[0] < 1) Then
    $name = InputBox("Workstation", "Input the property tag of a workstation", "")
    If (@error) Then Exit
    Call($method, $name)
Else; has commandline
    For $index = 1 To $CmdLine[0]; do all the commands
        If Not _HandleParameter($CmdLine[$index]) Then
            If (StringInStr($CmdLine[$index], ".txt")) Then
                $file = FileOpen($CmdLine[$index], 0)
                If $file = -1 Then
                    MsgBox(0, "Error", "Unable to open input file.")
                    Exit
                EndIf
                While (1)
                    $line = FileReadLine($file)
                    If @error = -1 Then ExitLoop
                    If Not _HandleParameter($line) Then Call($method, $line)
                WEnd
                FileClose($file)
            Else; allow workstation names
                Call($method, $CmdLine[$index])
            EndIf
        EndIf
    Next
EndIf

Func _HandleParameter($sParameter)
    Switch $sParameter
        Case "NoPop"
            $popup = 0
        Case "Fast"
            $method = "Fast"
        Case "Hide"
            Opt("TrayIconHide", 1)
        Case Else
            Return 0
    EndSwitch
    Return 1
EndFunc  ;==>_HandleParameter

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Prab

Func _HandleParameter($sParameter)

Switch $sParameter

Case "NoPop"

$popup = 0

Case "Fast"

$method = "Fast"

Case "Hide"

Opt("TrayIconHide", 1)

Case Else

Return 0

EndSwitch

Return 1

EndFunc ;==>_HandleParameter

;)

That helps a bunch. I didn't know you could switch on a string. I first learned Java and it can only switch on a int or char. That will help a lot, but it would still be nice to have a library, or I guess in AutoIt they are called UDFs.

Oh, in your example would I have to define $popup and $method as global or is that implied?

Edit: Also is there a difference between using Call("Function Name", "Parameter") and FunctionName("Parameter")?

Thanks again.

Edited by Prab

Share this post


Link to post
Share on other sites
PsaltyDS

That helps a bunch. I didn't know you could switch on a string. I first learned Java and it can only switch on a int or char. That will help a lot, but it would still be nice to have a library, or I guess in AutoIt they are called UDFs.

Oh, in your example would I have to define $popup and $method as global or is that implied?

They have to be declared somewhere. I just assumed it was handled outside of the snippet you posted.

Edit: Also is there a difference between using Call("Function Name", "Parameter") and FunctionName("Parameter")?

No functional difference unless you have to specify the function with a string for some bizarre reason. I wouldn't use Call() unless forced to.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
zfisherdrums

Edit: Also is there a difference between using Call("Function Name", "Parameter") and FunctionName("Parameter")?

There are some diffs; May not be applicable to your scenario, but you're the best judge of that. From the help file remarks for Call():

The function cannot be a built-in AutoIt function or plug-in function.

The function can pass arguments to functions, however, ByRef parameters are not supported; there is no way to retrieve the ByRef parameter.

A special array can be passed in lieu of individual parameters. This array must have it's first element set to "CallArgArray" and elements 1 - n will be passed as seperate arguments to the function. If using this special array, no other arguments can be passed to Call(). See example for a demonstration.

Edited by zfisherdrums

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  

×