Difference between revisions of "C programming/Command line options"

From Teknologisk videncenter
Jump to: navigation, search
m (New page: Example of how to use Command line options. <source lang=c> #include <errno.h> #include <stdio.h> #include <string.h> #include <stdlib.h> void usage( char *name) { printf("Usage: \...)
 
m
Line 14: Line 14:
 
         int c;
 
         int c;
 
         extern char *optarg;
 
         extern char *optarg;
         while(( c = getopt(argc,argv, "l:s:b:" )) != EOF ) {    // Loop the argu                                  ments
+
         while(( c = getopt(argc,argv, "l:s:b:" )) != EOF ) {    // Loop the arguments
 
                 switch(c) {
 
                 switch(c) {
 
                 case 'l': printf("l option %s\n",optarg);
 
                 case 'l': printf("l option %s\n",optarg);

Revision as of 09:18, 23 May 2018

Example of how to use Command line options.

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void usage( char *name) {
        printf("Usage: \n");
        printf("%s [-l loginname] [-s safetylevel] [-b blocksize]\n",name);
        exit(1);
}

int main( int argc, char *argv[] ) {
        int c;
        extern char *optarg;
        while(( c = getopt(argc,argv, "l:s:b:" )) != EOF ) {    // Loop the arguments
                switch(c) {
                case 'l': printf("l option %s\n",optarg);
                          break;
                case 's': printf("s option %s\n",optarg);
                          break;
                case 'b': printf("b option %s\n",optarg);
                          break;
                default:  usage( argv[0]);
                }
        }
        return(0);
}