Search code examples
crystal-langoptionparser

Crystal lang takes option parameters from OptionParser when it shouldn't


I started paying around with Crystal lang, I want to use OptionParser to show a help text, however -h will be interpred by Crystal instead of OptionParser

I am using the example from https://crystal-lang.org/api/0.18.7/OptionParser.html

and calling the app myAppl with:

crystal src/myAppl.cr --help

This shows Crystal help. Now, if I compile the app then it shows the help text I wrote OptionParser

What I am doing wrong?


Solution

  • Have a look at the first line of that help output:

    Usage: crystal run [options] [programfile] [--] [arguments]
    

    That -- is what allows you to force an argument to be passed to the compiled program rather than being used by the compiler. So following your example:

    crystal src/myApp1.cr -- --help
    

    Of course if you compile your program you can just pass it directly to the resulting binary:

    crystal build src/myApp1.cr
    ./myApp1 --help