How do I define the options I want to get from the command line? Any ideas?
How do I define the options I want to get from the command line? Any ideas?
You can specify the names of the array which stores command line arguments in your definition of the main procedure. E.g.Then argc tells you how many arguments you have, and argv is an array of memory addresses, which point to individual command line argument strings. So if you run a program with the commandCode:int main(int argc, char ** argv) { ...
then argc will be 5, and argv[0], argv[1], ..., argv[4] will point to the charactar arrays "./foo", "bar", "a", "b", and "-c", respectively. Yes, the program name gets passed as the first command line argument, and it's usually ignored.Code:./foo bar a b -c
You can define the options you want by writing code that does things, like complaining about missing options and exiting, based on the command line arguments.
Note: argv[0] on Windows will point to the absolute path of the exectuable whereas in unix argv[0] will be how the program was called.
I got this method from the supertux source code
Just add whatever commands you want the user to enter.Code:#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { /* Parse command line arguments */ int i; for (i = 1;i < argc, ++i) { if (strcmp(argv[i], "--test") == 0) { printf("Test Succesful\n"); printf("The string you entered is %s\n", argv[++i]); } else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { printf("Usage %s [options]\n", argv[0]); printf("Options:\n"); printf(" --test str Display the the value str\n"); printf(" -h, --help Display this help message\n"); } else { printf("Unknown option %s\n", argv[i]); printf("Use %s --help for a list of command line arguments\n", argv[0]); return -1; } } /* End of Command Line Parser */ return 0; }
"Just as eating contrary to the inclination is injurious to the health,
so study without desire spoils the memory, and it retains nothing that it takes in."
- Leonardo De Vinci (1452-1519)
Actually it may point to it. In fact, it probably will point to it. However, it is not guarinteed. If the name is not available, argv[0][0] will be '\0'.Originally Posted by OnionKnight
Quzah.
Hope is the first step on the road to disappointment.
Can argv[0] be NULL? (Instead of pointing to ""?)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
If argc is 0, given that argv[argc] is a null pointer, then argv[0] would be a null pointer.
http://web.archive.org/web/200502070...t.html#2.1.2.2
If they are defined, the parameters to the main function shall obey the following constraints:
* The value of argc shall be nonnegative.
* argv[argc] shall be a null pointer.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Okay, thanks. I always use
but I see I don't have to.Code:print_usage(argc ? argv[0] : "*unknown*");
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.