Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:28:19+00:00 2026-06-04T16:28:19+00:00

I am trying to teach myself Haskell. As a sample program, I am writing

  • 0

I am trying to teach myself Haskell. As a sample program, I am writing a Spider solitaire player.

I am trying to write a command line parser using System.Console.GetOpt. I know there are easier ways to do argument parsing for this program, but I want to learn how to use the GetOpt module because I anticipate needing its sophistication later in other programs that I will be writing.

I am trying to add a “–help” option that just prints a usage message and then exits. I would also like to print usage messages if either of the arguments to the “–games” option or the “–suits” option are not valid integers (games >= 1 and <= 1000, suits == 1, 2, or 4). I will be passing the resulting Options data type to other parts of my program.

I am also getting an error that progName is not in scope. Isn’t the case statement in parseArgs in the scope of the do block?

Here is my code, patched together from the examples in “Real World Haskell” and the Haskell wiki:

module Main (main) where

import System.Console.GetOpt
import System.Environment(getArgs, getProgName)

data Options = Options {
    optGames :: Int
  , optSuits :: Int
  , optVerbose :: Bool
  } deriving Show

defaultOptions = Options {
    optGames  = 1
  , optSuits = 4
  , optVerbose = False
  }

options :: [OptDescr (Options -> Options)]
options =
  [ Option ['g'] ["games"]
      (ReqArg (\g opts -> opts { optGames = (read g) }) "GAMES")
      "number of games"
  , Option ['s'] ["suits"]
      (ReqArg (\s opts -> opts { optSuits = (read s) }) "SUITS")
      "number of suits"
  , Option ['v'] ["verbose"]
      (NoArg (\opts -> opts { optVerbose = True }))
      "verbose output"
  ]

parseArgs :: IO Options
parseArgs = do
  argv <- getArgs
  progName <- getProgName
  case getOpt RequireOrder options argv of
    (opts, [], []) -> return (foldl (flip id) defaultOptions opts)
    (_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
  where
    header = "Usage: " ++ progName ++ " [OPTION...]"
    helpMessage = usageInfo header options

main :: IO ()
main = do
  options <- parseArgs
  putStrLn $ show options
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:28:21+00:00Added an answer on June 4, 2026 at 4:28 pm

    Here is the solution that I came up with:

    module Main (main) where
    
    import Control.Monad
    import Control.Monad.Error
    import System.Console.GetOpt
    import System.Environment(getArgs, getProgName)
    
    data Options = Options {
        optGames :: Int
      , optSuits :: Int
      , optVerbose :: Bool
      } deriving Show
    
    defaultOptions = Options {
        optGames  = 1
      , optSuits = 4
      , optVerbose = False
      }
    
    options :: [OptDescr (Options -> Either String Options)]
    options =
      [ Option ['g'] ["games"]
          (ReqArg (\g opts ->
            case reads g of
              [(games, "")] | games >= 1 && games <= 1000 -> Right opts { optGames = games }
              _ -> Left "--games must be a number between 1 and 1000"
            ) "GAMES")
          "number of games"
      , Option ['s'] ["suits"]
          (ReqArg (\s opts ->
            case reads s of
              [(suits, "")] | suits `elem` [1, 2, 4] -> Right opts { optSuits = suits }
              _ -> Left "--suits must be 1, 2, or 4"
            ) "SUITS")
          "number of suits"
      , Option ['v'] ["verbose"]
          (NoArg (\opts -> Right opts { optVerbose = True }))
          "verbose output"
      ]
    
    parseArgs :: IO Options
    parseArgs = do
      argv <- getArgs
      progName <- getProgName
      let header = "Usage: " ++ progName ++ " [OPTION...]"
      let helpMessage = usageInfo header options
      case getOpt RequireOrder options argv of
        (opts, [], []) ->
          case foldM (flip id) defaultOptions opts of
            Right opts -> return opts
            Left errorMessage -> ioError (userError (errorMessage ++ "\n" ++ helpMessage))
        (_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
    
    main :: IO ()
    main = do
      options <- parseArgs
      putStrLn $ show options
    

    How can I improve this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to teach myself python using Google's AppEngine , and I can't get
I'm trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I've
I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to
I'm trying to teach myself AJAX/Web services using C# and javascript. And I think
I'm trying to teach myself clojure and I'm using the principles of Prime Factors
I have recently started playing with databases, trying to teach myself using examples. I
So I'm trying to teach myself Haskell. I am currently on the 11th chapter
I'm a C# guy trying to teach myself Haskell from Erik Meijer's Channel 9
I am trying to teach myself C Programming and I am using DevC++ for
I am trying to teach myself WCF (using Learning WCF by Michele Leroux Bustamante),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.