SCAN_OPTION 3.01
Copyright (C) 1996,1997,1999 Taiji Yamada <taiji@aihara.co.jp>
[Back] [Japanese Page]
NAME
scan_option - Routine to analyze command-line option
SYNOPSIS
#include <option.h>
int scan_option(int argc, char *argv[], char *key, ...);
argc - the number of command-line arguments
argv - command-line arguments
key, ... - key string and multi-variable arguments
DESCRIPTION
-
If there is the key string to scan in command-line options, true is returned.
For an example, if there is "-help" in command-line options,
scan_option(argc, argv, "-help")
returns true.
If there is the key string to scan with scanf-style format, variables are
set by scanf style and true is returned. For an example, if there
is "-geom 100x100" in command-line options,
scan_option(argc, argv, "-geom %dx%d", &width, &height)
sets 100 and 100 to width and height,
respectively, and returns true.
If there is the key string to scan with scanf-style format of "%s",
a variable is set at the next string of the argument matched to key and
returns true. For an example, if there is "-title A string"
in command-line options and "A string" is one element in the command-line
arguments,
scan_option(argc, argv, "-title %s", title)
sets "A string" to title and returns true.
If there is the key string to scan with scanf-style format of "\"%s\"",
a variable is set at the next double-quoted strings of the argument matched
to key and returns true. For an example, if there is '-title
\"A string\"' in command-line options and whether "A string" is one
element or not,
scan_option(argc, argv, "-title \"%s\"", title)
sets "A string" to title and returns true. If
there is no double-quoted string at the previous argument of the matched
key, it processes the same as "-title %s" does.
If the key string is such as "-on[-]", it is considered to be
flag-type option and multi-variable arguments need a int-type
pointer. For an example, if there is "-on" or "-on-"
in command-line options,
scan_option(argc, argv, "-on[-]", &flag)
substitutes !0 or 0 into flag and returns true.
In addition, if the key string is such as "[-+]mode" or "[+-]mode",
it is also considered to be flag-type option and multi-variable arguments
need a int-type pointer. For an example, if there is "-mode"
or "+mode" in command-line options,
scan_option(argc, argv, "[-+]mode", &flag)
substitutes !0 or 0 into flag and returns true. On the
other hand, if there is "+mode" or "-mode" in command-line
options,
scan_option(argc, argv, "[+-]mode", &flag)
substitutes !0 or 0 into flag and returns true.
If the key string is such as "-color Red Green Blue" which includes some
blanks without scanf-style format, it is considered to be enumerate-type
option and multi-variable arguments need a int-type pointer. For
an example, there is "-color Blue" in command-line options,
scan_option(argc, argv, "-color Red Green Blue", &color)
substitutes 2 into color and returns true.
In addition to a returned value of scan_option, the above true
value is, to put it more precisely, the index of argument matched to key
string. To use this specification, for an example, the following usage
is available.
if ((c=scan_option(argc, argv, "-f")) {
c++; n = 0;
while (c<argc)
strcpy(files[n++], argv[c++]);
}
Author