Here’s the translation of the Go code to PureScript, along with explanations in Markdown format suitable for Hugo:
Our first example demonstrates how to create command-line subcommands in PureScript. Some command-line tools have multiple subcommands, each with its own set of flags. For example, git clone and git push are two different subcommands of the git tool. In PureScript, we can use the optparse-applicative library to easily define simple subcommands with their own flags.
To run this program, you would typically compile it and then execute the resulting binary. Here are some example invocations:
First, invoke the foo subcommand:
Now try bar:
But bar won’t accept foo’s flags:
In this PureScript version, we use the optparse-applicative library to define our subcommands and their respective flags. The Command data type represents our subcommands, and we define separate parsers for each subcommand (fooCmd and barCmd).
The commandParser combines these subcommand parsers, and main uses execParser to run the parser and handle the result.
This approach provides a type-safe way to handle command-line arguments and subcommands in PureScript, with helpful error messages and automatically generated help text.