I am looking for a very simple explanation/tutorial on what flags are. I understand that flags work indicate a command what to do. For example:
rm -Rf test
I know that the rm command will remove the test folder and that the -Rf flags will force the command to erase not just the folder but the files in it.
But, where are the flags read/compiled??? What handles the flags? Can I, for example, write my own C/C++ program and designate different flags so that the program does different things? I hope I am asking the right questions. If not, please let me know.
At the C level, command line arguments to a program appear in the parameters to the
main
function. For instance, if you compile this program:and invoke it with the same arguments as your example 'rm' command, you get this:
As you can see, the first entry in
argv
is the name of the program itself, and the rest of the array entries are the command line arguments.The operating system does not care at all what the arguments are; it is up to your program to interpret them. However, there are conventions for how they work, of which the following are the most important:
-r
,-f
), and long options, which are two dashes followed by one or more dash-separated words (--recursive
,--frobnicate-the-gourds
). Short options can be glommed together into one argument (-rf
) as long as none of them takes arguments (see below).-x
is either the remainder of theargv
entry, or if there is no further text in that entry, the very nextargv
entry whether or not it starts with a dash.--output=outputfile.txt
.--
means "do not treat anything after this point on the command line as an option, even if it looks like one." This is so, for instance, you can remove a file named '-f
' by typingrm -- -f
.-
means "read standard input".-v
= be verbose-q
= be quiet-h
= print some help text-o
file = output to file-f
= force (don't prompt for confirmation of dangerous actions, just do them)There are a bunch of libraries for helping you parse command line arguments. The most portable, but also the most limited, of these is getopt, which is built into the C library on most systems nowadays. I recommend you read all of the documentation for GNU argp even if you don't want to use that particular one, because it'll further educate you in the conventions.
It's also worth mentioning that wildcard expansion (
rm -rf *
) is done before your program is ever invoked. If you ran the above sample program as./a.out *
in a directory containing only the binary and its source code you would getGNU libc, which is very likely available on your system, has a library for this called getopt that can be used to parse the options in a sensible fashion. There are examples to get you started in the documentation linked below.
http://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt
Actually you can write your own C++ programm which accepts commandline parameters like this:
int main(int argc, char* argv[]){}
The variable argc will contain the number of parameters, while the char* will contain the parameters itself.
You can dispatch the parameters like this:
This simple program should demonstrate the arguments passed to the program (including the program name itself.)
Parsing, interpreting and using those arguments is up to the programmer (you), although there are libraries available to help.
If you compile this program into
a.out
, and run it as:You should see output:
flags are arguments passed into the main entry point of the program. For example, in a C++ program you can have
your arc is the # of arguments passed in, and the pointer gives u the list of actual arguments. so for
argc would be 3, and the argv array would contain your arguments. Notice argc >= 1 because the program name itself counts (rm). -RF is your 2nd parameter and test is your third.
So whenever you are typing commands in unix, you essentially are executing programs and passing them parameters that they operate on.
If you are really REALLY interested in the unix OS, you should look up forks and how they work. This can get pretty confusing to a newcomer though, so only if you are really interested in OS and how programs are executed.
In your own C program you can process command line options in any way you see fit. Command line parameters in C come in the parameters of the main(int argc, char *argv[]) method as strings.
And if you'd like to process command line parameters in a way similar to most UNIX commands, the function you're probably looking for is getopt()
Good luck!