# helps parsing CLI arguments for Nushell scripts # # the following Nushell script does not make sense to be used as an external # command because there is no such thing as a `list` in Bash for # instance. # ```nushell # def main [x: list] { # print $x # } # ``` # # one needs to write something less strict at parse-time and thus looses type # information... # ```nushell # def main [ # x: string, # list # ] { # print $x # } # ``` # # it's possible to write a much stronger script with `parse-arg` # ```nushell # def main [ # x: string, # list # ] { # let x = $x | parse-arg (metadata $x).span "list" # the script would crash if either # # `$x: string` is not valid NUON or if # # the resulting value is not a `list` # print $x # here, `$x` is a `list` as intended # } # ``` export def parse-arg [ span: record, # the span of the input variable expected_type: string, # the expected type for the input variable ]: [ string -> any ] { let val = try { $in | from nuon } catch { error make { msg: $"(ansi red_bold)invalid NUON(ansi reset)", label: { text: "invalid NUON", span: $span, }, } } if ($val | describe) != $expected_type { error make { msg: $"(ansi red_bold)bad type(ansi reset)", label: { text: $"type: ($val | describe)", span: $span, }, help: $"expected ($expected_type)", } } $val }