From eab7e2415a757bb4c04a060015649436d52aea49 Mon Sep 17 00:00:00 2001 From: Martok Date: Fri, 14 Apr 2017 18:54:23 +0200 Subject: [PATCH] uutlCommandLine: new function utlHandleAllOptions --- bitSpaceUtils.lpk | 6 ++++- bitSpaceUtils.pas | 3 ++- uutlCommandLine.pas | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 uutlCommandLine.pas diff --git a/bitSpaceUtils.lpk b/bitSpaceUtils.lpk index 3067a69..c11c49d 100644 --- a/bitSpaceUtils.lpk +++ b/bitSpaceUtils.lpk @@ -11,7 +11,7 @@ - + @@ -132,6 +132,10 @@ + + + + diff --git a/bitSpaceUtils.pas b/bitSpaceUtils.pas index 08d7c29..978529d 100644 --- a/bitSpaceUtils.pas +++ b/bitSpaceUtils.pas @@ -11,7 +11,8 @@ uses uutlAlgorithm, uutlArrayContainer, uutlCommon, uutlComparer, uutlCompression, uutlEmbeddedProfiler, uutlEnumerator, uutlEvent, uutlEventManager, uutlFilter, uutlGenerics, uutlInterfaces, uutlKeyCodes, uutlLinq, uutlListBase, uutlLogger, uutlMCF, uutlObservable, uutlSScanf, uutlStreamHelper, uutlSyncObjs, uutlThreads, uutlTypes, - uutlXmlHelper, uutlVariantObject, uutlVariantProperty, uutlVariantEnum, uutlVariantSet, LazarusPackageIntf; + uutlXmlHelper, uutlVariantObject, uutlVariantProperty, uutlVariantEnum, uutlVariantSet, uutlCommandLine, + LazarusPackageIntf; implementation diff --git a/uutlCommandLine.pas b/uutlCommandLine.pas new file mode 100644 index 0000000..5a15fe0 --- /dev/null +++ b/uutlCommandLine.pas @@ -0,0 +1,55 @@ +unit uutlCommandLine; + +{$mode objfpc}{$H+} + +interface + +uses + getopts; + +{ Forward definitions so getopts doesn't have to be used } +type + TOption = getopts.TOption; +const + No_Argument = getopts.No_Argument; + Required_Argument = getopts.Required_Argument; + Optional_Argument = getopts.Optional_Argument; + +type + TOptionHandler = procedure (const opt: string; const OptArg: string); + TOptions = array[1..10] of TOption; + POptions = ^TOptions; + +(* + Process all arguments as defined in ShortOpts and LongOpts + Arguments: + ShortOpts - short option string (see getopts) + LongOpts - pointer to the first element of an array of long options, terminated by an empty option record (see getopts) + Handler - pointer to a function called to handle each option + + Result: + The index of the first option not processed (this correctly handles the -- delimiter) +*) +function utlHandleAllOptions(ShortOpts : String; LongOpts : POptions; Handler: TOptionHandler): integer; + +implementation + +function utlHandleAllOptions(ShortOpts : String; LongOpts : POptions; Handler: TOptionHandler): integer; +var + optIndex: integer; + opt: string; +begin + optIndex:= 0; + while True do begin + opt:= GetLongOpts(ShortOpts, POption(LongOpts), optIndex); + if opt = EndOfOptions then + break; + if opt = #0 then + opt:= LongOpts^[optIndex].Name; + Handler(opt, getopts.optArg); + end; + Result:= getopts.OptInd; +end; + +end. +