gb.args - alternate form of usage.

Post

Posted
Rating:
#1 (In Topic #107)
Avatar
Trainee
Greetings Everyone!

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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Trainee
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

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
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
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

    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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
jornmo is in the usergroup ‘Regular’
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

Code (gambas)

  1. Select strTemp
  2.         Case "--help", "-h"
  3.           DisplayHelp()
  4.         [...]
  5.  

Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
jornmo is in the usergroup ‘Regular’
cogier : if you want to convert an array to a string, you can always use .Join. No need to use a loop :)

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Trainee
jornmo , I have done exactly what you have suggested.

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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Trainee
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

command --argument value
as in

Code

$ 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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘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.All
could have been simplified with

Code

Dim arrParams As String[] = Args.All
  Dim strParam As String
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
jornmo is in the usergroup ‘Regular’
If you want to do it all in one line

Code (gambas)

  1. Dim s As String = Args.All.Join(";", "[]")
  2.  

Online now: No Back to the top
1 guest and 0 members have just viewed this.