28 static int _ParseLongOption(
GnoOption_s * pOpt,
char *pszArg)
32 char *pszMatch =
NULL;
40 pszLong = strdup(pOpt->
pszLong);
42 if ((pszPar = strchr(pszArg,
'=')) !=
NULL) {
45 pszMatch = strdup(pszArg);
49 if (strcmp(pszLong, pszMatch + 2) == 0) {
78 static int _ParseLongSwitch(
GnoOption_s * pOpt,
char *pszArg)
85 if (strcmp(pOpt->
pszLong, pszArg + 2) == 0) {
96 static int _ParseShortOption(
GnoOption_s * pOpt,
char *pszArg,
char *pszPar)
106 for (ich = 1; pszArg[ich]; ich++) {
107 if (pszShort[0] == pszArg[ich]) {
108 if (pszPar ==
NULL || pszPar[0] == 0) {
114 if (pszPar[0] ==
'-' && pszPar[1] != 0) {
137 static int _ParseShortSwitch(
GnoOption_s * pOpt,
char *pszArg)
144 for (ich = 1; pszArg[ich]; ich++) {
145 if (pOpt->
pszShort[0] == pszArg[ich]) {
190 int iArg, iOpt, cOrphan = 0;
200 if (pOpt[iOpt].pfValue) {
205 if (pOpt[iOpt].pszDef) {
206 if (pOpt[iOpt].ppszValue) {
207 pOpt[iOpt].
ppszValue[0] = strdup(pOpt[iOpt].pszDef);
211 if (pOpt[iOpt].ppszValue) {
220 for (iArg = 0, pszArgv =
NULL;
221 iArg < argc && (pszArgv = strdup(argv[iArg])) !=
NULL;
222 iArg++, free(pszArgv), pszArgv =
NULL) {
224 if (pszArgv[0] ==
'-' && pszArgv[1] ==
'-' && pszArgv[2]) {
228 if (pOpt[iOpt].pszLong) {
230 nret = _ParseLongSwitch(&pOpt[iOpt], pszArgv);
233 nret = _ParseLongOption(&pOpt[iOpt], pszArgv);
238 "parse option: syntax error at <%s>\n",
250 fprintf(stderr,
"parse option: <%s> is out of scope\n",
255 else if (argv[iArg][0] ==
'-' && argv[iArg][1]) {
256 if (iArg + 1 < argc) {
257 pszArgvNxt = strdup(argv[iArg + 1]);
263 for (cret = iOpt = 0;
265 if (pOpt[iOpt].pszShort) {
267 nret = _ParseShortSwitch(&pOpt[iOpt], pszArgv);
271 _ParseShortOption(&pOpt[iOpt], pszArgv,
276 "parse option: syntax error at <%s>\n",
281 cret = (nret > cret) ? nret : cret;
293 else if (cret == 2) {
307 return (fParseError ==
True) ? -1 : cOrphan;
325 if (pOpt[iOpt].ppszValue) {
326 if (pOpt[iOpt].ppszValue[0]) {
327 free(pOpt[iOpt].ppszValue[0]);
350 fprintf(stderr,
"%s\n", (pszHead) ? pszHead :
"options");
355 if (pOpt[iOpt].pszShort) {
356 fprintf(stderr,
"-%s ", pOpt[iOpt].pszShort);
359 if (pOpt[iOpt].pszLong) {
360 fprintf(stderr,
"--%s", pOpt[iOpt].pszLong);
363 fprintf(stderr,
"\n\t%s\n", (pOpt[iOpt].pszDescr)
364 ? pOpt[iOpt].pszDescr :
"No description available.");
367 if (pOpt[iOpt].pszShort) {
368 fprintf(stderr,
"-%s ", pOpt[iOpt].pszShort);
370 fprintf(stderr,
"<value> ");
373 if (pOpt[iOpt].pszLong) {
374 fprintf(stderr,
"--%s", pOpt[iOpt].pszLong);
376 fprintf(stderr,
"=<value>");
379 fprintf(stderr,
"\n\t%s\n", (pOpt[iOpt].pszDescr)
380 ? pOpt[iOpt].pszDescr :
"No description available.");