Here’s the translation of the Go code to Crystal, with explanations in Markdown format suitable for Hugo:
Our first program demonstrates how to create command-line subcommands with their own set of flags. This is similar to how tools like git have subcommands like git commit and git push, each with their own options.
To run the program, save it as command_line_subcommands.cr and use the Crystal compiler:
First invoke the foo subcommand:
Now try bar:
But bar won’t accept foo’s flags:
This example demonstrates how to create a command-line application with subcommands in Crystal. It uses the OptionParser class to define and parse command-line options for each subcommand. The structure is similar to the original, but it uses Crystal’s idiomatic ways of handling command-line arguments and subcommands.
Next, we’ll look at environment variables, another common way to parameterize programs.