aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/getopt.tes
blob: 4ecfa3f1235e884187a0b00485872a618f2139e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
!*$
 * M[getopt] -> First non-flag argument -- Parse options
 *
 * Parses command line options according to the string
 * \(lqoptstring\(rq, similar to \fBgetopt\P(3).
 * \(lqoptstring\(rq is a list of single character
 * option names.
 * If followed by a colon, the option takes an argument
 * (which can be part of the same command line argument as in
 * \(lq-Ofoo\(rq).
 * If followed by two colons, the option argument is optional.
 * For every detected option \fIX\fP, a register \fBgetopt.\fIX\fR is
 * defined with -1 (true) in its integer part.
 * If the option had an argument, it is stored in the register's
 * string part.
 *
 * Returns the index of the first command-line argument that does not
 * belong to a flag or -1 in case of parsing errors.
 *!
[optstring]
@[getopt]{
  <%.j
    :Q[\.j]:;
    0Q[\.j]--"N 1; '
  !next!

    1Q[\.j]U.f
    Q.f--"= !* -- *! Q.j+1 '

    !* j-th argument is -f *!
    0U.i <
      Q.iQ[optstring]U.o Q.o"< -1 '

      !* count number of : after o *!
      0U.#ar <%.iQ[optstring]-:"N 1; ' %.#ar>

      Q.f-Q.o"=
        -U[getopt.U.o]
        Q.#ar">
          2Q[\.j]"<
            !* -f arg *!
            %.j
            Q.#ar-1"> !* optional arg *!
              :Q[\.j]"< Q.j '
              0Q[\.j]--"= Onext '
            |
              :Q[\.j]"< -1 '
            '
            EU[getopt.U.o]Q[\.j]
          |
            !* -farg *!
            EU[getopt.U.o]Q[\.j]
            [* EQ[getopt.U.o] 2D ]*
          '
        '
        1;
      '
    >
  >
Q.j}