Jump to content

Command Line ($CmdLine)


ScottL
 Share

Recommended Posts

Hi-

I've noticed in some of the example scripts on the forum that there is occasionally something that looks like "If $CmdLine[0] > 0 Then", or "If $CmdLine[0] < 2 Then", etc.

I've been trying to understand exactly how and why the command line variable works. I've read through the Help File, but it's still not clicking. I think the problem I'm having is in understanding the anatomy of the command line itself and how it is created.

Would somone being willing to try and explain this to me in plainer english?

Thanks

S

Link to comment
Share on other sites

  • Moderators

Hi-

I've noticed in some of the example scripts on the forum that there is occasionally something that looks like "If $CmdLine[0] > 0 Then", or "If $CmdLine[0] < 2 Then", etc.

I've been trying to understand exactly how and why the command line variable works. I've read through the Help File, but it's still not clicking. I think the problem I'm having is in understanding the anatomy of the command line itself and how it is created.

Would somone being willing to try and explain this to me in plainer english?

Thanks

S

I actually battled over this one myself for a while, this being the first language I had ever sat down to try to understand.

As simple as I can put it, is $CmdLine and $CmdLineRaw catch the Parameters you've sent to that executable when calling it, if any.

So lets say I want my application to only move forward, if it was opened with a parameter passed of "opensaysme".

Compile this below (name it parampass.exe):

If $Cmdline[0] Then
    If $Cmdline[1] <> 'opensaysme' Then
        MsgBox(16, 'Error Wrong Param', 'What the hell are you doing?  You need the right password.')
        Exit - 1
    EndIf
Else
    MsgBox(16, 'Error No Param', 'What the hell are you doing?  You need the right password.')
    Exit - 1
EndIf

MsgBox(64, 'Info', 'If you got this message box, you passed the parameter correctly.' & @CRLF & 'Good Bye!', 5)oÝ÷ Ù8^»§¶¬o(­±é_&Â+at7ºÚ"µÍ[ØZ]
    ÌÎNÉ][ÝÉÌÎNÈ    [ÈÚÝÜ   [È ÌÎNÉÌLÜ[ÜË^I][ÝÈ   ÌÎNÈ [È ][ÝÛÜ[Ø^ÛYI][ÝÊBÙÐÞ
    ÌÎNÓÚÉÌÎNË  ÌÎNÓÝÈÙHÚ[Ü[]Ú]HÜÛÈZÜÚ[ÛËÌÎNÊB[ØZ]
    ÌÎNÉ][ÝÉÌÎNÈ    [ÈÚÝÜ   [È ÌÎNÉÌLÜ[ÜË^I][ÝÈ   ÌÎNÈ [È ][ÝÛÜ[Ø^[YI][ÝÊBÙÐÞ
    ÌÎNÓÚÉÌÎNË  ÌÎNÓÝÈÙHÚ[Ü[]Ú]È[Y]ÈZ[ÈÜÙYÌÎNÊB[   ÌÎNÉ][ÝÉÌÎNÈ    [ÈÚÝÜ   [È ÌÎNÉÌLÜ[ÜË^I][ÝÉÌÎNÊ
You'll notice that we will only be able to open the .exe with the first instance being ran.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks both for the explanations. It's starting to gel a bit, but i still have some questions.

the $CMDLINE parameters would be anything you type AFTER the program like...

PROGRAM.EXE paramater1 parameter2 "Parameter 3 with spaces"

Maybe this is where I'm tripping up. Paramters? If I run something from the command line (which is rare, but happens), it's a flat EXE or it's a file like C:\Program Files\SomeExcelSpreadsheet.xls.

So are you saying the the whole of "C:\Program Files\SomeExcelSpreadsheet.xls" is a single parameter? Anything after the EXE or the file being run would be a parameter?

I actually battled over this one myself for a while, this being the first language I had ever sat down to try to understand.

As simple as I can put it, is $CmdLine and $CmdLineRaw catch the Parameters you've sent to that executable when calling it, if any.

You'll notice that we will only be able to open the .exe with the first instance being ran.

I tried your example and it worked beautifully. I think I'm beginning to understand it. But the idea of parameters is still a little fuzzy to me. In you example, it seemed like it was basically a password for running the program from the command line. I noticed in another script on the example board where it checked to see if one of the parameters was "/decompile" and if it was it retrieved the source code for the program.

So is it too simplistic to say that setting parameters in a program is akin to setting backdoors into things like the source code? I guess I'm now having some trouble understanding the usefulness of parameters.

Any thoughts?

Link to comment
Share on other sites

  • Moderators

I tried your example and it worked beautifully. I think I'm beginning to understand it. But the idea of parameters is still a little fuzzy to me. In you example, it seemed like it was basically a password for running the program from the command line. I noticed in another script on the example board where it checked to see if one of the parameters was "/decompile" and if it was it retrieved the source code for the program.

So is it too simplistic to say that setting parameters in a program is akin to setting backdoors into things like the source code? I guess I'm now having some trouble understanding the usefulness of parameters.

No, it's not a "backdoor", it's simply information being passed from one application to another. The parameter is read by the application to be opened, and the $cmdline[NN] is set up by the programmer on what to do with the values that are passed. Mine was just an example, it could have been just as easy as a number bing passed, and if the number matched something to do one thing in a condition, if it matched something else to do something else.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

C:\Program Files\SomeExcelSpreadsheet.xls

is not a program... a program is an EXE or some other executable type file... 99% of the time it is an EXE...

C:\Program Files\SomeExcelSpreadsheet.xls is a file that an EXE reads... in this case EXCEL.EXE...

so to open C:\Program Files\SomeExcelSpreadsheet.xls from a command line you would run...

EXCEL.EXE "C:\Program Files\SomeExcelSpreadsheet.xls"

and C:\Program Files\SomeExcelSpreadsheet.xls. would be thw "parameter" passed to EXCEL.EXE

LAr.

Okay, I think I'm almost there. The only stumbling block I've got now is that, when I run something from the "Run" dialog, I never input EXCEL.EXE. I just put in "C:\Program Files\SomeExcelSpreadsheet.xls" and off it goes. Is the EXCEL.EXE assumed by the "Run" dialog simply because the "C:\Program Files\SomeExcelSpreadsheet.xls" parameter ends in .XLS?

And if I were to run "C:\Program Files\SomeExcelSpreadsheet.xls" from a command line, in this case the command line we're talking about is ... a DOS prompt?

Thanks for your patience.

S

Link to comment
Share on other sites

This has been very helpful, guys, thanks so much.

I guess my final question would be:

What (if any) is the rule of thumb for using $CmdLine? Is there some kind of standard that AutoIt junkies tend to use in their scripts?

Thanks again.

Link to comment
Share on other sites

  • Moderators

This has been very helpful, guys, thanks so much.

I guess my final question would be:

What (if any) is the rule of thumb for using $CmdLine? Is there some kind of standard that AutoIt junkies tend to use in their scripts?

Thanks again.

Lots of cuss words.

Be careful when passing $Cmdline strings with spaces in them without being wrapped in quotes. Each space will be like a StringSplit() and be treated as individual words/commands. If you need to do such a thing, and get confused, you may look at $CmdLineRaw.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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