Displaying help like with "/?"

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Displaying help like with "/?"

Post by StarWarsFan »

Does anybody remember starting programs from the command line and having a short help displayed by running the same program with the parameter "/?"

How do I do just that with my program?
The only solution so far that I have found is openconsole(), but that is undesired, because I do not wish to open an extra console and write output there,
What I want to do is retun text to the already open command window.

What could I use?
A search like https://www.purebasic.fr/english/search ... +line+help has not returned what I wanted or maybe I am using the wrong search words.
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Displaying help like with "/?"

Post by Marc56us »

StarWarsFan wrote: Fri May 06, 2022 4:04 pm Does anybody remember starting programs from the command line and having a short help displayed by running the same program with the parameter "/?"

How do I do just that with my program?
The only solution so far that I have found is openconsole(), but that is undesired, because I do not wish to open an extra console and write output there,
What I want to do is retun text to the already open command window.

What could I use?
A search like https://www.purebasic.fr/english/search ... +line+help has not returned what I wanted or maybe I am using the wrong search words.
I'm not sure I understood the question correctly (I'm using a translator), but maybe this?
GUI version

Code: Select all

If ProgramParameter(0) = "/?"
  MessageRequester("Syntax", "This is Help..." )
EndIf
Console Version

Code: Select all

If ProgramParameter(0) = "/?"
  OpenConsole()
  PrintN(GetFilePart(ProgramFilename()) + " Help")
  PrintN("---")
  PrintN("- Help")
  PrintN("- Help")
  PrintN("- Help")
  PrintN("<Hit Enter>")
  Input()
  CloseConsole()
EndIf
To test in IDE, put /? in compiler option (tab 2)
If the program is compiled as console, OpenConsole will not open another console.
Yes ProgramParameter(0) is the first parameter, not the exe himself like some others language.

But I don't know how to know if a GUI program was launched from the command line or from a click ?

:wink:
Bitblazer
Enthusiast
Enthusiast
Posts: 761
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Displaying help like with "/?"

Post by Bitblazer »

My current application setup uses the basic application name to deliver a help file as ${APPNAME}.chm.

Every application i do, accepts commandline parameters like /? or /help or /config. For example my upcoming project is currently called "compilertool" so it comes with the files
  • compilertool.exe
  • compilertool.chm
  • compilertool.ini
inside compilertool.exe the following gets called if the parameter /? or /help are detected

Code: Select all

      If (FileSize(#COMPILERTOOL_HELPFILE) >= 0)
        RunProgram(#COMPILERTOOL_HELPFILE, "", GetCurrentDirectory())
      Else
        MessageRequester("Error", "The help file '" + #COMPILERTOOL_HELPFILE + "' could not be found / opened", #PB_MessageRequester_Warning | #PB_MessageRequester_Ok)
      EndIf
I am personally using Help&Manual to create the compilertool.chm. It is a compiled html based archive with a index file as manual index.

I think you can create HLP files with free tools like Libre Office or Docmaker too.

I also add a line using AddKeyboardShortcut to assign F1 to launch the helpfile. It is all a bit old school by now, but enough for me. Videolinks and Tutorials will come in the future ;)

This example is done assuming the windows platform. The runprogram use will therefore launch the viewer that the system user prefers for CHM files. For me sumatra pdf will open. Full cross indexing, bookmark support, searchable topic and 100% free.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Displaying help like with "/?"

Post by StarWarsFan »

Both no. Sorry.

I wrote:

"The only solution so far that I have found is OpenConsole(), but that is undesired, because I do not wish to open an extra console and write output there.
What I want to do is retuRn text to the already open command window." (Typo: "R" was missing and maybe confused your translator)
So no newly opened console and no newly window that shows a message requester.

Alright, let me give a more concrete example then.
Given you have installed one of the common compression tools, for the sake of the argument I take 7-zip here.
If you run 7z from the command line, it goes like this:

1. Open a command shell
2. Go to the folder, where 7z is installed
3. Run it with... .\7z.exe --help

What you get is NO extra console and NO extra window, but a help text displayed right to the already open console.
So use the one that already IS open, not create any other new window.

This is what I wish to do.
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Displaying help like with "/?"

Post by Marc56us »

This what I have wrote...
If the program is compiled as console, OpenConsole will not open another console.
For a command line program (like 7z.exe)
(Compiler > Compiler Options > Executable Format > Console),
If the program is launched from a console, OpenConsole() is ignored. No new console windows will be open..
OpenConsole() open a console only if program was launch by GUI ou scheduler.
Try...

Code: Select all

; Compile as command line program (console)
; Compiler > Compiler Options > Executable Format > Console

If ProgramParameter(0) = "/?"
  OpenConsole()
  PrintN(GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + " Help")
  PrintN("---")
  PrintN("- Help")
  PrintN("- Help")
  PrintN("- Help")
  CloseConsole()
Else
  OpenConsole()
  PrintN(GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + " v1.0")
  PrintN("Hello World")
EndIf
PrintN("<Hit Enter>")
Input()
CloseConsole()

Code: Select all

C:\>Console_help
Console_help v1.0
Hello World
<Hit Enter>


C:\>Console_help /?
Console_help Help
---
- Help
- Help
- Help
<Hit Enter>
:wink:
Or my online translator still hasn't figured out what you want and then I give up. Sorry. :?
IzzyB
New User
New User
Posts: 5
Joined: Sun Oct 21, 2012 5:59 am

Re: Displaying help like with "/?"

Post by IzzyB »

If I have divined your issue correctly, Marc56us is on target.

I suggest you review the documentation.


1) Section: General Libraries, Console, second paragraph. I quote:

If your program is intended to be a pure console application (i.e. not a GUI application which sometimes opens a console) then you must remember to set the executable format to 'Console' when you are compiling your programs.


2) Section: General Libraries, Process, ProgramParameter() and CountProgramParameters(). I Note:

In PB parsing of the command tail, and acting upon any switches and/or parameters supplied is the responsibility of the programmer. Your code needs to examine the command tail and execute (here meaning display help) as you see fit.


3) Section: The PureBasic IDE, Compiling your programs, Compiler options for non-project files, Executable format

Indicates where you tell the compiler to construct a Console mode executable when working within the IDE.


4) Note also, for possible extra credit: Under: Various Topics: Compiler Directives: Reserved Constants you will find a discussion of: #PB_Compiler_ExecutableFormat (and friends).

While it will not *set* the executable format from within the source, it can be used to *chastise* you when you fail to set the appropriate compiler option.


In my blundering, the classic symptom of failing to specify the correct compilation option is the 'extra' console window.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Displaying help like with "/?"

Post by ChrisR »

If it can be useful, here is my template for Parsing Command Line Switches
It accept /Letter or -Letter (Letter A,B,..,Z, ?), not case sensitive
Followed by "=" , ":", next Parameter for the Value, or the Switch alone, ex:

Code: Select all

Parsin_CmdLine_Switches /r=abc /h /s:def /t=123 /o "Option" "This is a file"
With the help displayed for the switches "/?", "/h", "/H", "-h ", "-H". In the same way as Marc56 :wink:

Code: Select all

; ------------------------------------------------------------------------------------------------------------------------------------
;        Name: Parsin_CmdLine_Switches.pb
; Description: Parsing Command Line Switches
;      Author: ChrisR
;        Date: 2018-03-18
;      Credit: ebs https://www.purebasic.fr/english/viewtopic.php?t=5731
;          OS: All
;       Forum: 
; ------------------------------------------------------------------------------------------------------------------------------------
; Compile as CmdLine Program (console)
;
; To test with: /r=abc /h /s:def /t=123 /o "Option" "This is a file"   Or With: /h   to Display the Help
; Or with: /r=abc /s:def /t=123 /o "Option" "This is a file"   to Parse Command Line Switches With the Appropriate Process to be added
; ------------------------------------------------------------------------------------------------------------------------------------

EnableExplicit

Structure CmdLineParam
  Switch.s
  Parameter.s
EndStructure
NewList CmdLineParams.CmdLineParam()

Declare Help()
Declare.s CmdLineParameter(Switch.s)

; Display the Help
Procedure Help()
  PrintN(GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + " (v1.0) Program Description")
  PrintN("")
  PrintN(GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + " [/H] [/O]")
  PrintN("")
  PrintN("  /H          Display this help")
  PrintN("  /O          Option Switch")
  PrintN("")
  Print("Press Enter to continue...")
  Input()
  CloseConsole()
  End
EndProcedure

; Return Command Line Parameter for Specified Switch Letter
Procedure.s CmdLineParameter(Switch.s)
  Protected USwitch.s, Param.s
  Shared CmdLineParams()
  
  ; Parse command line if not already done
  If ListSize(CmdLineParams()) = 0
    Param = ProgramParameter()
    Repeat
      If Len(Param)
        AddElement(CmdLineParams())
        Select Left(Param, 1)
          Case "/", "-"
          ; Add Switch to CmdLineParams() Element
            CmdLineParams()\Switch = UCase(Mid(Param, 2, 1))
          ; Add Parameter to CmdLineParams() Element (Ignore "=" and ":" If Present)
            Param = RemoveString(Param, "=")
            Param = RemoveString(Param, ":")
            Param = Mid(Param, 3, Len(Param) - 2)
            If Len(Param)
              CmdLineParams()\Parameter = Param
              Param = ProgramParameter()
            Else
            ; Parameter = Next CmdLine Parameter or Make Parameter a Space If the Switch is Alone to get If CmdLineParameter("?")
              Param = ProgramParameter()
              Select Left(Param, 1)
                Case "/", "-", ""
                  CmdLineParams()\Parameter = " "
                Default
                  CmdLineParams()\Parameter = Param
                  Param = ProgramParameter()
              EndSelect
            EndIf
          Default
          ; One Parameter Without switch Allowed (Can Contain Spaces If in Quotes)
            CmdLineParams()\Switch = ""
            CmdLineParams()\Parameter = Param
            Param = ProgramParameter()
        EndSelect
      EndIf
    Until Len(Param) = 0
  EndIf
  
  ; Search for Specified Command Line Switch Letter
  USwitch = UCase(Switch)
  ForEach CmdLineParams()
    If CmdLineParams()\Switch = USwitch
      ; Switch Found - Return Parameter
      ProcedureReturn CmdLineParams()\Parameter
    EndIf
  Next
  ; Switch Not Found
  ProcedureReturn ""
EndProcedure

Define I

OpenConsole()
If CmdLineParameter("?") : Help()                                  : EndIf
If CmdLineParameter("h") : Help()                                  : EndIf

PrintN(GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + " (v1.0) Program Description")
PrintN("")
If CmdLineParameter("r") : PrintN("/R = " + CmdLineParameter("r")) : EndIf
If CmdLineParameter("z") : PrintN("/Z = " + CmdLineParameter("z")) : EndIf
If CmdLineParameter("s") : PrintN("/S = " + CmdLineParameter("s")) : EndIf
If CmdLineParameter("t") : PrintN("/T = " + CmdLineParameter("t")) : EndIf
If CmdLineParameter("o") : PrintN("/O = " + CmdLineParameter("o")) : EndIf
If CmdLineParameter("h") : PrintN("/H = " + CmdLineParameter("h")) : EndIf
If CmdLineParameter("")
  Print(CmdLineParameter(""))
  For I = 1 To 3
    Print(".")
    Delay(1000)
  Next
EndIf
PrintN("")
CloseConsole()
End

; IDE Options = PureBasic 5.73 LTS (Windows - x64)
; ExecutableFormat = Console
; Folding = -
; EnableXP
; Executable = Parsin_CmdLine_Switches.exe
; CommandLine = /r=abc /h /s:def /t=123 /o "Option" "This is a file"
>

Code: Select all

E:\PureBasic\Exemples\Command Line Switches>Parsin_CmdLine_Switches /r=abc /h /s:def /t=123 /o "Option" "This is a file"
Parsin_CmdLine_Switches (v1.0) Program Description

Parsin_CmdLine_Switches [/H] [/O]

  /H          Display this help
  /O          Option Switch

Press Enter to continue...

E:\PureBasic\Exemples\Command Line Switches>Parsin_CmdLine_Switches /r=abc /s:def /t=123 /o "Option" "This is a file"
Parsin_CmdLine_Switches (v1.0) Program Description

/R = abc
/S = def
/T = 123
/O = Option
This is a file...

E:\PureBasic\Exemples\Command Line Switches>
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Displaying help like with "/?"

Post by StarWarsFan »

IzzyB wrote: Sat May 07, 2022 7:09 pm If I have divined your issue correctly, Marc56us is on target.
No it is not. Unfortunately.
Marc56us wrote: Sat May 07, 2022 5:06 pm If the program is launched from a console, OpenConsole() is ignored. No new console windows will be open..
Exactly that is not the case. I does open an extra console.

It uses OpenConsole(). And that is exactly the opposite of what I need.
You see, if you compile that code and run it, ANOTHER console is opened.
But I want the text output to be on the console that is already open.
Huge difference.

Though I get your point. You say it needs to be compiled as console (compiler setting).
Though that is not desired either, because my program uses Windows and as far as I can tell all that is disabled as soon as you compile as "Executable Formant: Console".

At any rate: I have learned something. So THANK YOU ahead!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Displaying help like with "/?"

Post by StarWarsFan »

So I take it nobody has solved this "dilemma"

EITHER you compile it in console mode -> result you can no longer use windows
OR you compile in in windows mode -> result you can not output text to the EXISTING (=already open) console and it opens ANOTHER console.
Am I correct in that?


So my last question is then:
Can a program that is not compiled as "Executable Formant: Console" not write text output to the console where it is started from?

Is that the case? Is it "either or"?
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Displaying help like with "/?"

Post by Marc56us »

EITHER you compile it in console mode -> result you can no longer use windows
No.

A console application is not a DOS program (will not run from DOS 6)
"DOS" does not exist in Windows (cmd.exe is like virtual machine with some DOS commands)
A console program can launch a graphic program

Compile this as console executable

Code: Select all

OpenConsole()
PrintN("Hello World")

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Repeat : Until WaitWindowEvent(0) = #PB_Event_CloseWindow 

PrintN("<Hit Enter>")

Input()
  • Run it from explorer: this open console, then run GUI
  • Run it from console: This does not open a new console, but write ""Hello World"" on the actual console, then launch GUI, and when you close GUI, this return to console.
This is the normal behavior of the operating system.

If a program is compiled in Windows mode, it does not keep an output to this console, so to write to the console it must open another one.

So, if you want a program that displays text help in the (launch) console and eventually continues in graphics mode, compile it in console mode.
It will work on all versions of Windows (Windows XP, Vista, Seven, 8, 8.1, 10, 11)
Post Reply