GETOPT(3) Linux Programmer's Manual GETOPT(3)
NAME
getopt - Parse command line options
SYNOPSIS
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
DESCRIPTION
The getopt() function parses the command line arguments.
Its arguments argc and argv are the argument count and
array as passed to the main() function on program invoca-
tion. An element of argv that starts with `-' (and is not
exactly "-" or "--") is an option element. The characters
of this element (aside from the initial `-') are option
characters. If getopt() is called repeatedly, it returns
successively each of the option characters from each of
the option elements.
If getopt() finds another option character, it returns
that character, updating the external variable optind and
a static variable nextchar so that the next call to
getopt() can resume the scan with the following option
character or argv-element.
If there are no more option characters, getopt() returns
EOF. Then optind is the index in argv of the first argv-
element that is not an option.
optstring is a string containing the legitimate option
characters. If such a character is followed by a colon,
the option requires an argument, so getopt places a
pointer to the following text in the same argv-element, or
the text of the following argv-element, in optarg. Two
colons mean an option takes an optional arg; if there is
text in the current argv-element, it is returned in
optarg, otherwise optarg is set to zero.
By default, getargs() permutes the contents of argv as it
scans, so that eventually all the non-options are at the
GNU Aug 30, 1995 1
GETOPT(3) Linux Programmer's Manual GETOPT(3)
end. Two other modes are also implemented. If the first
character of optstring is `+' or the environment variable
POSIXLY_CORRECT is set, then option processing stops as
soon as a non-option argument is encountered. If the
first character of optstring is `-', then each non-option
argv-element is handled as if it were the argument of an
option with character code 1. (This is used by programs
that were written to expect options and other argv-ele-
ments in any order and that care about the ordering of the
two.) The special argument `--' forces an end of option-
scanning regardless of the scanning mode.
If getopt() does not recognize an option character, it
prints an error message to stderr, stores the character in
optopt, and returns `?'. The calling program may prevent
the error message by setting opterr to 0.
The getopt_long() function works like getopt() except that
it also accepts long options, started out by two dashes.
Long option names may be abbreviated if the abbreviation
is unique or is an exact match for some defined option. A
long option may take a parameter, of the form --arg=param
or --arg param.
longopts is a pointer to the first element of an array of
struct option declared in <getopt.h> as
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
The meanings of the different fields are:
name is the name of the long option.
has_arg
is: no_argument (or 0) if the option does not take
an argument, required_argument (or 1) if the option
requires an argument, or optional_argument (or 2)
if the option takes an optional argument.
flag specifies how results are returned for a long
option. If flag is NULL, then getopt_long()
returns val. (For example, the calling program may
set val to the equivalent short option character.)
Otherwise, getopt_long() returns 0, and flag points
to a variable which is set to val if the option is
found, but left unchanged if the option is not
found.
val is the value to return, or to load into the
GNU Aug 30, 1995 2
GETOPT(3) Linux Programmer's Manual GETOPT(3)
variable pointed to by flag.
The last element of the array has to be filled with
zeroes.
If longindex is not NULL, it points to a variable which is
set to the index of the long option relative to longopts.
getopt_long_only() is like getopt_long(), but `-' as well
as `--' can indicate a long option. If an option that
starts with `-' (not `--') doesn't match a long option,
but does match a short option, it is parsed as a short
option instead.
RETURN VALUE
The getopt() function returns the option character if the
option was found successfully, `:' if there was a missing
parameter for one of the options, `?' for an unknown
option character, or EOF for the end of the option list.
getopt_long() and getopt_long_only() also return the
option character when a short option is recognized. For a
long option, they return val if flag is NULL, and 0 other-
wise. Error and EOF returns are the same as for getopt(),
plus `?' for an ambiguous match or an extraneous parame-
ter.
ENVIRONMENT VARIABLES
POSIXLY_CORRECT
If this is set, then option processing stops as
soon as a non-option argument is encountered.
EXAMPLE
The following example program, from the source code,
illustrates the use of getopt_long() with most of its fea-
tures.
#include <stdio.h>
int
main (argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
{"add", 1, 0, 0},
GNU Aug 30, 1995 3
GETOPT(3) Linux Programmer's Manual GETOPT(3)
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, 0, 'c'},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digit
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
opensc-0.11.13.tar.gz_Opensc_pkcs11_智能卡 (620个子文件)
pkcs15-init.1 12KB
netkey-tool.1 5KB
pkcs11-tool.1 5KB
pkcs15-crypt.1 4KB
opensc-explorer.1 4KB
pkcs15-tool.1 4KB
westcos-tool.1 3KB
cryptoflex-tool.1 3KB
opensc-config.1 2KB
opensc-tool.1 2KB
cardos-tool.1 2KB
compat_strlcpy.3 4KB
sc_establish_context.3 2KB
sc_file_t.3 2KB
sc_asn1_decode.3 2KB
sc_app_info_t.3 2KB
sc_wait_for_event.3 2KB
sc_write_record.3 2KB
sc_update_record.3 2KB
sc_update_binary.3 2KB
sc_set_card_driver.3 2KB
sc_write_binary.3 2KB
sc_select_file.3 2KB
sc_read_record.3 2KB
sc_card_t.3 2KB
sc_connect_card.3 2KB
sc_read_binary.3 2KB
sc_asn1_put_tag.3 2KB
sc_asn1_find_tag.3 2KB
sc_base64_encode.3 2KB
sc_asn1_encode.3 2KB
sc_append_record.3 1KB
sc_put_data.3 1KB
sc_get_data.3 1KB
sc_find_app_by_aid.3 1KB
sc_detect_card_presence.3 1KB
sc_asn1_read_tag.3 1KB
sc_check_sw.3 1KB
sc_card_ctl.3 1KB
sc_disconnect_card.3 1KB
sc_update_dir.3 1KB
sc_list_files.3 1KB
sc_find_pkcs15_app.3 1KB
sc_format_asn1_entry.3 1KB
sc_format_apdu.3 1KB
sc_asn1_entry.3 1KB
sc_copy_asn1_entry.3 1KB
sc_enum_apps.3 1KB
sc_base64_decode.3 1KB
sc_delete_record.3 1KB
sc_get_challenge.3 1KB
sc_lock.3 1KB
sc_get_cache_dir.3 1KB
sc_release_context.3 1KB
sc_asn1_verify_tag.3 1KB
sc_make_cache_dir.3 1KB
sc_file_dup.3 1KB
sc_asn1_skip_tag.3 1KB
sc_asn1_print_tags.3 1KB
sc_transmit_apdu.3 1KB
sc_unlock.3 1KB
sc_delete_file.3 1KB
sc_card_valid.3 1KB
sc_der_copy.3 1KB
sc_create_file.3 1KB
sc_strerror.3 1KB
sc_free_apps.3 1KB
sc_file_new.3 1KB
sc_der_clear.3 1KB
sc_file_free.3 1KB
pkcs15-profile.5 2KB
configure.ac 19KB
Makefile.am 3KB
Makefile.am 3KB
Makefile.am 2KB
Makefile.am 2KB
Makefile.am 2KB
Makefile.am 1KB
Makefile.am 1KB
Makefile.am 1000B
Makefile.am 958B
Makefile.am 924B
Makefile.am 856B
Makefile.am 787B
Makefile.am 433B
Makefile.am 420B
Makefile.am 184B
Makefile.am 169B
Makefile.am 149B
Makefile.am 114B
Makefile.am 89B
opensc-install.bat 1023B
cardos-info.bat 104B
bintest 512B
bootstrap 203B
pkcs11-tool.c 103KB
pkcs15-lib.c 101KB
framework-pkcs15.c 91KB
card-oberthur.c 68KB
pkcs15-init.c 66KB
共 620 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
JonSco
- 粉丝: 67
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0