Added version number and online help.
This commit is contained in:
12
hello.1
12
hello.1
@@ -1,18 +1,24 @@
|
|||||||
.\" hello.1
|
.\" hello.1
|
||||||
.TH HELLO 1 "Nov. 2007" "heinzel" "User Commands"
|
.TH HELLO 1 "Jan. 2011" "heinzel" "User Commands"
|
||||||
|
|
||||||
.SH NAME
|
.SH NAME
|
||||||
hello \- Send greetings to everyone
|
hello \- Send greetings to everyone
|
||||||
|
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B hello
|
.B hello
|
||||||
|
.B [\fIOPTION\fR]
|
||||||
|
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.B hello
|
.B hello
|
||||||
print the message "Hello World." to standard output.
|
print the message "Hello World." to standard output.
|
||||||
|
|
||||||
.SH OPTIONS
|
.SH OPTIONS
|
||||||
none
|
.TP
|
||||||
|
.B -h
|
||||||
|
Print a short help text.
|
||||||
|
.TP
|
||||||
|
.B -V
|
||||||
|
Print the version number.
|
||||||
|
|
||||||
.SH BUGS
|
.SH BUGS
|
||||||
.B hello
|
.B hello
|
||||||
|
|||||||
68
hello.c
68
hello.c
@@ -2,11 +2,79 @@
|
|||||||
* hello.c
|
* hello.c
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// err()
|
||||||
|
#include <err.h>
|
||||||
|
// getopt()
|
||||||
|
#include <unistd.h>
|
||||||
|
// basename()
|
||||||
|
#include <libgen.h>
|
||||||
|
// printf(), fprintf()
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
// exit()
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
// strdup()
|
||||||
|
#include <string.h>
|
||||||
|
// EX_... constants
|
||||||
#include <sysexits.h>
|
#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) {
|
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");
|
printf("Hello World.\n");
|
||||||
exit(EX_OK);
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user