ConsoleAsk: Simple CLI user input library

[ console, library, mit, system ] [ Propose Tags ] [ Report a vulnerability ]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5), lens (>=5 && <6), parsec (>=3.1 && <3.2), regex-tdfa (>=1.3 && <1.4), text (>=2 && <3) [details]
License MIT
Copyright 2025 Toma Sasaki
Author Toma Sasaki
Maintainer netst915@gmail.com
Category System, Console
Home page https://github.com/t-sasaki915/ConsoleAsk#readme
Bug tracker https://github.com/t-sasaki915/ConsoleAsk/issues
Source repo head: git clone https://github.com/t-sasaki915/ConsoleAsk
Uploaded by t_sasaki915 at 2025-07-01T14:53:15Z
Distributions
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-07-01 [all 1 reports]

Readme for ConsoleAsk-0.1.0.1

[back to package description]

ConsoleAsk

Simple CLI user input library

Example

import Data.Functor ((<&>))
import Data.Text (Text)
import Text.Parsec (char, digit, many1)
import Text.Regex.TDFA ((=~))

import System.Console.Ask (Ask, ask, askOptional, askOrElse, runAsk, defaultBehaviour)
import System.Console.Ask.Askable (Askable (fromText), fromParsec)

data UserInformation = UserInformation
    { name                   :: Text
    , age                    :: Maybe Int
    , birthday               :: Date
    , notificationPreference :: NotificationPreference
    } deriving Show

data NotificationPreference = NotificationPreference
    { needNotifications :: Bool
    , emailAddress      :: Maybe EmailAddress
    } deriving Show

askUserInformation :: Ask UserInformation
askUserInformation =
    UserInformation
        <$> ask         "What is your name?"
        <*> askOptional "How old are you?"
        <*> ask         "When is your birthday?"
        <*> askNotificationPreference

askNotificationPreference :: Ask NotificationPreference
askNotificationPreference = do
    needNotifications' <- askOrElse "Do you need our update notifications?" False

    emailAddress' <-
        if needNotifications'
            then Just <$> ask "What is your email address?"
            else pure Nothing

    pure NotificationPreference
        { needNotifications = needNotifications'
        , emailAddress      = emailAddress'
        }

newtype EmailAddress = EmailAddress Text deriving Show

instance Askable EmailAddress where
    fromText text =
        if text =~ ("[a-zA-Z0-9+._-]+@[a-zA-Z-]+\\.[a-z]+" :: Text)
            then Just (EmailAddress text)
            else Nothing

data Date = Date Int Int deriving Show

instance Askable Date where
    fromText = fromParsec $ do
        day   <- many1 digit <&> read
        _     <- char '/'
        month <- many1 digit <&> read

        pure (Date day month)

main :: IO ()
main = do
    userInfo <- runAsk defaultBehaviour askUserInformation

    print userInfo
What is your name?
> Toma Sasaki

How old are you?
>

When is your birthday?
> 15/9

Do you need our update notifications? (Default: False)
> aye

What is your email address?
> me@t-sasaki.net

UserInformation
    { name = "Toma Sasaki"
    , age = Nothing
    , birthday = Date 15 9
    , notificationPreference =
        NotificationPreference
            { needNotifications = True
            , emailAddress = Just (EmailAddress "me@t-sasaki.net")
            }
    }

Features

  • Automatically parses input values to Askable instances. (See also: Askable.hs)
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO

import System.Console.Ask (Ask, ask, askOptional, askOrElse, defaultBehaviour, runAsk)

main :: IO ()
main = runAsk defaultBehaviour $ do
    name              <- ask         "What is your name?"               :: Ask Text
    age               <- askOptional "How old are you?"                 :: Ask (Maybe Int)
    needNotifications <- askOrElse   "Do you need notifications?" False :: Ask Bool

    liftIO $ do
        TextIO.putStrLn ("Name: " <> name)
        TextIO.putStrLn ("Age: " <> Text.show age)
        TextIO.putStrLn ("Need notifications: " <> Text.show needNotifications)
What is your name?
> Toma Sasaki

How old are you?
> a
Invalid input.

How old are you?
> 18

Do you need notifications? (Default: False)
> no

Name: "Toma Sasaki"
Age: 18
Need notifications: False
  • Askable supports both Text -> Maybe a and parsec.
import Data.Functor ((<&>))
import Data.Text (Text)
import Text.Parsec (char, digit, many1)
import Text.Regex.TDFA ((=~))

import System.Console.Ask.Askable (Askable (fromText), fromParsec)

newtype EmailAddress = EmailAddress Text deriving Show

instance Askable EmailAddress where
    fromText text =
        if text =~ ("[a-zA-Z0-9+._-]+@[a-zA-Z-]+\\.[a-z]+" :: Text)
            then Just (EmailAddress text)
            else Nothing

data Date = Date Int Int deriving Show

instance Askable Date where
    fromText = fromParsec $ do
        day   <- many1 digit <&> read
        _     <- char '/'
        month <- many1 digit <&> read

        pure (Date day month)
  • Custom prompt
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO

import System.Console.Ask (Ask, ask', askOptional', askOrElse', defaultBehaviour, runAsk)

main :: IO ()
main = runAsk defaultBehaviour $ do
    name              <- ask'         "What is your name?"               "Text> " :: Ask Text
    age               <- askOptional' "How old are you?"                 "Int > " :: Ask (Maybe Int)
    needNotifications <- askOrElse'   "Do you need notifications?" False "Y/N > " :: Ask Bool

    liftIO $ do
        TextIO.putStrLn ("Name: " <> name)
        TextIO.putStrLn ("Age: " <> Text.show age)
        TextIO.putStrLn ("Need notifications: " <> Text.show needNotifications)
What is your name?
Text> Toma Sasaki

How old are you?
Int > 18

Do you need notifications? (Default: False)
Y/N > True

Name: "Toma Sasaki"
Age: 18
Need notifications: True
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO

import System.Console.Ask (Ask, ask, askOptional, askOrElse, defaultBehaviour, runAsk, withBehaviour)
import System.Console.Ask.Behaviour (DefaultValueStyle (..), defaultValueStyle, invalidInputErrorMsg, set)

main :: IO ()
main = runAsk defaultBehaviour $ do
    let customBehaviour1 = set invalidInputErrorMsg (Just "??????") defaultBehaviour
        customBehaviour2 = set defaultValueStyle OnNewline defaultBehaviour

    name              <- ask "What is your name?" :: Ask Text
    age               <- withBehaviour customBehaviour1 (askOptional "How old are you?")                 :: Ask (Maybe Int)
    needNotifications <- withBehaviour customBehaviour2 (askOrElse   "Do you need notifications?" False) :: Ask Bool

    liftIO $ do
        TextIO.putStrLn ("Name: " <> name)
        TextIO.putStrLn ("Age: " <> Text.show age)
        TextIO.putStrLn ("Need notifications: " <> Text.show needNotifications)
What is your name?
> Toma Sasaki

How old are you?
> a
??????

How old are you?
> 18

Do you need notifications?
Default: False
> True

Name: "Toma Sasaki"
Age: 18
Need notifications: True