i am not sure i understand the question exactly but i am in the same stage as you.
last night i could not sleep so i wrote this little program to experiment with the options. i never tried use long options before. i try to use the getopt function when ever i can.
but if i was to use getopt function or the argp function i would use it in a slightly larger program and i would think more of the design before writing anything.
as far as i know the options has just a switch '-' or '--' as a UNIX tradition. maybe it is because it was the most easy to parse and became a standard.
however i am not sure of the exact reason. but from, what i understand then in the end it is not needed to use the get opt function for very few options and not all systems has the library but for bigger programs it is the best approach.
Code:
/*
* usage; a simple yes program that print 'y' to stdout unless string giving as second option
* synopsis
* yes [option]
* yes [string]
* yes [default , will print'y' to stdout]
*
* options
* --version
* --help
* --license
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void help();
void license();
void version();
int main(int argc, char** argv)
{
int i;
if( argc == 1)
{
for(;;)
fprintf(stdout,"y\n");
}
if(argv[1][0] == '-' && argv[1][1] == 'i')
{
for(;;)
fprintf(stdout,"%s\n",argv[2]);
}
for(i=1;i < argc;i++)
{
if(strcmp(argv[i],"--version") == 0)
{
version(argv[0]);
}
else if (strcmp(argv[i],"--license") == 0)
{
license(argv[0]);
}
else if (strcmp(argv[i],"--help") == 0)
{
help(argv[0]);
} else {
for(;;)
fprintf(stdout,"%s\n",argv[i]);
}
}
return 0;
}
void help(const char *progname)
{
printf("%s: usage no option [option] [string] \n"
"\t--version prints version number to stdout\n"
"\t--help this help \n"
"\t--license prints license to stdout\n"
"\tno option prints singel char y to stdout until program killed\n"
"\t%s [string] prints string to stdout until program killed\n",progname,progname);
exit(0);
}
void license(const char *progname)
{
fprintf(stdout,"%s\tauthor cmay 2009\n license BSD\n",progname);
exit(0);
}
void version(const char *progname)
{
fprintf(stdout,"%s\tversion 0.0.1\n",progname);
exit(0);
}