[Gambas-devel] gb.option What it is used for?
chintan rao
chintanraoh at ...176...
Fri Sep 26 09:49:16 CEST 2008
Hi,
Any suggestions for improvement of interface is always welcome.
DIM opt AS NEW GetOptions("la:b:c:")
At this point you may really feel that GetOptions must be static class
( I don't remember why I made it non static. This component right now is in
beta stage :) . )
if colon (ie ":") is specified after a character c, it means that the
option must be be followed by an argument and opt[c] will be the
argument specified after "-c" switch.
if ":" is not specified opt[c] will be 0 or 1 depending on existence
of "-c" switch in the command line argument.
Also opt["?"] is 1 if there is an error in specifying the command line arguments
PUBLIC SUB Main()
DIM opt AS NEW GetOptions("la:b:c:")
IF opt["?"] = 1
PRINT "error parsing the arguments"
ENDIF
IF opt["l"] = 1
PRINT "found option l"
ENDIF
IF opt["a"] <> 1 AND opt["a"] <> 0
PRINT opt["a"]
ENDIF
IF opt["b"] <> 1 AND opt["a"] <> 0
PRINT opt["b"]
ENDIF
IF opt["c"] <> 1 AND opt["c"] <> 0
PRINT opt["c"]
ENDIF
END
Also there is a way in which you can list the opts one by one.
' Gambas module file
PUBLIC SUB Main()
DIM opt AS NEW GetOptions("h:elo")
DIM c AS String
PRINT "command line args:\"" & opt.CommandLineArgs.Join(" ") & "\""
WHILE TRUE
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()
BREAK
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
Regards,
Chintan Rao H
More information about the Devel
mailing list