2

I want to set arguments to c Program like this.

$./Program_name -a 100 -b 5 -c 30 

and I want to use this value a, b, c in the program.

For ex:

int count = a;
int number = b;
int limit_exec = c;

I don't know how it works. I couldn't find it in Google... I'd like to see some examples.

Also, in the same time, I want to make default and limits of values.

  1. Set limits like this: Value a can only have a number from 1 to 10000.

  2. Using default when I didn't put value like this:

    $./program -a 100 -c 30    // then value 'b' should use default number.
    

    I don't know how to set default. Is it okay to set default at c code like this?

       #define a 50   
       #define b 100   
       #define c 30
    
Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
  • 5
    One of the most common libraries for parsing options is called getopt. It might help your googling process if you look specifically for getopt examples in C. – jez Oct 08 '14 at 02:32
  • Oh! I didn't know it is called parsing options. Thank you. Then how can I set default of parsing options? –  Oct 08 '14 at 02:43
  • 1
    Just initialize the variables to defaults before your option parsing (and validity checking) logic. If the values are invalid or not present, they won't be overwritten. – polarysekt Oct 08 '14 at 03:07
  • Thank you:D I didn't think of that;; It was not complicate problem at all..Thanks a lot! –  Oct 08 '14 at 03:13
  • This is my final question.. Is there any better idea to set limits? I am trying to write a code like this. if(a>0 && a<10000) { perror("error"); exit(0); } –  Oct 08 '14 at 03:17
  • Amongst many other questions, look at [Parsing command line arguments](http://stackoverflow.com/questions/9642732/parsing-command-line-arguments/9643032#9643032). Searching in SO with '`[c] getopt`' will give you many more questions and answers about using `getopt()` — or `getopt_long()`. – Jonathan Leffler Oct 08 '14 at 06:13

1 Answers1

4

You can use getopt to parse the command line input.
Please see man page for a brief idea.

$ man 3 getopt

The following code works as per your requirement.

Code:

#include <stdio.h>
#include <unistd.h> /* Definition of getopt */
#include <stdlib.h> /* Definition of atoi */ 

int main (int argc, char ** argv)
{
    char option;

    /* Default Values */
    int count = 10, number = 20, limit = 30;

    while (-1 != (option = getopt (argc, argv, "a:b:c:")))
    {   
        switch (option)
        {
            case 'a':
                count = atoi(optarg);

                /* Check for limit */
                if (count <= 0 || count > 1000)
                {
                        printf("Usage <%s> [a] value, Range of value : 1-1000\n", argv[0]);
                        exit(EXIT_FAILURE);
                }
                break;

            case 'b':
                number = atoi(optarg);
                break;

            case 'c':
                limit = atoi(optarg);
                break;

            default:
                printf ("Usage: <%s> [a] value [b] value [c] value\n", argv[0]);
        }
    }

    printf("\nCount:[%d]\tNumber:[%d]\tlimit:[%d]\n\n", count, number, limit);
    return 0;
}

Compilation: gcc -o exe filename.c -Wall

Execution:

$ ./exe     /* Default values */
Count:[10]  Number:[20] limit:[30]

$ ./exe -a 1 -b 2 -c 3
Count:[1]   Number:[2]  limit:[3]

$ ./exe -a 1  -c 3
Count:[1]   Number:[20] limit:[3]

$ ./exe -a 9999  /* Same output for ./exe -a -1 */
Usage <./exe> [a] value, Range of Value : 1-1000
Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
  • What are the `x_flag` variables for? – Jonathan Leffler Oct 08 '14 at 06:58
  • @JonathanLeffler, Code updated : Removed redundant variable. Added `-Wall` for ensuring no warnings. – Saurabh Meshram Oct 08 '14 at 08:30
  • 1
    Speaking of warnings, `-Wall` doesn't enable all of them, (nor does this but it enables more): `-Wextra` – Jite Oct 08 '14 at 11:15
  • +1: but I would like to suggest that the usage message should: (1) be written to standard error, (2) be followed by `return 1;` in `main()` or `exit(1);` elsewhere, and (3) should reflect the command line you'd type better. For example, `fprintf(stderr, "Usage: %s [-a count] [-b number] [-c limit] [file ...]", argv [0]);`. That indicates that the options are all optional (the square brackets), more or less identifies what value each option takes, and notes that file names can be supplied too. – Jonathan Leffler Oct 08 '14 at 14:06
  • Waah; I missed out the newline `\n` before the close quote. Too early in the morning and far too much blood in my caffeine stream. – Jonathan Leffler Oct 08 '14 at 14:40