|
��������
getopt - ������ ����� �������
���������
int getopt (argc, argv, optstring) int argc; char **argv, *optstring; extern char *optarg; extern int optind, opterr;
��������
������� getopt ���������� ��������� ���� �� �������
argv, ��������������� �����-���� ����� � �������
optstring. ������� getopt ������������ ��� �������
������������ ���������� ������ [��. intro(1), �������
3 10]. ����� ����� ������� ��������� ���� ��������, ���
������� ����������� ���������� � �������� ������������
����� ������� ������������ ������� getopts(1) ��� ����������� ������� getopt.
������� optstring ������ ��������� �����, ������� ����� �������������� ����������, ������������ getopt. ���� �� ������ ������� ���������, �� ��������������, ��� ����� ����� �������� ��� ������ ����������, ���������� �� ����� ��������� ��� �����������.
� ���������� optarg ���������� ��������� �� ������ ��������� ������������ �����.
����� ������� ������ ������� getopt �������� �� ������� ���������� optind ������ ���� �������� ������� argv, ������� ������ �������������� ���������. �������� ���������� optind �� ������� ��������� � ������� getopt ����� 1.
����� ��� ����� ���������� (�� ���� ���������� ������ �� ����������, �� �������� � ������ �����), ��������� ������� getopt ����� -1. ����� ������������ ����������� ����� --, ����� �������� ����� �����. ����� ��� ���������� ��� �������, ����� ���������� �������� -1, � ���� ����� -- ����� ���������.
�����������
������� getopt ������ ��������� �� ������ � �����������
�������� � ���������� �������������� ���� ?, �����
����������� ����, �� �������� � ������� optstring, ���
�� ����������� ��������� � �����, ��������� ��������.
������ ��������� �� ������ ����� ���� ���������, ���
���� ���������� opterr ������� ��������� 0.
������ ��������� �������� ��������� ����������, ��� ����� ���������� ��������� �������, ����������� ����������������� ����� a � b, � ����� ����� o, ������� ������ ����� ��������:
main (argc, argv) int argc; char **argv; { int c; extern char *optarg; extern int optind; ... while ((c=getopt (argc,argv,"abo:")) != -1) switch (c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else bproc (); break; case 'o': ofile = optarg; break; case '?': errflg++; } if (errflg) { (void) fprintf (stderr, "usage: ... "); exit (2); } for ( ; optind < argc; optind++) { if (access (argv [optind], 4)) { ... }
���������������
���� ������� ���������� ������� getopt ��������� ��������� ����������� �� ������, �������� ��������� ������
[��. intro(1)], ���� �� ������� ������������, ��� ���
�������� ����������� ����� �� �������������� � ��������� ������� �������. �����, ��� � � �������, a � b ����� ��� ����������, � ����� o ��������� ��������.
���������� ��� �������� ����������� �� ������:
cmd -aboxxx file����� �������� ������� 5: ����� � ���������� �� ������ ���� � ����� ������ � ���������� �������.
cmd -ab -oxxx file�������� ������� 6: ����� �����, ������� ��������� ��������, ������ ���� ������ ��� ���������.
��������� �������� ���������� optind ��� ������ ������� getopt � ���������� ���������� argv ����� �������� � ��������������� �����������.
��. �����
getopts(1), intro(1) � ����������� ������������.
|