cannot get the options from the command line!

This is a discussion on cannot get the options from the command line! within the Linux Programming forums, part of the Platform Specific Boards category; Hi I have a pogram that reads the network address and netmask from the command line! But when i run ...

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269

    cannot get the options from the command line!

    Hi

    I have a pogram that reads the network address and netmask from the command line! But when i run the program. it fails! It calls the usage() function! But, when i use gdb to trace over the code, it runs properly except that it gives the whole line after "-i" as an argument....Here is the code

    Code:
    int main(int argc,char *argv[])
    {
    // Use default values?
    // All the defaults are defined in "program.h"
    	int useDefaults=0;	
    	if (argc > 3)
    		usage(argv[0],0);
    	else if (argc==1)
    		useDefaults=1;
    
    // Variables	
    	int n_opt;
    	int i_flag=0;
    	int n_flag=0;
    	int p_flag=0;
    	string netaddr(getNetworkAddress(getLocalIP(),DEFNETMASK));
    	string netmask(DEFNETMASK);
    	string portRange(DEFMINPORT "-" DEFMAXPORT);
    	string s_option("hi:n:p:v");
    	const option l_option[]={
    	 { "help",		0, NULL, 'h'},
    	 { "ip",		1, NULL, 'i'},
    	 { "nm",		1, NULL, 'n'},
    	 { "port",		1, NULL, 'p'},
    	 { "version",	0, NULL, 'v'},
     	};
    	
    // Now, looking for the supplied arguments and options 
    	if (!useDefaults )
    	{
    		do {
    			n_opt=getopt_long(argc,argv,s_option.c_str(),l_option,NULL);
    			switch(n_opt){
    				case 'h':
    					usage(argv[0],0);
    					break;
    				case 'i':
    					netaddr=optarg;
    					i_flag=1;
    					break;
    				case 'n':
    					netmask=optarg;
    					n_flag=1;
    					break;
    				case 'p':
    					portRange=optarg;
    					p_flag=1;
    					break;
    				case 'v':
    					cout << " " << argv[0] << " version " << VERSION_INFO << "." << endl; 
    					cout << " Written by fnoyan." << endl;
    					exit(0);
    				case -1:
    					break;
    				default:
    					usage(argv[0],1);
    			} // switch
    		} while (n_opt!=-1);
    	} // if (!useDefaults)
    	else
    	{
    		cout << " No extra options are given! The default values will be used..." << endl;
    		cout << " \"program -h\" for a short list of available options." << endl;		
    	}
    	
    	cout << " program will use the values below..." << endl;
    	cout << " Network IPv4 Address       : " << netaddr << endl;
    	cout << " Netmask for the Network    : " << netmask << endl;
    	cout << " Ports that will be scanned : " << portRange << endl;
    	
    // Main try block
    	try 
    	{ // just trying the Socket class....
    		Socket s;
    		s.connect("127.0.0.1",22);
    	} catch (SocketException e) 
    	{
    		cout << e.description() << endl;
    		exit(1);
    	} // End of Main try block 
    	
    	return 0;
    }
    When i run the program
    Code:
     $ program -i 82.122.156.32 -n 255.255.0.0
    i fails and calls usage() funstion. The interesting thing here is that, i cannot get the sane result inside GDB! While running the code with GDB, (r "-i 82.122.156.32 -n 255.255.0.0"), it just gets the "82.122.156.32 -n 255.255.0.0" part as the argument of "-i".

    Thanks in advice....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Don't put the command line in "" inside the debugger?
    Doing the same thing from the command line would make it a single parameter as well

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Ok, but what about the normal action! When i run the program from the command line, it gives me the output of usage() function. There must be something wrong in the while loop?!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > if (argc > 3)
    Perhaps you meant < 3
    Because your example has more than 3

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You have four command line arguments, not two: "-i", "82.122.156.32", "-n", and "255.255.0.0"

    This is probably why things don't work.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 04:06 PM
  3. Explorer options
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 04-02-2005, 08:23 AM
  4. Disabling the main frame menu options
    By MPC_Engr in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2003, 08:04 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21