Our first program demonstrates how to use command-line flags in Crystal. Command-line flags are a common way to specify options for command-line programs. For example, in wc -l the -l is a command-line flag.
To experiment with the command-line flags program, first compile it and then run the resulting binary directly.
Try out the built program by first giving it values for all flags.
Note that if you omit flags, they automatically take their default values.
Trailing positional arguments can be provided after any flags.
Use -h or --help flags to get automatically generated help text for the command-line program.
If you provide a flag that wasn’t specified to the OptionParser, the program will print an error message and show the help text again.
In Crystal, we use the OptionParser class to handle command-line flags. It provides a flexible way to define and parse command-line options. The syntax is different from Go’s flag package, but the concept is similar. We define the flags using the on method of the OptionParser, and then parse the command-line arguments. The parsed values are stored in variables that we can use in our program.