Debian Codegraph @

a_getopt/alt_getopt

raw
1 #!/usr/bin/env runawk
2
3 # Copyright (c) 2010 Aleksey Cheusov <[email protected]>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 #use "alt_assert.awk"
25 #use "alt_getopt.awk"
26 #use "shquote.awk"
27 #use "embed_str.awk"
28
29 #env "LC_ALL=C"
30
31 #.begin-str help
32 # alt_getopt is a command options parser, it gets options
33 # specification and application's options as arguments and outputs
34 # shell commands to be executed.
35 # Usage:
36 # alt_getopt -h
37 # alt_getopt [OPTIONS] opt1 sh_cmd1 [opt2 sh_cmd2...] -- [args]
38 # OPTIONS:
39 # -h|--help display this screen
40 # -c do not output 'shift' command, variable
41 # __shifts_cnt is assigned instead
42 #.end-str
43
44 function __takes_arg (opt){
45 if (!opt in __getopt_opts)
46 return 0
47 if (__getopt_opts [opt] == takes_arg)
48 return 1
49 if (__getopt_opts [opt] == "")
50 return 0
51 return __getopt_opts [__getopt_opts [opt]] == takes_arg
52 }
53
54 BEGIN {
55 start = 1
56 if (ARGV [1] == "-h" || ARGV [1] == "--help"){
57 print EMBED_STR ["help"] > "/dev/stderr"
58 exitnow(0)
59 }
60 if (ARGV [1] == "-c"){
61 delete ARGV [1]
62 ++start
63 output_shift_cnt = 1
64 }
65
66 for (i=start; i < ARGC; i += 2){
67 if (ARGV [i] == "--"){
68 delete ARGV [i]
69 ++i
70 break
71 }
72
73 if (ARGV [i] == "" || ARGV [i+1] == ""){
74 print "bad arguments, see alt_getopt -h for more information" \
75 > "/dev/stderr"
76 }
77
78 if (ARGV [i] ~ /^[A-Za-z0-9!@%^=+\/,.]$/){
79 shopts = shopts ARGV [i]
80 action [ARGV [i]] = ARGV [i+1]
81 }else if (ARGV [i] ~ /^=[A-Za-z0-9!@%^=+\/,.]$/){
82 opt = substr(ARGV [i], 2)
83 shopts = shopts opt ":"
84 action [opt] = ARGV [i+1]
85 }else if (ARGV [i] ~ /^[A-Za-z0-9!@%^=+\/,.][ |][^ |]+$/){
86 split(ARGV [i], arr, /[ |]/)
87 shopts = shopts arr [1]
88 long_opts [arr [2]] = arr [1]
89 action [arr [1]] = ARGV [i+1]
90 }else if (ARGV [i] ~ /^=[A-Za-z0-9!@%^=+\/,.][ |][^ |]+$/){
91 split(substr(ARGV [i], 2), arr, /[ |]/)
92 shopts = shopts arr [1] ":"
93 long_opts [arr [2]] = arr [1]
94 action [arr [1]] = ARGV [i+1]
95 }else if (ARGV [i] ~ /^[^=][^ |]*$/){
96 long_opts [ARGV [i]] = ""
97 action [ARGV [i]] = ARGV [i+1]
98 }else if (ARGV [i] ~ /^=[^ |]+$/){
99 opt = substr(ARGV [i], 2)
100 long_opts [opt] = takes_arg
101 action [opt] = ARGV [i+1]
102 }else{
103 abort("unknown format of'" ARGV [i] "'")
104 }
105
106 delete ARGV [i]
107 delete ARGV [i+1]
108 }
109
110 first = i
111
112 while (getopt(shopts, 1)){
113 if (__takes_arg(optopt))
114 print action [optopt] shquote(optarg)
115 else
116 print action [optopt]
117 }
118
119 shifts_cnt = 0
120 for (i=first; i < ARGC; ++i){
121 if (ARGV [i] == "")
122 ++shifts_cnt
123 }
124 if (output_shift_cnt)
125 print "shifts=" shifts_cnt
126 else
127 print "shift", shifts_cnt
128
129 exitnow(0)
130 }