File: alt_getopt.awk

package info (click to toggle)
runawk 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: awk: 1,127; ansic: 736; sh: 420; makefile: 103
file content (225 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
#        http://sourceforge.net/projects/runawk
#
############################################################

# =head2 alt_getopt.awk
#
# =over 2
#
# =item I<getopt(SHORT_OPTS)>
#
# This function processes ARGV array and
# returns TRUE if option is received, 
# received option is saved in 'optopt' variable, option argument (if any)
# is saved in 'optarg' variable. Long options (like --help or
# --long-option) present in GNU libc and BSD systems are also supported.
#
# NOTE: alt_getopt.awk module follows rules
# from SUS/POSIX "Utility Syntax Guidelines"
#
# =back
#

###############################################################
# #                       EXAMPLE

# #!/usr/bin/env runawk
#
# #use "alt_getopt.awk"
#
# BEGIN {
#     long_opts ["verbose"] = "v"
#     long_opts ["help"]    = "h"
#     long_opts ["fake"]    = ""
#     long_opts ["len"]     = takes_arg
#     long_opts ["output"]  = "o"
#
#     while (getopt("hVvo:n:")){
#         if (optopt == "h"){
#             print "option `h'"
#         }else if (optopt == "V"){
#             print "option `V'"
#         }else if (optopt == "v"){
#             print "option `v'"
#         }else if (optopt == "o"){
#             print "option `o':", optarg
#         }else if (optopt == "n"){
#             print "option `n':", optarg
#         }else if (optopt == "fake"){
#             print "option `fake'"
#         }else if (optopt == "len"){
#             print "option `len':", optarg
#         }else{
#             abort()
#         }
#     }
#
#     for (i=1; i < ARGC; ++i){
#         if (ARGV [i] != "")
#             printf "ARGV [%s] = %s\n", i, ARGV [i]
#     }
#
#     exit 0
# }
###############################################################

#use "alt_assert.awk"

function __getopt_errexit (msg, status)
{
	print msg > "/dev/stderr"
	exitnow(status)
}

function __getopt_addstdin (                  i)
{
	for (i=1; i < ARGC; ++i){
		if (ARGV [i] != __getopt_fill){
			return
		}
	}

	ARGV [1] = "-"
}

BEGIN {
	opterr = ""

	takes_arg = -1
}

function __getopt_prepare (\
	short_opts,
	i, len, nc) # local vars
{
	if ("-" in __getopt_opts){
		# already initialized
		return
	}

	len = length(short_opts)
	for (i=1; i <= len; ++i){
		if (i < len && substr(short_opts, i+1, 1) == ":"){
			__getopt_opts [substr(short_opts, i, 1)] = takes_arg
			++i
		}else{
			__getopt_opts [substr(short_opts, i, 1)] = ""
		}
	}

	for (i in long_opts){
		__getopt_opts [i] = long_opts [i]
	}
}

function __getopt_process_short_opts (\
	i,
	opts) # local vars
{
	opts = substr(ARGV [i], 2)
	optopt = substr(opts, 1, 1)

	if (! (optopt in __getopt_opts)){
		__getopt_errexit("Unknown option `-" optopt "'", 2)
	}

	if (__getopt_opts [optopt] == takes_arg){
		if (length(opts) == 1){
			optarg = ARGV [i+1]
			ARGV [i] = ARGV [i+1] = __getopt_fill
		}else{
			optarg = substr(opts, 2)
			ARGV [i] = __getopt_fill
		}
		return 1
	}

	if (length(opts) > 1)
		ARGV [i] = "-" substr(opts, 2)
	else
		ARGV [i] = __getopt_fill

	return 1
}

function __getopt_process_long_opts (\
	i,
	opt,val,prefix,eq)
{
	opt = substr(ARGV [i], 3)
	eq = (opt ~ /[=]/)
	sub(/[=].*$/, "", opt)
	prefix = "--"

	if ((opt in __getopt_opts) &&
		__getopt_opts [opt] != "" &&
		__getopt_opts [opt] != takes_arg)
	{
		prefix = "-"
		opt = __getopt_opts [opt]
	}

	if (opt in __getopt_opts){
		optopt = opt

		val = __getopt_opts [opt]
		if (val == ""){
			assert(!eq, "Unexpected argument for option `" opt "'")
			ARGV [i] = __getopt_fill
			return 1
		}else if (val == takes_arg){
			if (eq){
				optarg = ARGV [i]
				sub(/^[^=]*=/, "", optarg)
			}else{
				optarg = ARGV [i+1]
				ARGV [i+1] = __getopt_fill
			}
			ARGV [i] = __getopt_fill
			return 1
		}
	}

	__getopt_errexit("Unknown option `" prefix opt "'", 2)
}

function getopt (\
	short_opts, no_stdin,
	i) # local vars
{
	__getopt_prepare(short_opts)

	for (i = 1; i < ARGC; ++i){
		optarg = ""

		if (ARGV [i] == __getopt_fill){
			continue
		}

		if (ARGV [i] == "--"){
			ARGV [i] = __getopt_fill
			__getopt_addstdin()
			return 0
		}

		if (ARGV [i] !~ /^-/ || ARGV [i] == "-"){
			return 0
		}

		if (ARGV [i] ~ /^--/){
			return __getopt_process_long_opts(i)
		}

		assert(ARGV [i] ~ /^-/)

		return __getopt_process_short_opts(i)
	}

	if (!no_stdin)
		__getopt_addstdin()

	return 0
}