weird problem in command line parsing!

Pages: 12
hi all,
in my application, i want to use command-line parameters, if no parameters specified, show open dialog to select a file
if -h or --help was specified, print the help message in console
but it works when i give no parameters to command-line!
the problem is, when i go to cmd, and type my application name followed by the parameters, it doesnt show anything and when i debug it, it exit's normally!
this is some parts of my code:
 
int cmd=0;

i've declared other variables here, and it is the continue
1
2
3
4
5
6
7
8
while( ++cmd < argc)
{
if (lexicographical_compare(argv[cmd], "-h") || lexicographical_compare(argv[cmd], "--help") || lexicographical_compare(argv[cmd], "-?"))
{
printhlp(argv[0]);
exit(0);
}
}

please note that i'm using boost libraries!
what is the problem? because the loop is here and my application processe's command line from here
thanks all in advance
Last edited on
You seems to misunderstand how lexicographical_compare works. It would be the equivalent of (argv[cmd] < "-h") || argv[cmd] < "--help") || argv[cmd] < "-?") if it worked correctly
i've used strcmp too!
but it didnt work and gave segfault
How did you use it?
if(strcmp(argv[cmd], "--help")==0)
And where exactly did that sefault happens? What line, what statement? What call stack looked like?
it give's segfaults from line 162 where i start parsing my command-line parameters
and the error is from msvcrt.dll
What are parameters to call and cll stack (exactly). Are you using Visual studio nad wmain/_tmain?
i'm using GCC 4.8.1 with code::blocks 13.12 with -std=gnu++11
Can you please post minimal compiling example reproducing your issue with strcmp (or whole code if you cnnot make it small)
this is the parsing code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while( ++cmd < argc)
{
    if(strcmp(argv[cmd], "-c"))
{
compile=true;
}
if (strcmp(argv[cmd], "-d"))
{
dbg = TRUE;
}
if(strcmp(argv[cmd], "-o"))
{
strcpy(outfile, argv[cmd+=1]);
}
if (strcmp(argv[cmd], "-h") || strcmp(argv[cmd], "--help") || strcmp(argv[cmd], "-?"))
{
printhlp(argv[0]);
exit(0);
}
else
{
printf(stderr, "unknown option: ", argv[cmd]);
printhlp(argv[0]);
exit(1);
}
strcpy(scriptfile, argv[cmd++]);
}

compile is a variable to determine the script should be compiled or not
dbg is a variable that says the debugging information should be plased to script or not
scriptfile is the script filename
outfile is the executable path
outside my while loop, i've used tinyFileDialogs if scriptfile=="", show's the open dialog
and set the scriptfile to the specified path
if outfile=="", show save dialog and set outfile to the specified path of save dialog
what do i have to do?
First of all line 15 is always true: strcmp returns non-zero (converted to true) if strings are not equal, and it is impossible for string to equal to three different strings.

Also all other conditions are backward too.

Line 11: your program is going to crash if you do not provide next parameter, and because of the previous bug, it is going to crash if you run your program with one parameter which is not -o
does this code is correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while( ++cmd < argc)
{
    if(strcmp(argv[cmd], "-c"))
{
compile=true;
}
if (strcmp(argv[cmd], "-d"))
{
dbg = TRUE;
}
if(strcmp(argv[cmd], "-o"))
{
strcpy(outfile, argv[cmd++]);
}
if ((strcmp(argv[cmd], "-h") || strcmp(argv[cmd], "--help") || strcmp(argv[cmd], "-?"))==true)
{
printhlp(argv[0]);
exit(0);
}
else
{
printf(stderr, "unknown option: ", argv[cmd]);
printhlp(argv[0]);
exit(1);
}
strcpy(scriptfile, argv[cmd++]);
}

what is the problem here!
which line causes problem?
i've did everything you've said!, but again that bad behaiviour
You changed nothing from your previous code and all problems are still here:
1) Incorrect use of strcmp: strcmp returns 0 (false) for equal strings
2) Not checking if there something after -o parameter.

Lets see what happens if you run your program with -h parameter:

strcmp("-h", "-c") returns positive value → true, so compile is set to true.
strcmp("-h", "-d") returns positive value → true, so dbg is set to true.
strcmp("-h", "-o") returns negative value → true, so you are trying to copy next argument from argv, which is null pointer. Crash.

You did show a correct examle of using strcmp earlier: http://www.cplusplus.com/forum/general/167947/#msg842319
this is the new code, and i changed it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
while( ++cmd < argc)
{
    if(strcmp(argv[cmd], "-c")==0)
{
compile=true;
}
if (strcmp(argv[cmd], "-d")==0)
{
dbg = TRUE;
}
if(strcmp(argv[cmd], "-o")==0)
{
strcpy(outfile, argv[cmd++]);
}
if (strcmp(argv[cmd], "-h") ==0){
printhlp(argv[0]);
exit(0);
}
if (strcmp(argv[cmd], "--help") ==0)
{
printhlp(argv[0]);
exit(0);
}
if( strcmp(argv[cmd], "-?")==0)
{
printhlp(argv[0]);
exit(0);
}
else
{
printf( (const char*) stderr, "unknown option: ", argv[cmd]);
printhlp(argv[0]);
exit(1);
}
strcpy(scriptfile, argv[cmd++]);
}

but it doesnt show the help message
And what it does? Are you sure that it is not the problem of consolewindow closing before you can read it?
it has closed the console, but no help were displayed
i dont know what the problem is!
for example, when in cmd i type appname -h it doesnt show anything!
but it doesnt crash
Mind posting full code here?
i've posted full command-line parsing code here!
my app is closed source, and this is the code that i can plase here!
do you recommend to use a command-line parser library?
Post at least code which (a) compiles, (b) shows problem you have. Cut parts you don't need, replace called functions by do-nothing mockups. I believe that problem lies with interaction of this code and some other.
Pages: 12