Page 1 of 1

Getting output from purebasic.exe (/help, /build, /version)

Posted: Thu Apr 28, 2011 4:50 pm
by helpy
See PureBasic help: "Commandline options for the IDE"
General options:

Code: Select all

  /VERSION                 displays the IDE version and exits
  /HELP or /?              displays a description of the commandline arguments
Options for launching the IDE:

Code: Select all

  /P <Preferences file>    loads/saves all the configuration to/from the given file
  /T <Templates file>      loads/saves the code templates from/to the given file
  /A <tools file>          loads/saves the configuration of the external tool from/to this file
  /S <Source path>         overwrites the "Source path" setting from the preferences
  /E <Explorer path>       starts the Explorer tool with the given path
  /L <Line number>         moves the cursor to the given line number in the last opened file
  /NOEXT                   disables the registering of the .pb extension in the registry
  /LOCAL                   puts all preferences in the PureBasic directory instead of the user profile location
  /PORTABLE                the same as /LOCAL and /NOEXT combined
Options for building projects:

Code: Select all

  /BUILD <file>            specifies the project file to build
  /TARGET <target>         specifies the target to build (the default is to build all targets)
  /QUIET                   hides all build messages except errors
  /READONLY                does not update the project file after compiling (with new access time and build counters)
If I enter "purebasic.exe /help" in the command window nothing is shown!
==> a second command window is opened and immediately closed!

If I use the option "/BUILD <project file>" the same happens.
==> a second command window is opened and immediately closed!
==> I can not read the messages (information, warnings, errors, ...) because the window is immediately closed when purebasic finishes!

Can someone confirm this behavior?

cu,
guido

Re: Using commandline: purebasic.exe /help

Posted: Thu Apr 28, 2011 10:20 pm
by c4s
I think this is normal. You would have to run PureBasic using an already opened command prompt to read the help text.

Re: Using commandline: purebasic.exe /help

Posted: Fri Apr 29, 2011 3:54 pm
by freak
This is a general problem in Windows with console applications. You can either compile with the console flag then you always have a console window, or you can compile without it, then running from the commandline produces this behavior. There is nothing that can be done about that, sorry.

You can catch the console output by redirecting to a file using something like "purebasic.exe /help > test.txt".

Re: Using commandline: purebasic.exe /help

Posted: Fri Apr 29, 2011 5:26 pm
by helpy
Hi freak,

Danke für den Tipp !

Aber leider ist die erzeugte Textdatei leer!

Ich hatte das eigentlich für einen BUILD-Prozess schon probiert, damit ich zumindest die Meldung speichern und dann mit "type build-output.txt" anzeigen kann.

Noch eine Frage:
Gibt "purebasic.exe /BUILD" eigentlich einen exit-Code ungleich Null zurück, wenn der Compiler einen Fehler meldet?
Damit könnte man in einer Batch-Datei mit "if %errorlevel% NEQ 0 goto :CompilerError" den Fehler abfangen und entsprechend reagieren (z.B. kein Setup erzeugen und stattdessen die Fehlermeldungen ausgeben, wenn das Umleiten in eine Textdatei funktionieren würde).

lg, guido

Re: Using commandline: purebasic.exe /help

Posted: Fri Apr 29, 2011 8:44 pm
by helpy
Oh sorry ... it happend again, that I wrote in my native language here in the english forum!
Here my translation:

Hi freak,

Thank you for the tip!

I tried it before and tested it with /help again, but the text file is empty!

I tested this already with the /BUILD option, in order to save the compiler messages and display it with "type build-output.txt".

An additional question:
If I call "purebasic.exe /BUILD ...", does it return an exit-Code unequal to zero, if the compiler returns error messages?
If this would be the case, I could catch the error with "if %errorlevel% NEQ 0 goto :CompilerError" ... and then it would be possible to react accordingly to the result (for instance: does not create a setup but display the error messages instead, if the redirection of in to a file would work).

cu,
guido

Re: Getting output from purebasic.exe (/help, /build, /versi

Posted: Mon May 09, 2011 1:24 pm
by helpy
Hello,

If purebasic.exe is called with some options (/help, /version, /build), it does output messages to the standard output.

Problems:
  1. If purebasic.exe is called from a windows console window (cmd.exe), purebasic.exe opens its own console window, which is closed very quickly!
  2. It is not possible to redirect the output to a text file (purebasic.exe /help >help.txt). Only an empty textfile is created!
Solution: I wrote a little console application, which passes the arguments to purebasic.exe, catches the output and writes this output to the console window.

I compiled this application as PureBasic.com and copied it in to the same directory as PureBasic.exe.
This way I can use it from the windows command line and will get all outputs!

Here is the source:

Code: Select all

EnableExplicit

;{ Procedure declarations
Declare Main()
Declare PrintProgramOutput( Program, *bError.Integer = #Null )
;}
;{ Constants
#APP_TITLE = "PureBasic.com"
#APP_CONSOLE_COLOR_DEFAULT_FG = 7
#APP_CONSOLE_COLOR_DEFAULT_BG = 0
#APP_CONSOLE_COLOR_DEBUG_FG   = 8
#APP_CONSOLE_COLOR_DEBUG_BG   = 0
#APP_CONSOLE_COLOR_ERROR_FG   = 12
#APP_CONSOLE_ERROR_DEBUG_BG   = 0

#APP_EXITCODE_NOERROR         = 0
#APP_EXITCODE_NOCONSOLE       = 255
#APP_EXITCODE_IDE_MISSING     = 254
#APP_EXITCODE_IDE_ERRORS      = 253
#APP_EXITCODE_IDE_NOTSTARTED  = 252

#APP_SEPARATOR = ">>>>"
;}
;{ Macros
Define DBG_ConsoleForeground, DBG_ConsoleBackground
Macro DBG( msg )
	CompilerIf #PB_Compiler_Debugger
		ConsoleColor( #APP_CONSOLE_COLOR_DEBUG_FG, #APP_CONSOLE_COLOR_DEBUG_BG )
		PrintN( "DBG> " + msg )
		ConsoleColor( #APP_CONSOLE_COLOR_DEFAULT_FG, #APP_CONSOLE_COLOR_DEFAULT_BG )
	CompilerEndIf
EndMacro
Macro WAIT( msg="Press ESC to continue", key=#ESC$, exitcode=#Null )
	CompilerIf #PB_Compiler_Debugger
		PrintN("")
		DBG( msg )
		PrintN("")
		While Inkey() <> key
			Delay(100)
		Wend
		If exitcode : ProcedureReturn exitcode : EndIf
	CompilerEndIf
EndMacro
Macro ErrorN( msg )
	ConsoleColor( #APP_CONSOLE_COLOR_ERROR_FG, #APP_CONSOLE_ERROR_DEBUG_BG )
	ConsoleError( msg )
	ConsoleColor( #APP_CONSOLE_COLOR_DEFAULT_FG, #APP_CONSOLE_COLOR_DEFAULT_BG )
EndMacro
;}

End Main()

Procedure Main()
	Protected PurePath.s, IDE.s
	Protected Arg.s, iArg, bConsole = #False
	Protected AllArgs.s
	Protected prgHandle, prgID, prgError, msgError.s, prgExitCode
	Protected myExitCode = #APP_EXITCODE_NOERROR
	
	If Not OpenConsole()
		MessageRequester( #APP_TITLE, "Could not open console window!", #MB_ICONERROR )
		ProcedureReturn #APP_EXITCODE_NOCONSOLE
	EndIf
	
	PrintN( #APP_TITLE + " (call PureBasic.exe and prints its output to console window)" )
	
	CompilerIf #PB_Compiler_Debugger
		PurePath = #PB_Compiler_Home
	CompilerElse
		PurePath = GetPathPart( ProgramFilename() )
	CompilerEndIf
	DBG( "PurePath = " + PurePath )
	
	IDE = PurePath + "PureBasic.exe"
	DBG( "IDE Path = " + IDE )
	
	If FileSize( IDE ) < 0
		ErrorN( "PureBasic.exe does not exist in directory " + PurePath )
		WAIT( "Press ESC to finish " + #APP_TITLE )
		ProcedureReturn #APP_EXITCODE_IDE_MISSING
	EndIf
	
	For iArg = 0 To CountProgramParameters() - 1
		Arg = ProgramParameter( iArg )
		If (Left(Arg,1) = "-" Or Left(Arg,1) = "/") And Len(Arg) > 1
			Arg = LCase( Right( Arg, Len(Arg)-1 ) )
			Select Arg
				Case "?" : bConsole = #True
				Case "help" : bConsole = #True
				Case "version" : bConsole = #True
				Case "build" : bConsole = #True
			EndSelect
		EndIf
	Next iArg
	
	AllArgs = ""
	For iArg = 0 To CountProgramParameters() - 1
		AllArgs + ProgramParameter(iArg) + " "
	Next iArg
	AllArgs = Trim(AllArgs)
	
	If Not bConsole
		PrintN( "IDE PureBasic.exe is called in standard mode!" )
		DBG( "Call PureBasic.exe " + AllArgs )
		RunProgram( IDE, AllArgs, "" )
	Else
		PrintN( "PureBasic.exe is called in console mode!" )
		DBG( "Call PureBasic.exe " + AllArgs )
		prgHandle = RunProgram( IDE, AllArgs, "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | #PB_Program_Error )
		
		If prgHandle
			PrintN( #CRLF$ + #APP_SEPARATOR + " PureBasic.exe started successfully " + #APP_SEPARATOR + #CRLF$ )
			While ProgramRunning(prgHandle)
				If Not PrintProgramOutput(prgHandle, @prgError)
					Delay(1)
				EndIf
			Wend
			PrintProgramOutput(prgHandle, @prgError)
			prgExitCode = ProgramExitCode(prgHandle)
			CloseProgram( prgHandle )
			PrintN( #CRLF$ + #APP_SEPARATOR + " PureBasic.exe finished execution " + #APP_SEPARATOR + #CRLF$ )
			
			If prgExitCode
				ErrorN( "PureBasic.exe finished with exit code: " + Str(prgExitCode) )
				myExitCode = prgExitCode
			ElseIf prgError
				ErrorN( "PureBasic.exe finished with errors" )
				myExitCode = #APP_EXITCODE_IDE_ERRORS
			Else
				PrintN( "PureBasic.exe finished without errors!" )
				myExitCode = #APP_EXITCODE_NOERROR
			EndIf
		Else
			ErrorN( "PureBasic.exe could not be started!" )
			myExitCode = #APP_EXITCODE_IDE_NOTSTARTED
		EndIf
	EndIf
	
	WAIT( "Press ESC to finish " + #APP_TITLE )
	CloseConsole()
	ProcedureReturn myExitCode
EndProcedure

Procedure PrintProgramOutput( Program, *bError.Integer = #Null )
	Protected msgError.s, bPrintedOutput
	While AvailableProgramOutput(Program)
		PrintN( ReadProgramString(Program) )
		bPrintedOutput = #True
	Wend
	Repeat
		msgError = ReadProgramError(Program)
		If msgError <> ""
			ErrorN( msgError )
			bPrintedOutput = #True
			If *bError : *bError\i = #True : EndIf
		Else
			Break
		EndIf
	ForEver
 	ProcedureReturn bPrintedOutput
EndProcedure
Maybe someone can use it.

cu, guido