[m-users.] getopt; checking for invalid/illegal option values

Sean Charles (emacstheviking) objitsu at gmail.com
Mon May 3 05:47:29 AEST 2021


I have a working getopt configuration and, to cut a long story short, is it possible to restrict the values that an option can take or not ?

I have an option that specifies the error output mode thus,


    short_option('e', error_mode).
    long_option("errormode", error_mode).
    option_defaults(error_mode, maybe_string(no)).

Currently if I pass a value such as `—errormode blah` then it comes through as that string. The permissible values for this are one of ‘plain’, ’simple’, ‘json’ or ‘xml’… I would like to know how I can reject anything else, and maybe be case indifferent at the same time?

Is it a ’special handler’ that I need ?  The issure has arisen because I currently implemtned a predicate to extract and return an error formor based on the string value, the code is is this:


:- pred get_error_format(felt_options::in, error_format::out) is semidet.

get_error_format(Options, F) :-
    (
        yes(Mode) = getopt.lookup_maybe_string_option(Options, options.error_mode)
    ->
        string.to_lower(Mode, Modei),
        (
            "plain" = Modei, F = plain
        ;
            "simple" = Modei, F = simple
        ;
            "json" = Modei, F = json
        ;
            "xml" = Modei, F = xml
        )
    ;
        F = options.plain
    ).


but I stared fighting the det/semidet/multi argument with the compiler and it made me realise that, really, I should be limiting the strings I allowed through at that point. Is that a correct conclusion to have drawn?


Essentially I don’t want to the application to start of the command line contains junk.

Thanks,
Sean.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210502/654fa311/attachment.html>


More information about the users mailing list