http://forum.ubuntu-it.org/viewtopic.php?f=33&t=556461
ho scritto una libreria C per il parsing degli argomenti dalla linea di comando.
I sorgenti possono essere scaricati da qui:
https://github.com/Vincenzo1968/myoptParser
La documentazione:
https://github.com/Vincenzo1968/myoptPa ... mpilare.md
https://github.com/Vincenzo1968/myoptPa ... _si_usa.md
Il parser costruisce automaticamente il messaggio di help partendo dai parametri passati alla funzione myopt_Parser.
È possibile specificare quanti argomenti un'opzione può accettare e di che tipo: stringa, numero intero o numero in virgola mobile.
Anche per gli argomenti posizionali è possibile specificare numero e tipo(di default il parser accetta zero o più argomenti posizionali, tutti di tipo stringa).
C'è la possibilità di specificare gruppi di opzioni mutualmente esclusive.
È possibile installare la libreria con i comandi usuali configure/make/make install:
Se volete contribuire allo sviluppo del progetto potete effettuare una pull request all'indirizzo sopra specificato. Oppure si può effettuare una fork. La libreria è rilasciata con licenza GPL3.$ ./configure
$ make
$ sudo make install
Un esempio:
Codice: Seleziona tutto
/*
Copyright (C) 2013 Vincenzo Lo Cicero
Author: Vincenzo Lo Cicero.
From an idea by Leonardo Pozzati.
Special thanks to Francesco Baro for code review and bug fixing.
This file use myoptParserLib.
myoptParserLib is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
myoptParserLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with myoptParserLib. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include "myoptParser.h"
int main(int argc, char* argv[])
{
myopt_Parser_t parser;
int x;
char strDescr[MAX_LEN_STR + 1];
int32_t idGroup;
int32_t idOption;
double num;
bool bParse;
parser = myopt_InitParser();
if ( !parser )
return -1;
idGroup = myopt_AddGroup(parser, "Gruppo 0", true, true);
strcpy(strDescr, "Print this message and exit");
myopt_AddOption(parser, idGroup, 'h', "help", strDescr, false, MOB_OVERRIDE, 0, 0, "");
strcpy(strDescr, "Accumulator: print sum of positionals arguments");
myopt_AddOption(parser, idGroup, 'a', "accumulate", strDescr, true, MOB_OVERRIDE, 0, 0, "");
strcpy(strDescr, "Max: print the maximum of the positional arguments");
myopt_AddOption(parser, idGroup, 'm', "max", strDescr, false, MOB_OVERRIDE, 0, 0, "");
myopt_SetPositionalArgsParams(parser, "NUMS", 1, OR_MORE, "float");
bParse = myopt_ParseArray(parser, argc, argv);
if ( parser->countInternalErrors > 0 )
{
printf("Internal errors:\n");
printf(parser->strInternalErrors);
return -1;
}
idOption = myopt_LookupShort(parser, 'h');
//idOption = myopt_LookupLong(parser, "help");
if ( idOption >= 0 && parser->arrayOptArgs[idOption].countOccurrences > 0 )
{
printf(parser->strUsage);
myopt_FreeParser(parser);
return 0;
}
if ( !bParse )
{
printf(parser->strUsage);
printf(parser->strErrors);
myopt_FreeParser(parser);
return -1;
}
idOption = myopt_LookupShort(parser, 'a');
//idOption = myopt_LookupLong(parser, "accumulate");
if ( idOption >= 0 && parser->arrayOptArgs[idOption].countOccurrences > 0 )
{
num = 0;
for ( x = 0; x < parser->countPosArgs; x++ )
num += parser->arrayPosArgs[x].floatValue;
printf("\nThe sum is: %f\n", num);
}
idOption = myopt_LookupShort(parser, 'm');
//idOption = myopt_LookupLong(parser, "max");
if ( idOption >= 0 && parser->arrayOptArgs[idOption].countOccurrences > 0 )
{
num = 0;
if ( parser->countPosArgs > 0 )
num = parser->arrayPosArgs[0].floatValue;
for ( x = 0; x < parser->countPosArgs; x++ )
if ( parser->arrayPosArgs[x].floatValue > num )
num = parser->arrayPosArgs[x].floatValue;
printf("\nThe maximum is: %g\n", num);
}
myopt_FreeParser(parser);
return 0;
}