I've tried googling the subject, but didn't know the technical name to search for. How do you allow console apps to have switches like "myprogram.exe -x whatever"?
Printable View
I've tried googling the subject, but didn't know the technical name to search for. How do you allow console apps to have switches like "myprogram.exe -x whatever"?
Hey Hey,
That's a pretty broad question.
You should at least let us know which programming language you are writing your console app in. For most languages there are arrays that store command line arguments so that they can be called.. then in your program execution you just have to see which flags are set.. if isset(flag) or if (flag = 'command')... and have the proper code run in each of those if statements.....
I'm sure if you give us the language you're using that someone here will be able to give you a much more detailed answer.
Peace,
HT
Bah, sorry, didn't proof read my post. I'm programming in C++ using Microsoft Visual C++ .NET
Hi
It is not the obvious place to look for it, but here it is on AO[1].
What you have to google for is "c c++ program arguments", e.g.[2].,
or "c c++ command line arguments".
Cheers
[1] http://www.antionline.com/showthread...r=1#post822521
[2] http://publications.gbdirect.co.uk/c...arguments.html
Thanks, that works great. :)
Here is another way of processing them, especially if you are expecting specific arguments (code is from C, but c++ works the same as do most language). This code snippet allows for tacked arguments (i.e. the -al in ls -al) it is handled very similarly in bash scripting as well, but some different stuff has to be done.
previous code taken from svr_tty.c provided here if you have any more questionsCode:while ((argc > 1) && (argv[1][0] == '-')) {
switch (argv[1][1]) {
case '?':
case 'h':
usage(0);
case 'D':
DebugFileName = argv[2];
argc--;
argv++;
/* fall thru */
case 'd':
Debugging = 1;
break;
case 'o':
ConfigType = CONFIG_OLD;
break;
case 'n':
ConfigType = CONFIG_NEW;
break;
case 'B':
daemonize = 1;
break;
case 'F':
daemonize = 0;
break;
case 'f':
NetworkOnDemand = 1;
break;
case 'm':
NetworkDrop = 1;
break;
case 'p':
ConfigType = CONFIG_ARG;
if (ParseConfig(0, argv[2]) < 0) {
fprintf(stderr,
"%s: Invalid port configuration\n",
whoami);
usage(1);
}
break;
case 'v':
printf("Version: %s %s\n", VERSION, rcsid);
exit(0);
case 'k':
Keepalives = 1;
break;
case 't':
n = sscanf(argv[2], "%d,%d", &SocketTimeout,
&SocketWaitTime);
if ((n != 1) && (n != 2)) {
fprintf(stderr,
"%s: Invalid timeout specification\n",
whoami);
usage(1);
}
if (SocketTimeout < 0 || SocketWaitTime < 0) {
fprintf(stderr,
"%s: Invalid time specification\n",
whoami);
usage(1);
}
argc--;
argv++;
break;
default:
fprintf(stderr, "%s: Invalid option -- '%c'\n",
whoami, argv[1][1]);
usage(1);
}
argc--;
argv++;
}
Cheers,
As for the parseing aspect of the command line arguments, I have been looking at C++ class librarys that make command line parseing easy.
One such library I have become particulary fond of called CLAP can be found at http://www.cs.bgu.ac.il/~cgproj/CLAP/
There is also one called TCLAP which is based on the origional CLAP, with full template support check here http://tclap.sourceforge.net/