gb.args - alternate form of usage.
Posted
#1
(In Topic #107)
Trainee
Benoït has an excellent explanation on using gb.args in this thread:
http://gambas.8142.n7.nabble.com/New-component-gb-args-td31829.html
However, my mind is wrapped around the way command line arguments are used in most Ruby libraries. Simply because I am used to it, I use gb.Args in a different way, and it works. Now whenever I am swimming against the flow, I am wondering whether I am doing the right thing. Any comments from the experienced coders on this?
Code
Public Sub Main()
Dim strTemp As String
If Args.Count > 1
For Each strTemp In Args
Select strTemp
Case "--help"
DisplayHelp()
Case "--version"
DisplayVersion()
Case "--tmux"
ExecTmuxStartTranslate()
Case "--chdir"
HandleChdir()
Case "--term"
CatFileGrep(strTerminologyFile)
Case "--trans"
CatFileGrep(strTranslateFile)
Case "--test" ' for running arbitrary testing code
DoTest()
Case Else
Print "Unknown parameter: " & strTemp
End Select
Next
Else
Print "No parameters provided" ' this is useful to run the prog within the IDE to find syntax errors before compiling an executable to be executed in a shell
Endif
End
Posted
Trainee
I access the value of the parameter through a function that is used in the routines called in the case loop. Here is the code for that:
Code
Public Sub GetStrParam() As String
Dim arrParams As String[]
Dim strParam As String
arrParams = Args.All
If arrParams.Count = 3 Then strParam = arrParams[2] Else strParam = "NIL"
Return strParam
End
Posted
Guru

Code
Dim strTemp, sAllArgs As String
For Each strTemp In Args
sAllArgs &= strTemp
Next
If InStr(sAllArgs, "help") Then DisplayHelp()
If InStr(sAllArgs, "version") Then DisplayVersion()
...
Sorry but I could not work out the purpose of your second post.
Posted
Regular

Posted
Regular

Posted
Trainee
Code
Case "--help", "-h"However, I have many CLI scripts that call other scripts in turn. When developing custom CLI scripts it is ridiculous how often the short version is simply impractical since numerous commands will share the same first letter of the alphabet. I now tend to use the short version only for frequently used commands that I have to type, and use the long versions in cases where a script will call another script.
Posted
Trainee
The second post was to illustrate how to get the value passed to the parameter in a CLI command. For example,
Given the CLI command convention:
Code
command --argument value
Code
$ myclicmd --foo bar
Posted
Guru

jornmo said
@cogier: if you want to convert an array to a string, you can always use .Join. No need to use a loop![]()
Thanks jornmo for that. I also noticed: -
Code
Dim arrParams As String[]
Dim strParam As String
arrParams = Args.AllCode
Dim arrParams As String[] = Args.All
Dim strParam As String
Posted
Regular

1 guest and 0 members have just viewed this.


