Merge branch 'arg-update' into 'master'

gb.args: allow override of version and help.

See merge request gambas/gambas!189
This commit is contained in:
Benoît Minisini 2021-02-24 00:51:28 +00:00
commit ef4493c460
4 changed files with 59 additions and 20 deletions

View file

@ -1,4 +1,4 @@
[Component]
Key=gb.args
Version=3.14.90
Version=3.15.90
Authors=Benoît Minisini

View file

@ -1,10 +1,11 @@
# Gambas Project File 3.0
Title=gb.args
Startup=MMain
Version=3.14.90
Version=3.15.90
VersionFile=1
Authors="Benoît Minisini"
Arguments=[["-V","-p","toto","zozo","-s","512","--","project"]]
Arguments=[["-V","-p","toto","zozo","-s","512","--","project"],["--help"]]
CurrentArgumentList=["--help"]
TabSize=2
Translate=1
Language=en

View file

@ -6,6 +6,8 @@ Private $cArg As Collection
Private $aRest As Integer[]
Private $aHelp As String[]
Private $sUsage As String
Private $bManualVersion As Boolean
Private $bManualHelp As Boolean
Private Sub Abort(sErr As String)
@ -70,21 +72,18 @@ Public Sub End() As String[]
Dim aRest As New String[]
Dim I As Integer
If Has("V", "version", ("Display version")) Then
Print Application.Version
Quit
If Not $bManualVersion Then
If Has("V", "version", ("Display version")) Then
Print Application.Version
Quit
Endif
Endif
If Has("h", "help", ("Display this help")) Then
If $sUsage Then
Print $sUsage
Else
Print Subst(("Usage: &1 <options> <arguments>"), Application.Title)
If Not $bManualHelp Then
If Has("h", "help", ("Display this help")) Then
HelpText(True)
Quit
Endif
Print
Print ("Options:")
Print $aHelp.Join("\n")
Quit
Endif
If $cArg.Count Then
@ -101,6 +100,31 @@ Public Sub End() As String[]
End
'' Get the default help message.<br>Must be used **after** Args.Begin()\
'' Returns the message text and will Print to stdout if boolean arg PrintText is True.
Public Sub HelpText(Optional PrintText As Boolean) As String
If Not $aHelp Then
Abort(("Error: Args.Helptext() must be called after the command Args.Begin()"))
Endif
Dim sVar As String
If $sUsage Then
sVar &= $sUsage & "\n"
Else
sVar &= Subst(("Usage: &1 <options> <arguments>"), Application.Title) & "\n"
Endif
sVar &= "\n" & ("Options:") & "\n"
sVar &= $aHelp.Join("\n")
If PrintText Then Print sVar
Return sVar
End
Private Sub GetKey(ShortName As String, LongName As String) As String
Dim sKey As String
@ -140,6 +164,12 @@ Public Sub Has(ShortName As String, LongName As String, Optional Description As
AddHelp(ShortName, LongName, Description)
If ShortName = "V" Then
$bManualVersion = True
Else If ShortName = "h" Then
$bManualHelp = True
Endif
vShort = $cArg["-" & ShortName]
vLong = $cArg["--" & LongName]
@ -152,18 +182,18 @@ Public Sub Has(ShortName As String, LongName As String, Optional Description As
End
Public Sub Get(ShortName As String, LongName As String, Optional Description As String, Optional ArgName As String) As String
Public Sub Get(ShortName As String, LongName As String, Optional Description As String, Optional ArgName As String, Optional {Default} As String) As String
Dim sKey As String = GetKey(ShortName, LongName)
Dim vShort, vLong As Variant
If Not sKey Then Return
If Not sKey Then Return {Default}
AddHelp(ShortName, LongName, Description, ArgName)
vShort = $cArg["-" & ShortName]
vLong = $cArg["--" & LongName]
If IsNull(vShort) And If IsNull(vLong) Then Return ""
If IsNull(vShort) And If IsNull(vLong) Then Return {Default}
If TypeOf(vShort) = gb.Boolean Then Abort(Subst("argument missing for option -&1", ShortName))
If TypeOf(vLong) = gb.Boolean Then Abort(Subst("argument missing for option --&1", LongName))
@ -182,6 +212,8 @@ Public Sub Get(ShortName As String, LongName As String, Optional Description As
Return Args[vLong]
Endif
Return {Default}
End
Public Sub GetInteger(ShortName As String, LongName As String, Optional Description As String, Optional ArgName As String, Optional {Default} As Integer) As Integer

View file

@ -3,13 +3,19 @@
Public Sub Main()
Dim iStack As Integer
Args.Begin() '"Usage: gbx3 <options> <project>\n gbx3 -e <expression>")
Args.Has("k", "no-unload", "Do not unload shared libraries")
Args.Get("p", "profile", "Activate profiling mode", "path")
iStack = Args.GetInteger("s", "stack-size", "Stack size [256]", "kilobytes", 256)
If Args.Has("h", "help", "custom help") Then
Print "I CAN PREFIX....."
Print Args.HelpText()
Print "I CAN SUFFIX..."
Endif
Print Args.End().Join(",")
Print iStack