madziikoy
madziikoy

Reputation: 1497

C++ linux argv dash parameters

I have been trying to work with C++ console app on linux.

I want to change the options of the user depending on what parameter he used.

Like: ./my -n 1234 or ./my -f file.txt

-n or -f is always argv[1] 1234 or file.txt is always argv[2]

my main()

int main(int argc, char *argv[])

Here is my code so far:

string numlist, num;


if (argc < 3) {
   fprintf(stderr,"usage stuff...%s %s", argv[0], argv[0]);
   exit(0);
}

char *f = "-f";
char *n = "-n";
char *argv0 = argv[0];
char *argv1 = argv[1];
char *argv2 = argv[2];

if (strcmp(argv1, f) == 0){
    numlist = argv[2];
    //more data for this parameter
}

if (strcmp(argv1, n) == 0){
    num = argv[2];
    //more data for this parameter
}

Problem:

when i try ./my -n 1234
This should skip 1st if block, then execute 2nd if block.
or ./my -f file.txt
Should execute the 1st if block, and skip the 2nd if block.

Now with my current code if i enter either -n or -f it skips all if blocks.

-- edit --
I guess I fixed it the problem was actually not on the argv but on my if blocks.

Thanks everyone I'll check all your suggestions.

Upvotes: 1

Views: 3978

Answers (4)

Zane
Zane

Reputation: 926

Add some output statement to your if-clauses, like printf("this is option -f\n"), I'd bet you will see that it does not skip both if-bodies, only one.

Looks like you are not very familiar with string handling in C++. Look for std::string and see how you can do things easier using strings than using char*. I would assume that using boost is better left to more experienced programmers, but you can give it a try,

Upvotes: 0

bjoernz
bjoernz

Reputation: 3852

Unless you want to do this as an exercise for yourself, you should probably use a library function for this.

Walter has already mentioned boost::program_options. If you do not want to work with Boost, you can also have a look at getopt, which is a tried and true C library function.

Upvotes: 0

Skatox
Skatox

Reputation: 4284

I don't know what's your complete source code but here is what it worked for me:

#include <string.h>
#include <string>
#include <stdio.h>

using namespace std;

int main(int argc, char **argv){

    if (argc < 3) {
       fprintf(stderr,"usage stuff...%s %s", argv[0], argv[0]);
       exit(0);
    }

    string numlist;
    string num;

    char *f = "-f";
    char *n = "-n";
    char *argv0 = argv[0];
    char *argv1 = argv[1];
    char *argv2 = argv[2];


    if (strcmp(argv1, f) == 0){
        numlist = argv[2];
       //more data for this parameter
    }

    if (strcmp(argv1, n) == 0){
        num = argv[2];
    //more data for this parameter
    }
}

I've added the using namespace std to make the writing of the code easier, also i've moved the validation to the top, because if argv[2] doesn't exists, it will throw an error.

Upvotes: 0

George Aprilis
George Aprilis

Reputation: 1200

Does this help? It looks like a neat way to parse command arguments.

(And a more complicated one, although I think the first is good enough)

Upvotes: 1

Related Questions