-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parametre in command line for better handling per script
- Loading branch information
Bruno De Bel
authored and
Bruno De Bel
committed
Feb 8, 2018
1 parent
5797ed2
commit 431e95f
Showing
6 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "paramMain.h" | ||
|
||
#include<iostream> | ||
#include<cstdio> | ||
#include<ctype.h> | ||
#include<cstdlib> | ||
|
||
|
||
contextParam::contextParam(){ | ||
option_index = 1; | ||
configFile="config.xml"; | ||
}; | ||
|
||
|
||
// TODO: link with gnu solution for getopt | ||
// | ||
int contextParam::getopt(int argc, char** argv,char* option){ | ||
|
||
if (option_index >= argc) return -1; | ||
|
||
char* tmp = argv[option_index]; | ||
|
||
char ret; | ||
|
||
if ( tmp[0] == '-' ){ // START OPTION | ||
|
||
ret = optopt = tmp[1]; | ||
|
||
if ( optopt == 0 ) { | ||
ret='?'; | ||
goto end; | ||
} | ||
|
||
bool found = false; | ||
for ( char* tmpOption = option; *tmpOption !=0 ; tmpOption++){ | ||
if (*tmpOption == optopt ) {// Found | ||
found = true; | ||
tmpOption++; | ||
if ( *tmpOption == ':' ){ | ||
|
||
if (tmp[2] == 0 ){ | ||
option_index ++; | ||
if (option_index >= argc){ | ||
ret='?'; | ||
goto end; | ||
} | ||
optarg = argv[option_index]; | ||
}else{ | ||
|
||
optarg = tmp+2; | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
if (!found){ | ||
ret='?'; | ||
goto end; | ||
} | ||
|
||
|
||
} | ||
end: | ||
option_index++; | ||
return ret; | ||
|
||
} | ||
|
||
int contextParam::analyse(int argc, char** argv){ | ||
|
||
int c; | ||
|
||
|
||
if (argc == 0){ | ||
configFile = "config.xml"; | ||
return 0; | ||
} | ||
|
||
while ((c = getopt (argc, argv, "c:")) != -1) | ||
switch (c) | ||
{ | ||
case 'c': | ||
configFile = strdup(optarg); | ||
break; | ||
case '?': | ||
if (optopt == 'c') | ||
fprintf (stderr, "Option -%c requires an argument.\n", optopt); | ||
else if (isprint (optopt)) | ||
fprintf (stderr, "Unknown option `-%c'.\n", optopt); | ||
else | ||
fprintf (stderr, | ||
"Unknown option character `\\x%x'.\n", | ||
optopt); | ||
fprintf (stderr, "usage: %s -c config.xml\n", argv[0]); | ||
return -1; | ||
default: | ||
fprintf (stderr, "usage: %s -c config.xml\n", argv[0]); | ||
return -1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef _PARAMMAIN_H | ||
#define _PARAMMAIN_H | ||
|
||
// Manage the parameter in line | ||
|
||
class contextParam{ | ||
|
||
private: | ||
int option_index; | ||
char optopt; | ||
char* optarg; | ||
int getopt(int argc, char** argv,char* option); | ||
|
||
public: | ||
contextParam(); | ||
char* configFile; | ||
int analyse(int argc, char** argv); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters