jeudi 29 avril 2021

C++ accepting command line argument with "-" symbol

I am new to c++ and trying to read command line arguments specified as below.

./helloworld -i input_file -o outputfile -s flag3 -t flag4

I tried hardcoding the flags by index as below

int main(int argc, char *argv[]) {
 // argv[1] corresponds to -i
 // argv[2] corresponds to input_file
 // argv[3] corresponds to -o
 // argv[4] corresponds to outputfile
 // argv[5] corresponds to -s
 // argv[6] corresponds to flag3
 // argv[7] corresponds to -t
 // argv[8] corresponds to flag4

}

Then i realized the order can be changed so I can't use hardcoded index, I used a unordered_map<string, string> to put the -i, -o, -s, -t as keys and inputfile, outputfile, flag3, flag4 as values.

This is working fine, but I was wondering is there any better way to do the same.

Aucun commentaire:

Enregistrer un commentaire