getopt einige parameter immer zuerst einlesen
-
Hi,
Wie kann ich mittels getopt bestimmte parameter immer zuerst einlesen werden sogesagt die reiheenfolge bestimmten was eingelesen wird. Weil z.B. wenn die Option -x oder -y angeben wird muss diese zuerst eingelesen werden bevor die Option -p eingelesen wird.
Inmoment mach ich das so aber kann man das nicht bestimmten?int main( int argc, char ** argv ) { struct option param_options[] = { {"print", required_argument, NULL, 'p'}, {"load", required_argument, NULL, 'l'}, {"x", required_argument, NULL, 'x'}, {"y", required_argument, NULL, 'y'}, {"help", no_argument, NULL, 'h'}, {0,0,0,0} }; int x = 0, y = 0; char *text = NULL, *load = NULL; while ((c = getopt_long(argc,argv, "p:l:x:y:h", param_options, NULL)) != EOF) { switch (c) { case 'x': x = atoi(optarg); break; case 'y': y = atoi(optarg); break; case 'p': text = optarg; break; case 'l': xml = optarg; break; case 'h': printf("Help"); break; } } if (x != 0) { printf("x value"); } if (y != 0) { printf("y value"); } if (text != NULL) { printf("text"); } if (load != NULL) { printf("load"); } return 0; }
thx
alpin
-
was macht/ist getopt eigentlich???
-
--linuxuser-- schrieb:
was macht/ist getopt eigentlich???
mit getopt kannst du schnell command-line Argumente in argv parsen ohne das Rad neu erfinden zu müssen.
-
ok anscheinend gibt es keine gescheite funktion jetzt hab ich probiert zwei mal die while schleife zu erzeugen aber in der zweite springt er garnicht mehr rein da ja die erste Schleife bis zum EOF gelaufen ist aber ich bekomm nicht hin das er zum Start zurückspringt wie kann ich getopt oder argc? zum start setzen?
-
Ich bin jetzt zu faul, den Kram fuer Dich umzustricken, und muss auch sagen, dass dies aus einem C++-Programm kommt, aber ich glaube, dass Du envtl. das fuer Deinen Kram entsprechend umstricken kannst - der Code muesste weitgehend selbsterklaerend sein:
bool CommonData::ReadInArgumentList (int argc, char **argv) { bool fSuccess = true, fStopLoop = false; int iLoop = 1; while ((fSuccess == true) && (iLoop < argc) && (fStopLoop == false)) { iLoop = InterpreteAndAddArguments (argv, &fStopLoop, iLoop, argc); if (iLoop < 0) { fSuccess = false; printf ("At parameter no. <%d>\n", iLoop); } } if (fSuccess == false) printf ("Error in <VALI_CommonData::ReadInArgumentList> %s\n", _SeeAboveMssg_); else iStartListArg = iLoop; return fSuccess; } int CommonData::InterpreteAndAddArguments (char **pSzIn, bool *fStopLoop, int iLoop, int iMax) { char *szHelp; int iReturn; bool fSuccess = true, fReadNext = false, fReadConfigFile = false, fReadFilenames = false, fReadFilenameAllXML = false, fReadFilenameAllASCII = false; iReturn = iLoop; szHelp = pSzIn [iLoop]; if (szHelp == NULL) { fSuccess = false; printf ("Null pointer argument\n"); } else { if (strcmp (szHelp, _FalgConfigFile_) == 0) { fReadConfigFile = true; fReadNext = true; } else if (strcmp (szHelp, _FalgHelp_) == 0) fPrintHelp = true; else if (strcmp (szHelp, _FalgFilename_) == 0) { fReadNext = true; fReadFilenames = true; } else if (strcmp (szHelp, _FlagAllDataXML_) == 0) { fReadNext = true; fReadFilenameAllXML = true; } else if (strcmp (szHelp, _FlagAllDataASCII_) == 0) { fReadNext = true; fReadFilenameAllASCII = true; } else if (PDB_File::IsPDB_FileID (szHelp) == true) { *fStopLoop = true; iReturn--; } else { printf ("Argument <%s> not valid\n", szHelp); fSuccess = false; } } if ((fSuccess == true) && (fReadNext == true)) { iReturn++; if (iReturn >= iMax) { printf ("Max count reached <%d> of <%d>\n", iReturn, iMax); fSuccess = false; } else { szHelp = pSzIn [iReturn]; if (szHelp == NULL) { printf ("Argument is missing\n"); fSuccess = false; } } } if (fSuccess == true) { if (fReadFilenames == true) fSuccess = AddFilenameSeperatly (szHelp); else if (fReadConfigFile == true) fSuccess = SetConfigurationFilename (szHelp); else if (fReadFilenameAllXML == true) fSuccess = SetXLM_AllPattern (szHelp); else if (fReadFilenameAllASCII == true) fSuccess = SetTable_AllPattern (szHelp); } if (fSuccess == false) { printf ("Error in <VALI_CommonData::InterpreteAndAddArguments> %s\n", _SeeAboveMssg_); return (-1); } else return (iReturn + 1); }