Differences

This shows you the differences between two versions of the page.

Link to this comparison view

getopt [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== Use getopt in c/c++ ====
  
 +<​code>​
 +// getopt.cpp
 +//
 +// $Id$
 +//
 +#include <​unistd.h>​
 +#include <​stdio.h>​
 +#include <​getopt.h>​
 +
 +
 +using namespace std;
 +
 +int main(int argc, char **argv);
 +
 +int main(int argc, char **argv) {
 +    int c;
 +    int digit_optind = 0;
 +    while (1) {
 +        int options_index = 0;
 +        static struct option long_options[] = {
 +            { "​date",​ 1, 0, 0},
 +        };
 +
 +        c = getopt_long(argc,​ argv, "​d:",​
 +            long_options,​ &​options_index);​
 +        if (c == -1) break;
 +
 +        switch (c) {
 +            case 0:
 +                printf("​options %s", long_options[options_index].name);​
 +                if (optarg) printf("​ with arg %s ", optarg);
 +                printf("​\n"​);​
 +                break;
 +
 +            case '​d':​
 +                printf("​option d with value '​%s'​\n",​ optarg);
 +                break;
 +        }
 +        if (optind < argc) {
 +            printf("​non-option argv elements"​);​
 +            while (optind < argc) {
 +                printf("​%s ", argv[optind++]);​
 +                printf("​\n"​);​
 +            }
 +        }
 +    }
 +    return 0;
 +}
 +</​code>​
 +
 +
 +----
 +  * [[cplusplus|Back to C++]]
getopt.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0