gb.args - alternate form of usage.

Post your Gambas programming questions here.
Post Reply
User avatar
casperl
Posts: 11
Joined: Wednesday 1st February 2017 10:07am

gb.args - alternate form of usage.

Post by casperl »

Greetings Everyone!

Benoït has an excellent explanation on using gb.args in this thread:

http://gambas.8142.n7.nabble.com/New-co ... 31829.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: Select all

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
User avatar
casperl
Posts: 11
Joined: Wednesday 1st February 2017 10:07am

Re: gb.args - Something I forgot...

Post by casperl »

I forgot to include this in the above...

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: Select all

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
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: gb.args - alternate form of usage.

Post by cogier »

I have looked at your code in your first post and if you need all the options then the code you have written looks good to me. To reduce the amount of code you could put all the 'Args' into a single string and use InStr() to look in the string for the 'Arg'. This method would catch '--version' or '-version' etc.

Code: Select all

	 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.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: gb.args - alternate form of usage.

Post by jornmo »

Well, you will lose the benefit of getting useful information from using --help and you are also only getting the long option names. The last can easily be solve by adding
Select strTemp
        Case "--help", "-h"
          DisplayHelp()
        [...]
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: gb.args - alternate form of usage.

Post by jornmo »

@cogier: if you want to convert an array to a string, you can always use .Join. No need to use a loop :)
User avatar
casperl
Posts: 11
Joined: Wednesday 1st February 2017 10:07am

Re: gb.args - alternate form of usage.

Post by casperl »

@jornmo, I have done exactly what you have suggested.

Code: Select all

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.
User avatar
casperl
Posts: 11
Joined: Wednesday 1st February 2017 10:07am

Re: gb.args - alternate form of usage.

Post by casperl »

@cogier
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: Select all

command --argument value 
as in

Code: Select all

$ myclicmd --foo bar
would return the value "bar" by accessing Args[3] (or Args[4]...) for multiple values. The coded example is a bit verbose, but it serves the purpose of just having a single function to call anywhere else in the program to access whatever value was passed to the the --parameter. Since this is an important part of making the alternate usage of gb.args functional, I felt I had to mention this.
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: gb.args - alternate form of usage.

Post by cogier »

jornmo wrote:@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: Select all

Dim arrParams As String[]
  Dim strParam As String
  
  arrParams = Args.All
could have been simplified with

Code: Select all

Dim arrParams As String[] = Args.All
  Dim strParam As String
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: gb.args - alternate form of usage.

Post by jornmo »

If you want to do it all in one line
Dim s As String = Args.All.Join(";", "[]")
Post Reply