Thread: Working With Command Line Arguements

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Exclamation Working With Command Line Arguements

    im looking for a good way of using command line arguements in a program. like app -x "some vaule". how do you guys do it? right now, im in if hell tying to get this right. is any other way?

    thanks

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
    int main (int argc, char *argv[])
    argv[0] is a null-terminated string of the name of your program (though what it actually contains can vary)
    argv[1] is a null-terminated string of the first argument the user passed to your program
    argv[2] is the second arg
    argv[argc-1] is the last arg that was passed
    hello, internet!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ..... and finally (to continue moi's help).... argv[argc] is a null pointer to provide a redundant check for the end of the list.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Have you looked at getopt() ?

    something like:

    Code:
    #include <unistd.h>
    
    int main(int argc, char **argv) {
        int opt;
    
    
        while((opt = getopt(argc, argv, "p:V")) != -1) {
          switch(opt) { 
            case 'p':
              printf("argument p = %s\n", optarg);
              break;
            case 'V':
              verbose++;
              break;
            case ':'
              printf("Option %c needs a value\n", opt);
              break;
            case '?':
              printf("invalid option?\n");
              break;
          }
        }
    }

    getopt(3)
    Last edited by jabrams; 08-03-2002 at 04:29 PM.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    
    void DefaultMessage ( void );
    
    int main ( int argc, char * argv[] )  
    {
    	if (argc == 2)
    	{
    		if(argv[1][0] == '?')
    		{
    			printf("Help stuff would go here.\n");
    		}
    		else
    		{
    			printf("%s \n", argv[1]);
    		}
    	}
    	else if ( argc >= 3 )
    	{
    		printf("Invalid input:\n");
    		printf("Only one command-line parameter allowed.\n");
    	}
    	else
    	{
    		DefaultMessage();
    	}
    	return 0;
    }
    
    void DefaultMessage ( void )
    {
    	printf("CmdLine.exe:\n");
    	printf("The purpose of this program is to demonstrate command line arguments.\n");
    	printf("This program will print whatever you type on the screen.\n\n");
    	printf("Example:\n");
    	printf("C:\\>CmdLine.exe Hmmm\n");
    	printf("Hmmm\n");
    }
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing command line arguements to a string.
    By SlyMaelstrom in forum C++ Programming
    Replies: 14
    Last Post: 10-30-2005, 04:56 AM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM