@@ -213,10 +213,34 @@ pub fn flag(
213
213
short_name : String ,
214
214
next : fn ( Bool ) -> Decoder ( final) ,
215
215
) -> Decoder ( final) {
216
- use value <- optional_field ( long_name , decode . bool )
216
+ optional_opt ( long_name , short_name , False , decode . bool , next )
217
+ }
218
+
219
+ /// Decode a command line option by either a long name or short name.
220
+ /// The given default value is returned if the option does not exist.
221
+ /// ```gleam
222
+ /// let decoder = {
223
+ /// use name <- clad.optional_opt("name", "n", "Lucy", decode.string)
224
+ /// decode.success(name)
225
+ /// }
226
+ ///
227
+ /// let result = clad.decode(["--name", "Joe"], decoder)
228
+ /// assert result == Ok("Joe")
229
+ ///
230
+ /// let result = clad.decode([], decoder)
231
+ /// assert result == Ok("Lucy")
232
+ /// ```
233
+ pub fn optional_opt (
234
+ long_name : String ,
235
+ short_name : String ,
236
+ default : t,
237
+ field_decoder : Decoder ( t) ,
238
+ next : fn ( t) -> Decoder ( final) ,
239
+ ) -> Decoder ( final) {
240
+ use value <- optional_field ( long_name , field_decoder )
217
241
case value {
218
242
Some ( v ) -> next ( v )
219
- None -> decode . optional_field ( short_name , False , decode . bool , next )
243
+ None -> decode . optional_field ( short_name , default , field_decoder , next )
220
244
}
221
245
}
222
246
0 commit comments