PDA

View Full Version : command line parameters: wildcard problem



vonCZ
18th April 2013, 16:06
Hey guys,

I'm working on a C++ program that runs/processes data from a bash shell on Cygwin. I've written code to parse command line arguments like so:




// in main(int argc, char *argv[])
char testFile[10][256]; // holds up to 10 testFile names of up to 256 char each
uint16 testFileNUM = 0;


// call my parsing function:
doArgs(argc, argv);


// afterward report the parameter info
for(uint16 a=0; a<testFileNUM; a++)
printf("TestFile and NUM: %s %hd\n", testFile[a], testFileNUM);


// snippet from command line parsing function
void doArgs(int argc, char *argv[])
{
char *a;
while(--argc > 0 && (*++argv)[0] == '-')
{
a = argv[0] + 1;
switch(*a)
{
case 't':
if(!strcasecmp(a, "test_file"))
{
sscanf(*++argv, "%s", &testFile[testFileNUM]);
testFileNUM++;
argc--;
}
else
{
printf("Unknown option: %s\n", *argv);
usage();
exit(1);
}
break;
// etc...
}
}
}


This has worked fine for me. If I type:

$> program -test_file text01.txt -test_file text02.txt -test_file text03.txt

I get the following expected report:

$> TestFile and NUM: text01.txt 3
$> TestFile and NUM: text02.txt 3
$> TestFile and NUM: text03.txt 3


This is what I want: after the file names have been read into the program, I can open them and do what I want.

However, now I've a situation such that, instead of specifying the input files specifically, I want to read them using a wildcard like so:

$> program -test_file text*.txt

But when I do so, I get the following report:

$> TestFile and NUM: text*.txt 1


This is useless to me (I think): I cannot keep track of and access the individual files. Can someone give me a suggestion here: how can I read the individual file names in my doArgs() function using the * wildcard?

Thanks!

amleto
20th April 2013, 14:55
well yes - c++ is not a mind reader.

you have to read the local directory and use regex to match filenames.

btw, boost :: program_options might be of interest. qt may have alternative as well.

wysota
20th April 2013, 15:03
'*' should be expanded by the shell. Apparently you're using one that doesn't expand wildcards.