xref: /openbsd/usr.sbin/pkg_add/OpenBSD/Getopt.pod (revision 4999ab86)
1$OpenBSD: Getopt.pod,v 1.2 2023/05/21 13:44:56 espie Exp $
2
3=head1 NAME
4
5OpenBSD::Getopt - Process single-characters switches
6
7=head1 SYNOPSIS
8
9   use OpenBSD::Getopt;
10
11   my $h = { 'v' =>
12	    sub() {
13		++$opt_v;
14	    };
15   };
16   getopts('oifv:', $h);
17
18=head1 DESCRIPTION
19
20This is similar to L<getopt(3)>. One call to C<getopts($optstring, $h)> parses
21all the options using the C<$optstring> as a list of simple switches
22(letter) and switches with arguments (letter followed by C<:>).
23
24Option values are written into the hash C<$h>.
25Contrary to L<getopt(3)>, C<$h-E<gt>{v}> is incremented each time the switch is
26seen, to allow for stuff like C<-vv>.
27
28Alternately, a code ref can be put into the hash for a given switch.
29When a switch is seen, the sub C<$foo> is called as
30C<&$foo()> for a simple switch and as C<&$foo(option_value)> for a switch
31with argument.
32