This is a simple tutorial that will show you how to read command line arguments and how to parse them with getopt.
To read command line arguments, you must declare main in a special way.
Here is how you declare main:
Their are two variables, argc and argv. argc stands for argument count, so it is how many arguments are passed on the command line. argv is a pointer to a pointer, it is the set of actual arguments. This may seem confusing at first.Code:int main(int argc, char **argv)
About argv:
A string is a pointer to a bunch of characters, right? Well argv is a pointer to a bunch of strings. For example, argv[0] is a pointer to the first argument, stored as a string.
Here is an example program that prints out all of the command line arguments you provide:
Seems easy enough, right? Now that is only half of it. Most programs accept arguments using dashes, for example:Code:#include <stdio.h> int main(int argc, char **argv) { int i; //counter for (i=0;i<argc;i++) printf("%s\n", argv[i]); return 0; }
In Unix would list the directory, and -a means list all files. To recognize and parse these options, programs use a function called getopt.Code:ls -a
In order to use getopt, you must include this line:
Note: This file is part of the C POSIX library, and will not work on Windows unless compiled with Mingw or Cygwin.Code:#include <unistd.h>
Using getopt:
getopt is almost always used in a loop. When there are no more arguments to parse, getopt will return -1. You use getopt like this:
argc and argv are the arguments from main. "abc" is the set of characters that are acceptable arguments. c is simply a character that is the argument returned by getopt.Code:c=getopt(argc, argv, "abc");
Arguments that take options:
Some arguments need to take an option. For example, many programs use the argument "-f file" to provide file access. When arguments use options, you must follow the acceptable argument by a colon. For example, "a:bc" will accept arguments a, b, and c, and a will have an extra option. The extra option is stored in *optarg.
Here is an example program that can take two arguments, -a and -x. It will tell you if -a is provided, and if -x is provided. It will also tell you the option that -x is provided with.
There are actually more things you can do with getopt, but I will not cover those things here. Any feedback is welcome. Please post here if you have any questions, or improvements. +rep is appreciated a lot!Code:#include <unistd.h> #include <stdio.h> int main (int argc, char **argv) { int c; while ((c=getopt(argc, argv, "ax:")) != -1) { if(c=='a') printf("-a was provided\n"); if(c=='x') printf("-x was provided with %s\n", optarg); } return 0; }
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Nice intro+rep
Taking in command line arguments is a common topic of confusion among newer C programmers, and you explained it well. +rep 4 u!
Wow I changed my sig!
I would use a switch statement instead of a block of ifs, but yes, this is a good explanation! Do you know how to use getopt_long?
EDIT: Can't +rep you for some reason...
sudo rm -rf /
Yes, I know how to use getopt_long, but like I said I didn't explain some things here. Plus that is exclusive to GNU and not part of the standard POSIX library.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Well that's no fun.![]()
sudo rm -rf /
Maybe I will have a part two that includes GNU extensions.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Multipart tutorials are always fun - especially when they get long and you skip numbers. Almost did that once.
sudo rm -rf /
i like it .. +rep![]()
Since you begged for the rep... And since you did an excellent job, +rep!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks