[Gambas-devel] gb.options

chintan rao chintanraoh at ...176...
Sat Dec 30 12:00:46 CET 2006


Hi

I have made a component using  getopt function

here is the GB_DESC
  GB_DECLARE("GetOptions", sizeof(COPTIONS)),

  GB_METHOD ( "_new", NULL, COPTIONS_new, "(Options)s[(Argument)String[];]"
),
  GB_METHOD ( "_free", NULL, COPTIONS_free, NULL ),
  GB_METHOD ( "_get" ,"v",COPTIONS_get,"(Option)s" ),
  GB_METHOD ( "NextOption","s",COPTIONS_next,NULL ),
  GB_METHOD ( "OptionArgument","s",COPTIONS_opt_arg,NULL ),
  GB_METHOD ( "RestOfArgs","String[]",COPTIONS_rest,NULL ),
  GB_METHOD ( "GetErrorOption","s",COPTIONS_invalid,NULL ),
  GB_METHOD ( "GetArgument","String[]",COPTIONS_getarg,"(Option)s"),

  GB_PROPERTY_READ ("CommandLineArgs","String[]",COPTIONS_cmdline),

I have made it non-static which i think is right(hopefully).

NextOption works like getopt function.
OptionArgument is  optarg.
GetErrorOption is optopt.
RestOfArgs gives the remaining arguments after running getopt.

_get function does not follow getopt template
for example

DIM opt AS NEW GetOptions("h:elo") if argument not specify assumes agument
as command line options

Options["h"] -> returns the last argument associated with 'h' if any
othewise returns 0
Options["e"] -> returns 1 if the 'e' option is specified, and 0
otherwise.
Options["z"] -> returns 0

opt.GetArgument("h") returns all the arguments assosiated with 'h'

opt.CommandLineArgs returns the command line options

Example 1(getopt style)

' Gambas module file

PUBLIC SUB Main()

  DIM opt AS NEW GetOptions(":h:elo")
  DIM c AS String
  PRINT "command line args:\"" & opt.CommandLineArgs.Join(" ") & "\""

    c = opt.NextValidOption()
    PRINT opt.OptionArgument()

    IF c = ""
      BREAK
    ENDIF
    SELECT CASE c
      CASE "h"
        PRINT "option h" & " the corresponding argument is\"" &
opt.OptionArgument() & "\""
      CASE "e"
        PRINT "option e"
      CASE "l"
        PRINT "option l"
      CASE "o"
        PRINT "option o"
      CASE "?"
        PRINT "error parsing the arguments"
        PRINT "invalid option " & opt.GetErrorOption()
      CASE ":"
        PRINT "error parsing the arguments"
        PRINT "no arguments to option " & opt.GetErrorOption()
    END SELECT
  WEND

  IF opt.RestOfArgs().Count <> 0
    PRINT "Rest of the arguments are \"" & opt.RestOfArgs().Join(" ") & "\""
  ENDIF

END

Example 2 (using "_get")

' Gambas module file

PUBLIC SUB Main()
  DIM opt AS NEW GetOptions("hevo:")

  IF opt["?"] = 1
    PRINT "error parsing the arguments"
  ENDIF

  IF opt["h"] = 1
    PRINT "Help"
  ENDIF

  IF opt["e"] = 1
    PRINT "Execute"
  ENDIF

  IF opt["v"] = 1
    PRINT "Verbose"
  ENDIF

  IF opt["o"] <> 1 AND opt["o"] <> 0
      PRINT "Ouput to " & opt["o"]
  ENDIF

END

I wonder should i return all the valid options found as a String[] all at
once

Hoping this interface is alright
If there should be a static interface i think it should be for command line
arguments in particular

Chintan Rao H
OPEN at ...549...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/devel/attachments/20061230/0eadae10/attachment.html>


More information about the Devel mailing list