title: Haskell: How to report validation errors from command line arguments to user with proper exit codes
body: <p>I'm using <code>getOpt</code> to parse and validate the args provided by <code>getArgs</code>.</p>
  <p>I chose to use the action based approach for <code>getOpt</code>, here are the types.</p>
  <pre class="lang-hs prettyprint-override"><code>data Options = Options { afield :: foo, bfield :: bar}
  type Action = Either String (Options -&gt; Options)
  </code></pre>
  <p>Then I fold over the actions list on a default options record using,</p>
  <pre class="lang-hs prettyprint-override"><code>evaluateActions :: Either [String] Options -&gt; Action -&gt; Either [String] Options
  evaluateActions (Left errors) (Left err)      = Left $ err:errors
  evaluateActions (Left errors) (Right _)       = Left errors
  evaluateActions (Right _) (Left err)          = Left [err]
  evaluateActions (Right option) (Right action) = Right $ action option
  </code></pre>
  <p>Afterwards I am left with the desired type <code>Either [String] Options</code>.
  Upon error(s), I want to print the strings out and exit with the error code InvalidArgument.</p>
  <pre class="lang-hs prettyprint-override"><code>printErrorsThenExit :: [String] -&gt; ExitCode -&gt; IO ()
  printErrorsThenExit errs code =
    hPutStr stderr (concat errs) &gt;&gt;
    exitWith code
  </code></pre>
  <p>I am struggling to glue this together in my main function without writing something like the following which doesn't work with the types:</p>
  <pre class="lang-hs prettyprint-override"><code>opts &lt;- case result of
            Left errors   -&gt; printErrorsThenExit errors illegalArgument  
            Right options -&gt; return options
  </code></pre>
  <p>I have seen the <code>when</code> function but I don't think it helps since I need to unwrap the Either.</p>
  <p>Anyone know how I can express failure process in Haskell?</p>