@@ -97,10 +97,85 @@ The `error` builtin is more informative:
97
97
echo "$[_error.code] $[_error.message] foo=$[_error.foo]"
98
98
# => 99 Custom message foo=zz"
99
99
100
+ ## Tips
101
+
102
+ ### Proc Return Status Should Be Either OK-Fail, or True-False-Fail
103
+
104
+ <style >
105
+ table {
106
+ margin-left : 2em ;
107
+ background-color : #eee ;
108
+ }
109
+ thead {
110
+ background-color : white ;
111
+ }
112
+ </style >
113
+
114
+ That is, use ** one** of these styles:
115
+
116
+ <div style =" display : flex ; gap : 20px ;" >
117
+ <table cellpadding =" 10 " cellspacing =" 5 " >
118
+
119
+ - thead
120
+ - Return Status
121
+ - Meaning
122
+ - tr
123
+ - 0
124
+ - OK
125
+ - tr
126
+ - 1 or more
127
+ - Fail
128
+
129
+ </table >
130
+
131
+ <table cellpadding =" 10 " cellspacing =" 5 " >
132
+
133
+ - thead
134
+ - Return Status
135
+ - Meaning
136
+ - tr
137
+ - 0
138
+ - True
139
+ - tr
140
+ - 1
141
+ - False
142
+ - tr
143
+ - 2 or more
144
+ - Fail
145
+
146
+ </table >
147
+ </div >
148
+
149
+ For example, here's a proc that does is ** not** follow the style:
150
+
151
+ proc is-different (left, right) {
152
+ mkdir /tmp/dest # may return 1 on failure
153
+
154
+ cp $left $right /tmp/dest # may return 1 on failure
155
+
156
+ diff -u $left $right # 0-true, 1-false, 2-failure
157
+ }
158
+
159
+ The exit code isn't well-defined, because ` mkdir ` and ` cp ` use the OK-fail
160
+ paradigm, while ` diff ` uses the ** boolean** paradigm:
161
+
162
+ Explicitly checking for failure fixes it:
163
+
164
+ proc different (left, right) {
165
+ if ! mkdir /tmp/dest {
166
+ return 2 # 2-failure
167
+ }
168
+ if ! cp $left $right /tmp/dest {
169
+ return 2 # 2-failure
170
+ }
171
+
172
+ diff -u $left $right # 0-true, 1-false, 2-failure
173
+ }
174
+
100
175
## Related
101
176
102
177
- [ YSH vs. Shell Idioms > Error Handling] ( idioms.html#error-handling )
103
- - [ YSH Fixes Shell's Error Handling (` errexit ` )] ( error-handling.html ) - Long
104
- design doc.
178
+ - [ YSH Fixes Shell's Error Handling (` errexit ` )] ( error-handling.html ) - A
179
+ detailed design doc
105
180
106
181
0 commit comments