dillonkearns / elm-cli-options-parser / Cli.Validate

This module contains helper functions for performing validations (see the "validate..." functions in Cli.Option).

predicate : String -> (a -> Basics.Bool) -> a -> ValidationResult

Turns a predicate function into a validate function.

import Cli.Option as Option
import Cli.Validate as Validate

isEven : Int -> Bool
isEven n =
    modBy 2 n == 0

pairsOption : Option.Option (Maybe String) (Maybe Int)
pairsOption =
    Option.optionalKeywordArg "pair-programmers"
        |> Option.validateMapIfPresent String.toInt
        |> Option.validateIfPresent
            (Validate.predicate "Must be even" isEven)


type ValidationResult
    = Valid
    | Invalid String

regex : String -> String -> ValidationResult

A helper for regex validations.

programConfig : Program.Config String
programConfig =
    Program.config
        |> Program.add
            (OptionsParser.build identity
                |> OptionsParser.with
                    (Option.requiredKeywordArg "name"
                        |> Option.validate
                            (Cli.Validate.regex "^[A-Z][A-Za-z_]*")
                    )
            )

If the validation fails, the user gets output like this:

$ ./greet --name john
Validation errors:

`name` failed a validation. Must be of form /^[A-Z][A-Za-z_]*/
Value was:
"john"

regexWithMessage : String -> String -> String -> ValidationResult

A helper for regex validations with an additional message.

programConfig : Program.Config String
programConfig =
    Program.config
        |> Program.add
            (OptionsParser.build identity
                |> OptionsParser.with
                    (Option.requiredKeywordArg "name"
                        |> Option.validate
                            (Cli.Validate.regexWithMessage "I expected this to be" "^[A-Z][A-Za-z_]*")
                    )
            )

If the validation fails, the user gets output like this:

$ ./greet --name john
Validation errors:

`name` failed a validation. I expected this to be matching "^[A-Z][A-Za-z_]*" but got 'john'
Value was:
"john"