+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Reading Command Line Arguments

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Reading Command Line Arguments

    This is a simple tutorial that will show you how to read command line arguments and how to parse them with getopt.

    To read command line arguments, you must declare main in a special way.
    Here is how you declare main:
    Code:
    int main(int argc, char **argv)
    Their are two variables, argc and argv. argc stands for argument count, so it is how many arguments are passed on the command line. argv is a pointer to a pointer, it is the set of actual arguments. This may seem confusing at first.

    About argv:
    A string is a pointer to a bunch of characters, right? Well argv is a pointer to a bunch of strings. For example, argv[0] is a pointer to the first argument, stored as a string.

    Here is an example program that prints out all of the command line arguments you provide:
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	int i; //counter
    	for (i=0;i<argc;i++)
    		printf("%s\n", argv[i]);
    	return 0;
    }
    Seems easy enough, right? Now that is only half of it. Most programs accept arguments using dashes, for example:
    Code:
    ls -a
    In Unix would list the directory, and -a means list all files. To recognize and parse these options, programs use a function called getopt.
    In order to use getopt, you must include this line:
    Code:
    #include <unistd.h>
    Note: This file is part of the C POSIX library, and will not work on Windows unless compiled with Mingw or Cygwin.

    Using getopt:
    getopt is almost always used in a loop. When there are no more arguments to parse, getopt will return -1. You use getopt like this:
    Code:
    c=getopt(argc, argv, "abc");
    argc and argv are the arguments from main. "abc" is the set of characters that are acceptable arguments. c is simply a character that is the argument returned by getopt.

    Arguments that take options:
    Some arguments need to take an option. For example, many programs use the argument "-f file" to provide file access. When arguments use options, you must follow the acceptable argument by a colon. For example, "a:bc" will accept arguments a, b, and c, and a will have an extra option. The extra option is stored in *optarg.

    Here is an example program that can take two arguments, -a and -x. It will tell you if -a is provided, and if -x is provided. It will also tell you the option that -x is provided with.
    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    int main (int argc, char **argv)
    {
    	int c;
    	while ((c=getopt(argc, argv, "ax:")) != -1) {
    		if(c=='a')
    			printf("-a was provided\n");
    		if(c=='x')
    			printf("-x was provided with %s\n", optarg);
    	}
    	return 0;
    }
    There are actually more things you can do with getopt, but I will not cover those things here. Any feedback is welcome. Please post here if you have any questions, or improvements. +rep is appreciated a lot!
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Reading Command Line Arguments

    Nice intro +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Reading Command Line Arguments

    Taking in command line arguments is a common topic of confusion among newer C programmers, and you explained it well. +rep 4 u!
    Wow I changed my sig!

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Reading Command Line Arguments

    I would use a switch statement instead of a block of ifs, but yes, this is a good explanation! Do you know how to use getopt_long?

    EDIT: Can't +rep you for some reason...
    sudo rm -rf /

  6. #5
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Reading Command Line Arguments

    Yes, I know how to use getopt_long, but like I said I didn't explain some things here. Plus that is exclusive to GNU and not part of the standard POSIX library.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Reading Command Line Arguments

    Well that's no fun.
    sudo rm -rf /

  8. #7
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Reading Command Line Arguments

    Maybe I will have a part two that includes GNU extensions.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Reading Command Line Arguments

    Multipart tutorials are always fun - especially when they get long and you skip numbers. Almost did that once.
    sudo rm -rf /

  10. #9
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Reading Command Line Arguments

    i like it .. +rep

  11. #10
    Jordan Guest

    Re: Reading Command Line Arguments

    Since you begged for the rep... And since you did an excellent job, +rep!

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem with command line arguments
    By csepraveenkumar in forum Java Help
    Replies: 1
    Last Post: 05-17-2011, 09:43 AM
  2. Command line arguments in DOS
    By Actor in forum Computer Software/OS
    Replies: 0
    Last Post: 05-18-2010, 12:57 AM
  3. Command Line Arguments
    By whitey6993 in forum C Tutorials
    Replies: 0
    Last Post: 04-04-2010, 12:57 PM
  4. [X86 ASM] Command line arguments demo
    By Sysop_fb in forum Classes and Code Snippets
    Replies: 0
    Last Post: 02-12-2010, 07:38 AM
  5. Reading Command line arguments in VB.NET
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 06-06-2009, 06:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

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