一个小模板

/*
 *2010-1.4
 */
#include <stdio.h>  /*提供标准I/O函数原型*/
#include <stdlib.h> /*提供EXIT_SUCCESS和EXIT_FAILURE*/
#include <unistd.h> /*提供getopt()*/

struct globalArgs_t {
	char *arg1;
	char *arg2;
	char *arg3;
}globalArgs;

static const char *optString = "a:b:c:h";

void display_usage(void)
{
	puts( " a standard commandline app template.\n" 
	      " usage: ./test_arg -a arg1 -b arg2 -c arg3 [-h] \n"
	      " \t -h: print this mesage.\n"
	      " \t -a: input arg1.\n"
	      " \t -b: input arg2.\n"
	      " \t -c: input arg3.\n");
	exit( EXIT_FAILURE );
}

void do_something(char *arg1, char *arg2, char *arg3)
{
	printf( "get args : %s, %s, %s.\n", arg1,arg2,arg3 );
}
int main(int argc,char *argv[])
{
	int opt = 0;

	/*initialize globalArgs before we get to work.*/
	globalArgs.arg1 = NULL;
	globalArgs.arg2 = NULL;
	globalArgs.arg3 = NULL;

	opt = getopt( argc, argv, optString );
	while( opt !=-1 ) {
		switch( opt ) {
			case 'a':
				globalArgs.arg1 = optarg;
				break;
			case 'b':
				globalArgs.arg2 = optarg;
				break;
			case 'c':
				globalArgs.arg3 = optarg;
				break;
			case 'h':
				display_usage();
				break;
			default:
				/* you won't actually get here. */
				break;
		}
		opt = getopt( argc, argv, optString);
	}

	do_something( globalArgs.arg1,globalArgs.arg2,globalArgs.arg3 );

	return EXIT_SUCCESS;

}

你可能感兴趣的:(C++,c,C#)