使用 GNU getopt 工具
應用程式的命令列選項與 C 語言的命令列引數的處理方式不同。它們只是在 Linux 或 Unix 環境中傳統上以破折號(-
)開頭的引數。
使用 glibc 在 Linux 或 Unix 環境中,你可以使用 getopt 工具從其餘引數中輕鬆定義,驗證和解析命令列選項。
這些工具希望根據 GNU 編碼標準格式化你的選項, GNU 編碼標準是 POSIX 為命令列選項格式指定的擴充套件。
下面的示例演示瞭如何使用 GNU getopt 工具處理命令列選項。
#include <stdio.h>
#include <getopt.h>
#include <string.h>
/* print a description of all supported options */
void usage (FILE *fp, const char *path)
{
/* take only the last portion of the path */
const char *basename = strrchr(path, '/');
basename = basename ? basename + 1 : path;
fprintf (fp, "usage: %s [OPTION]\n", basename);
fprintf (fp, " -h, --help\t\t"
"Print this help and exit.\n");
fprintf (fp, " -f, --file[=FILENAME]\t"
"Write all output to a file (defaults to out.txt).\n");
fprintf (fp, " -m, --msg=STRING\t"
"Output a particular message rather than 'Hello world'.\n");
}
/* parse command-line options and print message */
int main(int argc, char *argv[])
{
/* for code brevity this example just uses fixed buffer sizes for strings */
char filename[256] = { 0 };
char message[256] = "Hello world";
FILE *fp;
int help_flag = 0;
int opt;
/* table of all supported options in their long form.
* fields: name, has_arg, flag, val
* `has_arg` specifies whether the associated long-form option can (or, in
* some cases, must) have an argument. the valid values for `has_arg` are
* `no_argument`, `optional_argument`, and `required_argument`.
* if `flag` points to a variable, then the variable will be given a value
* of `val` when the associated long-form option is present at the command
* line.
* if `flag` is NULL, then `val` is returned by `getopt_long` (see below)
* when the associated long-form option is found amongst the command-line
* arguments.
*/
struct option longopts[] = {
{ "help", no_argument, &help_flag, 1 },
{ "file", optional_argument, NULL, 'f' },
{ "msg", required_argument, NULL, 'm' },
{ 0 }
};
/* infinite loop, to be broken when we are done parsing options */
while (1) {
/* getopt_long supports GNU-style full-word "long" options in addition
* to the single-character "short" options which are supported by
* getopt.
* the third argument is a collection of supported short-form options.
* these do not necessarily have to correlate to the long-form options.
* one colon after an option indicates that it has an argument, two
* indicates that the argument is optional. order is unimportant.
*/
opt = getopt_long (argc, argv, "hf::m:", longopts, 0);
if (opt == -1) {
/* a return value of -1 indicates that there are no more options */
break;
}
switch (opt) {
case 'h':
/* the help_flag and value are specified in the longopts table,
* which means that when the --help option is specified (in its long
* form), the help_flag variable will be automatically set.
* however, the parser for short-form options does not support the
* automatic setting of flags, so we still need this code to set the
* help_flag manually when the -h option is specified.
*/
help_flag = 1;
break;
case 'f':
/* optarg is a global variable in getopt.h. it contains the argument
* for this option. it is null if there was no argument.
*/
printf ("outarg: '%s'\n", optarg);
strncpy (filename, optarg ? optarg : "out.txt", sizeof (filename));
/* strncpy does not fully guarantee null-termination */
filename[sizeof (filename) - 1] = '\0';
break;
case 'm':
/* since the argument for this option is required, getopt guarantees
* that aptarg is non-null.
*/
strncpy (message, optarg, sizeof (message));
message[sizeof (message) - 1] = '\0';
break;
case '?':
/* a return value of '?' indicates that an option was malformed.
* this could mean that an unrecognized option was given, or that an
* option which requires an argument did not include an argument.
*/
usage (stderr, argv[0]);
return 1;
default:
break;
}
}
if (help_flag) {
usage (stdout, argv[0]);
return 0;
}
if (filename[0]) {
fp = fopen (filename, "w");
} else {
fp = stdout;
}
if (!fp) {
fprintf(stderr, "Failed to open file.\n");
return 1;
}
fprintf (fp, "%s\n", message);
fclose (fp);
return 0;
}
它可以用 gcc
編譯:
gcc example.c -o example
它支援三種命令列選項(--help
,--file
和 --msg
)。所有人都有短形式(-h
,-f
和 -m
)。file
和 msg
選項都接受引數。如果指定 msg
選項,則其引數是必需的。
選項的引數格式為:
--option=value
(適用於長形選項)-ovalue
或-o"value"
(適用於短格式選項)