Added version number and online help.

This commit is contained in:
heinzel
2011-01-12 14:50:21 +00:00
parent 9ca5d09f4b
commit a020f2e816
2 changed files with 77 additions and 3 deletions

12
hello.1
View File

@@ -1,18 +1,24 @@
.\" hello.1
.TH HELLO 1 "Nov. 2007" "heinzel" "User Commands"
.TH HELLO 1 "Jan. 2011" "heinzel" "User Commands"
.SH NAME
hello \- Send greetings to everyone
.SH SYNOPSIS
.B hello
.B hello
.B [\fIOPTION\fR]
.SH DESCRIPTION
.B hello
print the message "Hello World." to standard output.
.SH OPTIONS
none
.TP
.B -h
Print a short help text.
.TP
.B -V
Print the version number.
.SH BUGS
.B hello

68
hello.c
View File

@@ -2,11 +2,79 @@
* hello.c
*/
// err()
#include <err.h>
// getopt()
#include <unistd.h>
// basename()
#include <libgen.h>
// printf(), fprintf()
#include <stdio.h>
// exit()
#include <stdlib.h>
// strdup()
#include <string.h>
// EX_... constants
#include <sysexits.h>
/* Symbols */
#define VERSION "0.1"
/* Prototypes */
void print_version(void);
void print_help(void);
/* Global Objects */
char *progname;
/*
* main
*/
int main(int argc, char** argv) {
char c;
/* Store program name without path
* (will be used in e.g. error messages) */
if( (progname = strdup(basename(argv[0]))) == NULL )
err(EX_OSERR, NULL);
while( (c = getopt(argc, argv, "hV")) >= 0 ) {
switch(c) {
case 'h':
print_help();
exit(EX_OK);
case 'V':
print_version();
exit(EX_OK);
case '?':
fprintf(stderr, "Try `-h´ for more information.\n");
exit(EX_USAGE);
default:
fprintf(stderr, "getopt() is on fire.\n");
exit(EX_SOFTWARE);
}
}
if(optind < argc) {
fprintf(stderr, "%s: Invalid argument: %s\n", progname, argv[optind]);
fprintf(stderr, "Try `-h´ for more information.\n");
exit(EX_USAGE);
}
printf("Hello World.\n");
exit(EX_OK);
}
void print_version(void) {
printf("%s %s\n", progname, VERSION);
}
void print_help(void) {
printf("hello - Send greetings to everyone\n");
printf("\n");
printf("Usage: %s [-hV]\n\n", progname);
printf("Options:\n");
printf(" -h Print this help text.\n");
printf(" -V Print version number.\n");
printf("\n");
}