File: init_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 (86 lines) | stat: -rw-r--r-- 2,018 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
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
#        http://sourceforge.net/projects/runawk
#
############################################################

# =head2 init_getopt.awk
#
# Initialization step for power_getopt.awk module.  In some cases it
# makes sense to process options in a while() loop.  This module
# allows doing this.  See the documentation about how options are
# initialized in power_getopt.awk module.
#
# =over 2
#
# =item I<print_help ()>
#
# display help message.
#
# =back
#

#use "alt_getopt.awk"
#use "embed_str.awk"

function print_help (            i){
	for (i = 1; i <= _help_msg_cnt; ++i){
		if (_help_msg_arr [i] ~ /^[ \t]*=/){
			sub(/=/, "-", _help_msg_arr [i])
		}
		print _help_msg_arr [i] > "/dev/stderr"
	}
}

BEGIN {
	if ("help" in EMBED_STR){
		_help_msg = EMBED_STR ["help"]
		_help_msg_cnt = split(_help_msg, _help_msg_arr, /\n/)
		for (i = 1; i <= _help_msg_cnt; ++i){
			if (match(_help_msg_arr [i], /^[ \t]*[-=][^ \t]+/)){
				_opt = substr(_help_msg_arr [i], RSTART, RLENGTH)
				sub(/^[ \t]+/, "", _opt)

				if (_opt ~ /^-.[|]--.+$/){
					# -h|--help
					_sopt = substr(_opt, 1, 2)
					_lopt = substr(_opt, 4)
				}else if (_opt ~ /^=.[|]--.+$/){
					# =h|--help
					_sopt = substr(_opt, 1, 2)
					_lopt = "=" substr(_opt, 5)
				}else if (_opt ~ /^[-=].$/){
					# -h or =h
				_sopt = _opt
					_lopt = ""
				}else if (_opt ~ /^[-=]-.+$/){
					# --help or =-help
					_sopt = ""
					_lopt = _opt
				}

				if (_sopt ~ /^-.$/){
					# -h
					short_opts = short_opts substr(_sopt, 2, 1)
				}else if (_sopt ~ /^=.$/){
					# =F
					short_opts = short_opts substr(_sopt, 2, 1) ":"
				}

				sub(/^[-=]/, "", _sopt)

				if (_lopt ~ /^--.+$/){
					# --help
					long_opts [substr(_lopt, 3)] = _sopt
				}else if (_lopt ~ /^=-.+$/){
					# =-FLAG
					if (_sopt != "")
						long_opts [substr(_lopt, 3)] = _sopt
					else
						long_opts [substr(_lopt, 3)] = takes_arg
				}
			}
		}
	}
}