Jump to content

Recommended Posts

Posted (edited)

I have a compiled script that displays a message box with usage information if a user starts it without a parameter.

It is a GUI app, but, if a user starts it from the command line with a switch like "/?" or "-h" or "--help", I would like it to print a usage message in the CMD window instead of popping up a message box. I know how to parse the command line - I'm only asking how to send output to the CMD window.

Is this possible with AutoIt? I don't want to compile the script as a CLI application, only to send output to the Windows console if it detects the "/?" switch (or something similar) on the command line.

 

Edited by emendelson
Posted

Thank you for these. ConsoleWrite() doesn't do the job. I thought of using the second method you mentioned - that is, open a second CMD window and echo the message - but that isn't convenient for a user (it opens a second CMD window), so I'm hoping there's a method that would let the compiled script write the output to the command shell that started it.  

Meanwhile, thank you for those ideas!

Posted

Nine's method certainly works. Here is another one that may be clumsy, but only clutters the screen with one extra line of text:

 

Func CmdWindowHelpMessage()
    Opt("WinTitleMatchMode", 2)
    If WinActive("cmd.exe") Then
        _FileCreate(@TempDir & "\usagetxt.bat")
        Local $u = FileOpen(@TempDir & "\usagetxt.bat", 1)
        FileWrite($u, "@echo off" & @CRLF & "echo." & @CRLF)
        FileWrite($u, "echo    Usage: MyExecutableScript.exe d:\path\filename" & @CRLF & "echo." & @CRLF)
        FileWrite($u, "echo    [/option=OPTION] [/choice=CHOICE] [/string=STRING]" & @CRLF)
        FileClose($u)
        Opt("SendKeyDelay", 0)
        Send(@TempDir & "\usagetxt.bat {ENTER}")
        ; Sleep(20)
        ; FileDelete(@TempDir & "\usagetxt.bat")
        Exit
    EndIf
EndFunc   ;==>HelpMessage

You could add CLS to the top of the batch file, but that seems a bit impolite.

If there's a better way to do this, I'll be grateful

Posted

Yes I was thinking the same, but I would strongly suggest to use ControlSend as it is way more robust and using the class, you can have the same script with multiple language : french, english, etc.  :

Opt("SendKeyDelay", 0)
;Run("cmd.exe")
Local $handle = WinGetHandle("[CLASS:ConsoleWindowClass]")
If Not $handle Then Exit MsgBox (0,"","Console not found")
WinActivate ($handle)
Local $hFile = FileOpen ("help.cmd", $FO_OVERWRITE)
FileWrite ($hFile, _
  "@echo off" & @CRLF & _
  "echo this is the first line of explaination" & @CRLF & _
  "echo this is the second line of explaination" & @CRLF & _
  "echo this is the third line of explaination" & @CRLF & _
  "echo this is the fourth line of explaination" & @CRLF & _
  "echo this is the fifth line of explaination" & @CRLF )
FileClose ($hFile)
ControlSend($handle, "", "", "help" & @CRLF)
Sleep (500)
FileDelete ("help.cmd")

and calling the script help, is more appropriate for the user ;)

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
  • Recently Browsing   0 members

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