Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    command line arguments

    Hey all,

    I am at a stage learning C where command line arguments are becoming useful, and I understand the basics, but there are a few things that I am missing. For example, why is it comventional to add '-' or '--' flags before most options? How does this aid in processing the arguments?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In a sense, it's just convention. However, if you use getopts() (which I imagine a lot of familiar CLI programs do), then getopts uses the '-' character. There is another very common, slightly more sophisticated function that I have not used which processes long ("--option") and short ("-o") forms, but I cannot remember the name of it...but I am sure it is these two C library functions that are the source of the convention.

    Anyway, if you do look up getopt() here's an example you can use at the same time:
    Code:
    #include <stdio.h>
    #include <unistd.h>   /* contains getopt() */
    
    int main(int argc, char *argv[]) {
            char options[]="x:y:z:";
            int opt;
            if (argc>1) {
                    while ((opt=getopt(argc, argv, options))>0) {
                            if (opt=='?') {puts("Wrong!");return -1;}
                            printf("option: %c argument: %s\n",opt,optarg);
                    }        
            }
            return 0;       
    }
    A normal way to use it is with a "switch (opt) {"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    i am not sure i understand the question exactly but i am in the same stage as you.
    last night i could not sleep so i wrote this little program to experiment with the options. i never tried use long options before. i try to use the getopt function when ever i can.

    but if i was to use getopt function or the argp function i would use it in a slightly larger program and i would think more of the design before writing anything.

    as far as i know the options has just a switch '-' or '--' as a UNIX tradition. maybe it is because it was the most easy to parse and became a standard.

    however i am not sure of the exact reason. but from, what i understand then in the end it is not needed to use the get opt function for very few options and not all systems has the library but for bigger programs it is the best approach.
    Code:
    /* 
    * usage; a simple yes program that print 'y' to stdout unless string giving as second option
    * synopsis
    * yes [option] 
    * yes [string]
    * yes  [default , will print'y' to stdout] 
    *
    * options 
    *            --version
    *            --help
    *            --license 
    * 
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    
    void help();
    void license();
    void version();
    
    int main(int argc, char** argv)
    {
       
    	int i;
    	if( argc == 1)
    	{ 
    		for(;;)
    		   fprintf(stdout,"y\n");
    		        
    				 
        } 
    	     if(argv[1][0] == '-' && argv[1][1] == 'i')
    	    {
    		       
    			 for(;;)
    			    fprintf(stdout,"%s\n",argv[2]);
    				                  
    	    }  
    		 
    		 for(i=1;i < argc;i++)
    		 {
    	            
    				   
    				   if(strcmp(argv[i],"--version") == 0)
    				   {
    				             version(argv[0]);
    				   }
    				   
    				   else if (strcmp(argv[i],"--license") == 0)
    				   {
    				            license(argv[0]);
    			       }
    				   
    				   else if (strcmp(argv[i],"--help") == 0)
    				  {
    					         help(argv[0]);
    				
    				   } else { 
    				    
    					          for(;;)
    			        	         fprintf(stdout,"%s\n",argv[i]);
    				                  
    					     }   
    	     }
    		 
    		 
    	return 0;
    }
    
    
    void help(const char *progname)
    {
    	printf("%s: usage no option [option] [string]                        \n"
    	       "\t--version                   prints version number to stdout\n"
    		   "\t--help                                           this help \n"
    		   "\t--license                          prints license to stdout\n"
    		   "\tno option prints singel char y to stdout until program killed\n"
    		   "\t%s [string]  prints string to stdout until program killed\n",progname,progname);
    			exit(0);
    }
    
    void license(const char *progname)
    {
    	fprintf(stdout,"%s\tauthor cmay 2009\n license BSD\n",progname);
    	 exit(0);
    }
    
    void version(const char *progname)
    {
    	fprintf(stdout,"%s\tversion 0.0.1\n",progname);
    	  exit(0);
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The GNU "long form" option function is getopt_long()
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    i found this article which i think is interesting
    Command-Line Options

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's just a matter of convention. A quickie way to differentiate between two types of arguments: file names and flags. Those are the most common items passed.

    If you are writing your own program you can decide to have flags preceded by another character and interpret the arguments accordingly, but why change?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by KBriggs View Post
    Hey all,

    I am at a stage learning C where command line arguments are becoming useful, and I understand the basics, but there are a few things that I am missing. For example, why is it comventional to add '-' or '--' flags before most options? How does this aid in processing the arguments?
    As stated, the reason why options begin with a minus sign is to differentiate them from filenames. There are times when filenames too begin with a minus sign causing much confusion. That's the reason why a double minus "--" was introduced as it signifies the end of options.
    Code:
    some_cmd -o -a -- -inputfile
    Here the file name starts with a minus, so to distinguish it from command line options "-o" and "-a" a double minus -- is placed just before its name as it signals the end of options to some_cmd. After seeing the -- some_cmd knows it's done with option processing and whatever comes next is a filename. Ofcourse this is all a convention.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    That article was interesting, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM