Click to See Complete Forum and Search --> : Command Line Options in C/C++


Dun'kalis
10-17-2002, 06:32 PM
How would I add command line options to a program in C or C++? Can't seem to find that anywhere.

michaelk
10-17-2002, 06:56 PM
argc - number of arguments
argv - array of arguments

Simple C program


int main(int argc, char *argv[])
{
int count;

for (count=1;count < argc; count++)
printf("%s ",argv[count]);

printf("\n")

return 0
}

furrycat
10-17-2002, 10:38 PM
You can also check out getopt. It may be overkill for what you want, though.

bin2hex
10-31-2002, 06:12 PM
Originally posted by michaelk
argc - number of arguments
argv - array of arguments

Simple C program


int main(int argc, char *argv[])
{
int count;

for (count=1;count < argc; count++)
printf("%s ",argv[count]);

printf("\n")

return 0
}



Just for the sake of completeness (is this correctly spelled? :) ), if you also want to retrieve the environment variables (the ones like HOME, PAGER, etc.), you can use the following main() form:
(this sample will list all the command line parameters, followed by a list of all environment variables)


/*
argc - number of arguments
argv - arguments array
envp - environment strings array
*/
int main(int argc, char *argv[], char *envp[])
{
int count;

/* display command line arguments */
for (count = 0; count < argc; count++)
printf("arg %d = %s\n", count, argv[count]);

printf("-------------------------------\n");

/* display all environment variables */
/* note that when we reach the end of the list */
/* envp[count] will be equal to NULL */
for (count = 0; envp[count] != NULL; count++)
printf("env %d = %s\n", count, envp[count]);

return 0;
}


I know that this was not asked about, but it seemed to me that, as we were talking about main(), this could be useful knowledge.

bwkaz
11-01-2002, 02:05 PM
I don't know if envp is portable, though... I know it works in every OS I've seen (OK, well, DOS and Linux), but I don't believe C libraries have to support it.

I know C libraries have to support getenv(), though, so I've just used it whenever I've needed access to the environment. Not that it matters much, but if you want maximum portability, I'm not sure envp is a good way to accomplish that.

bin2hex
11-04-2002, 01:46 PM
Originally posted by bwkaz
I don't know if envp is portable, though... I know it works in every OS I've seen (OK, well, DOS and Linux), but I don't believe C libraries have to support it.

I know C libraries have to support getenv(), though, so I've just used it whenever I've needed access to the environment. Not that it matters much, but if you want maximum portability, I'm not sure envp is a good way to accomplish that.

Yes, I have to agree with you. I've only worked with DOS/Windows and Linux until now and all C/C++ compilers that I've used seem to support envp, but I can't say that all OS's support envp.

You are right that it could not be a good thing to use if you want max portability, but there are times that I feel that it's easier to use envp instead of getenv(): one of those situations is when you wan to know all the environment variables currently defined.

I tried to check the C standards to see if envp is part of them but was unable to get any answer (that is, I got too much answers to be able to check them ;) )

bwkaz
11-04-2002, 02:16 PM
True.

I believe you can also access the global extern char **environ; variable if you need envp's functionality, but that may only be as portable as envp, so I don't know.

Actually, from the getenv() man page:

CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899 So I'd check into the System V Interface Definition v3, the POSIX standard, and the ISO 9899 standard for how portable it would be. Or maybe just assume it's as portable as POSIX/SVID 3?