options.bi

Summary
options.bi
LicenseCopyright © 2007-2011, FreeBASIC Extended Library Development Group
ext.options
ParserProvides an easy way to process command line arguments.
Functions
addBoolAdds a boolean option to the parser.
addOptionAdds an option to the parser.
parseParses the command line and prepares the results.
hasErrorReturns true if there was an error parsing the command line.
getErrorReturns a string describing the error.
setHelpHeaderSets the text to show before the options list when showHelp is called.
setHelpFooterSets the text to show after the options list when showHelp is called.
showHelpShows the built-in help for all known options.
isSetWas the option passed on the command line?
getArg
getRemainderAll unparsed content from the command line.

License

Copyright © 2007-2011, FreeBASIC Extended Library Development Group

Contains code contributed and Copyright © 2007, mr_cha0s: ruben.nosp@m..coder@gmai.nosp@m.l.com

Distributed under the FreeBASIC Extended Library Group license.  See accompanying file LICENSE.txt or copy at http://code.google.com/p/fb-extended-lib/wiki/License

ext.options

Parser

Provides an easy way to process command line arguments.  Supports short (-c) and long (--command) style arguments with optional and required parameters.

Summary
Functions
addBoolAdds a boolean option to the parser.
addOptionAdds an option to the parser.
parseParses the command line and prepares the results.
hasErrorReturns true if there was an error parsing the command line.
getErrorReturns a string describing the error.
setHelpHeaderSets the text to show before the options list when showHelp is called.
setHelpFooterSets the text to show after the options list when showHelp is called.
showHelpShows the built-in help for all known options.
isSetWas the option passed on the command line?
getArg
getRemainderAll unparsed content from the command line.

Functions

addBool

declare function addBool(byref short_opt as string,  
byref long_opt as string =  "",
byref help_string as string =  "") as integer

Adds a boolean option to the parser.

Parameters

short_optSingle letter version of the option, e.g. c for -c
long_opt(optional) long form version of the option, e.g. copy for --copy
help_string(optional) String to print for this command when the -h or --help options are passed.

Returns

Integer identifier for this option used in isSet and getArg

addOption

declare function addOption(byref short_opt as string,  
byref long_opt as string =  "",
byval has_arg as bool =  false,
byval arg_required as bool =  false,
byval can_repeat as bool =  false,
byref rep_seperator as string =  ";",
byref help_string as string =  "") as integer

Adds an option to the parser.

Parameters

short_optSingle letter version of the option, e.g. c for -c
long_opt(optional) long form version of the option, e.g. copy for --copy
has_arg(optional) option has argument, defaults to false, e.g.  --copy filename where filename is the argument
arg_required(optional) is the argument required, defaults to false
can_repeat(optional) can the argument repeat, defaults to false, e.g.  --copy file1 --copy file2
rep_seperator(optional) if the argument can repeat what should the results be seperated by, defaults to “;”
help_string(optional) String to print for this command when the -h or --help options are passed.

Returns

Integer identifier for this option used in isSet and getArg

parse

declare sub parse(byval argc as integer,
byval argv as zstring ptr ptr)

Parses the command line and prepares the results.

Usage

myOptions.parse( __FB_ARGC__, __FB_ARGV__ )

hasError

declare function hasError() as bool

Returns true if there was an error parsing the command line.

getError

declare function getError() as string

Returns a string describing the error.

setHelpHeader

declare sub setHelpHeader(byref s as string)

Sets the text to show before the options list when showHelp is called.  When printing the help this message is displayed first, then a blank line and then the options.

setHelpFooter

declare sub setHelpFooter(byref s as string)

Sets the text to show after the options list when showHelp is called.  When printing the help this message is displayed after the options preceded by a blank line.

showHelp

declare sub showHelp()

Shows the built-in help for all known options.  Called automatically by parse if -h or --help is passed.

isSet

declare function isSet(byval index as integer) as bool

Was the option passed on the command line?

Parameters

indexIdentifier returned from addOption for an option.

Returns

True if the option was passed on the command line.

getArg

declare function getArg(byval index as integer) as string

Parameters

indexIdentifier returned from addOption for an option.

Returns

String containing the argument passed to this option optionally seperated by the seperator if there is more than one result.

getRemainder

declare function getRemainder() as string

Returns

All unparsed content from the command line.

declare function addBool(byref short_opt as string,  
byref long_opt as string =  "",
byref help_string as string =  "") as integer
Adds a boolean option to the parser.
declare function addOption(byref short_opt as string,  
byref long_opt as string =  "",
byval has_arg as bool =  false,
byval arg_required as bool =  false,
byval can_repeat as bool =  false,
byref rep_seperator as string =  ";",
byref help_string as string =  "") as integer
Adds an option to the parser.
declare sub parse(byval argc as integer,
byval argv as zstring ptr ptr)
Parses the command line and prepares the results.
declare function hasError() as bool
Returns true if there was an error parsing the command line.
declare function getError() as string
Returns a string describing the error.
declare sub setHelpHeader(byref s as string)
Sets the text to show before the options list when showHelp is called.
declare sub showHelp()
Shows the built-in help for all known options.
declare sub setHelpFooter(byref s as string)
Sets the text to show after the options list when showHelp is called.
declare function isSet(byval index as integer) as bool
Was the option passed on the command line?
declare function getArg(byval index as integer) as string
declare function getRemainder() as string
All unparsed content from the command line.