Jump to content

Parsing Command-line Args


Recommended Posts

I'm writing some scripts that will take command-line parameters. For example:

myScript.exe  [msDelay] {/INCLUDE | /EXCLUDE} -E listOfFileExtensions

Do any of you experts have advice / sample scripts / notation guidelines?

Thanks

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

  • Developers

This is a portion of a utility i use to install/configure SUS and it takes several commandline options.. maybe it give s you some food for thought.. :whistle:

$install = 0  ; switch for client install
$Force = 0      ; switch for forcing download of patches and installation/reboot
$Batch = 0     ; indicator for asking confirmation
$Status = 0     ; indicator for Showing SUS status
$Susserver = ""; Sus server name
$Password = ""; PSW for autologon after reboot
;
$V_Arg = "Valid Arguments are:" & @LF
$V_Arg = $V_Arg & "    /batch   - don't prompt for anything.. fail is something is missing." & @LF
$V_Arg = $V_Arg & "    /install - Install the Client and Update Registry." & @LF
$V_Arg = $V_Arg & "    /force   - Force Software downloads and installation/reboot." & @LF
$V_Arg = $V_Arg & "    /s NAME  - Target Susserver." & @LF
$V_Arg = $V_Arg & "    /p Password for userid " & EnvGet("USERNAME") & ", used for autologon after reboot."  & @LF
$V_Arg = $V_Arg & "    /reset   - Removes the local SUS information, pc will go to microsoft for updates." & @LF
; retrieve commandline parameters
For $x = 1 to $CmdLine[0]
   Select
      Case $CmdLine[$x] = "/batch"
         $Batch = 1
         writelog(" -- Running in Batch mode.")
      Case $CmdLine[$x] = "/install"
         $install = 1
      Case $CmdLine[$x] = "/force"
         $Force = 1
      Case $CmdLine[$x] = "/reset"
         RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate", "")
         MsgBox( 1, "SUS Utility", "Windows Update setting are reset.", 5)
         Exit
      Case $CmdLine[$x] = "/s"
         $x = $x + 1
         $Susserver = $CmdLine[$x]
      Case $CmdLine[$x] = "/p"
         $x = $x + 1
         $Password = $CmdLine[$x]
      Case $CmdLine[$x] = "/?" Or $CmdLine[$x] = "/h" Or $CmdLine[$x] = "/help"
         MsgBox( 1, "SUS Utility", "" & $v_Arg,)
         Exit
      Case Else
         MsgBox( 1, "SUS Utility", "Wrong commandline argument: " & $CmdLine[$x] & @LF & $v_Arg,)
         Exit
   EndSelect
Next

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I also use a similar method as JdeB. It seems most easy to have a global flag representing an argument, then set that flag to a true condition by iterating through the arguments in a loop while comparing them in a case structure.

Link to comment
Share on other sites

I share JdeB and Valik advise.

I can just add that It is uncommon to see /keyword mixed with - in the same syntax

/ come from windows

- from unix

in general under windows - are considered as /

that just perfectionism :whistle:

Link to comment
Share on other sites

Well,

Now that we have the beautiful Call() function,

Name your functions the same as the possible parameter values.

Then you just have to write..

Call($CmdLine[$2]) ;  or whatever index you want

at least one less SELECT structure to write

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

Link to comment
Share on other sites

Well,

Now that we have the beautiful Call() function,

Name your functions the same as the possible parameter values.

Then you just have to write..

Call($CmdLine[$2]);  or whatever index you want

at least one less SELECT structure to write

That has too much potential for problems. The Call function is dangerous in that you may attempt to call a function that doesn't exist. In C, when using pointers to functions (Similar to Call), a good programmer will at least check to make sure the pointer actually points to something before using it, and if not, properly handle that situation, too. AutoIt doesn't have that capability though, making Call a source for trouble.

P.S. Always remember, even though you may know how to properly use your programs, somebody else may not, and so you have to try to account for what they may try to do to your program, even if that isn't anything somebody should want to try. It's for that reason that I think using Call is dangerous.

Link to comment
Share on other sites

You are absolutely right Valik,

I just assumed that CyberSlug would check the @error condition after that line, and didn't bother writting it in.

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...