Jump to content

Can a function be effectively turned on or off based on variable state of the script?


Recommended Posts

As with a lot of programming, the less manual repetition done to achieve the same outcome...the better.

I package programs for network deployment at the organization I work for. Sometimes these applications require verbose messaging to the user, and sometimes by special request we have to turn that off and deploy silently.

The way I currently handle these 2 scenarios is this.

I have a function that builds the splashwin display screen to variable size depending on message length, and displays the desired message to the end user.

Then at each point throughout my script as I need to make the user aware of what is currently happening, I inject a splashwin function call with the unique message pertaining to that specific event. ie. "Installing Microsoft Office, please wait..."

In effort to build a more universal script to handle any type of request submitted. I've incorporated a switch command of '/silent'

If cmdlineraw detects the usage of that switch when the script is triggered it sets a variable flag, ie. $silent = "on"
Then at every splashwin call I make, I'm prefixing it with an IF statement that checks for the flag, and then does NOT display the message if that flag is "on". Or does display it if its not. Has worked for me just fine. But while I grow my programming skills and look for more streamlined ways of handling areas I find myself creating repetition. I'm curious if I can relocate that flag check. Taking it out of every single splashwin call, and injecting it into the function itself.

Example of how I currently display or hide the splashwin based on command line...

If StringInStr($cmdlineraw, "/silent", 0) Then
    $SilentSwitch = "ON"
EndIf

If $SilentSwitch = "OFF" Then SplashWin($Uninstalling & $ProductInfo[1][0])

Some scripts have dozens of those splashwin calls littered throughout, so Is this the best approach I can take already? Or as stated earlier, can I pull the IF statement away from the splashwin call. And left the function as a whole either activate or deactivate as a result of the command line check.

Link to comment
Share on other sites

Hi.

First make $SilentSwitch global and set it depending on $cmdlineraw. Then write your Func _SplashWin($sMessageToShow).

Inside the Func first you prove if $SilentSwitch is On Then Return Else show message.

Conrads

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
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
 Share

×
×
  • Create New...