Command Line Flags in PureScript

Here’s the translation of the Go code to PureScript, along with explanations in Markdown format suitable for Hugo:

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.

PureScript doesn't have a built-in package for command-line flag parsing, but we can use the `optparse-applicative` library to achieve similar functionality. Let's implement our example command-line program using this library.

```purescript
module Main where

import Prelude

import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Options.Applicative

-- Define the structure to hold our command-line options
type Options = 
  { word :: String
  , numb :: Int
  , fork :: Boolean
  , svar :: String
  }

-- Define our command-line parser
parser :: Parser Options
parser = ado
  word <- strOption
    ( long "word"
    <> short 'w'
    <> metavar "STRING"
    <> help "a string"
    <> value "foo"
    )
  numb <- option auto
    ( long "numb"
    <> short 'n'
    <> metavar "INT"
    <> help "an int"
    <> value 42
    )
  fork <- switch
    ( long "fork"
    <> short 'f'
    <> help "a bool"
    )
  svar <- strOption
    ( long "svar"
    <> short 's'
    <> metavar "STRING"
    <> help "a string var"
    <> value "bar"
    )
  in { word, numb, fork, svar }

main :: Effect Unit
main = do
  opts <- execParser $ info (parser <**> helper)
    ( fullDesc
    <> progDesc "Program description"
    <> header "header - a test for optparse-applicative" )
  
  log $ "word: " <> opts.word
  log $ "numb: " <> show opts.numb
  log $ "fork: " <> show opts.fork
  log $ "svar: " <> opts.svar

To experiment with the command-line flags program, you’ll need to compile it first and then run the resulting executable.

$ spago build
$ spago run

Try out the built program by giving it values for all flags:

$ spago run -- --word=opt --numb=7 --fork --svar=flag
word: opt
numb: 7
fork: true
svar: flag

Note that if you omit flags, they automatically take their default values:

$ spago run -- --word=opt
word: opt
numb: 42
fork: false
svar: bar

The optparse-applicative library automatically generates help text for the command-line program. You can access it using the -h or --help flags:

$ spago run -- --help
header - a test for optparse-applicative

Usage: <program> [-w|--word STRING] [-n|--numb INT] [-f|--fork] 
                 [-s|--svar STRING]
  Program description

Available options:
  -w,--word STRING         a string
  -n,--numb INT            an int
  -f,--fork                a bool
  -s,--svar STRING         a string var
  -h,--help                Show this help text

If you provide a flag that wasn’t specified in the parser, the program will print an error message and show the help text.

In PureScript, we’ve used the optparse-applicative library to replicate the functionality of Go’s flag package. This library provides a more declarative way to define command-line options and automatically generates help text.