Command Line Switches
Results 1 to 7 of 7

Thread: Command Line Switches

  1. #1
    Senior Member
    Join Date
    May 2004
    Posts
    206

    Command Line Switches

    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"?
    It is better to die on your feet than to live on your knees.

  2. #2
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    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
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  3. #3
    Senior Member
    Join Date
    May 2004
    Posts
    206
    Bah, sorry, didn't proof read my post. I'm programming in C++ using Microsoft Visual C++ .NET
    It is better to die on your feet than to live on your knees.

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    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
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  5. #5
    Senior Member
    Join Date
    May 2004
    Posts
    206
    Thanks, that works great.
    It is better to die on your feet than to live on your knees.

  6. #6
    Member
    Join Date
    Oct 2004
    Posts
    40
    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.
    Code:
    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++;
    	}
    previous code taken from svr_tty.c provided here if you have any more questions
    Cheers,
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

  7. #7
    Junior Member
    Join Date
    Dec 2004
    Posts
    28
    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/
    My Blog -> journy101.myblogsite.com/blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •