Skip to content

Conversation

@smaximov
Copy link
Contributor

This pull requests introduces the module System.Console.Docopt.QQ which exports two QuasiQuoters: docopt and docoptFile. docopt parses a literal string — usage string — into some abstract data type (a wrapper around OptFormat); docoptFile takes a file and parses its contents. Parsing is done at the compile time, which means an invalid usage string will result in a compile time error.

Example from Readme:

{-# LANGUAGE QuasiQuotes #-}
module Main where

import Control.Monad (when)
import Data.Char (toUpper)
import System.Console.Docopt.QQ

patterns :: Docopt
patterns = [docopt|
Usage:
  myprog cat <file>
  myprog echo [--caps] <string>

Options:
  -c, --caps    Caps-lock the echoed argument
|]

main :: IO ()
main = do
  args <- parseArgs' patterns

  when (args `isPresent` (command "cat")) $ do
    file <- args `getArg` (argument "file")
    putStr =<< readFile file

  when (args `isPresent` (command "echo")) $ do
    let charTransform = if args `isPresent` (longOption "caps")
                          then toUpper
                          else id
    string <- args `getArg` (argument "string")
    putStrLn $ map charTransform string

See docs for System.Console.Docopt.QQ for more info.

@ryanartecona
Copy link
Member

Apologies @smaximov for letting this go completely unacknowledged since you submitted it back in July 😨 ! I was just settling into a new job, and managed to forget to come back to this repo since then.

Upon revisiting now, I'm as excited about this PR as I remember being excited about it when I saw it submitted. Thanks especially for putting in the effort!

Upon revisiting this old code of mine from when I was just starting out in Haskell, there's...stuff I would like to change. I'm going to try over the next few days to build and play with this locally, and make the API a bit smoother and more consistent. Once that's sorted, I'll make a release with these changes in it.

@ryanartecona ryanartecona mentioned this pull request Feb 20, 2015
4 tasks
ryanartecona added a commit that referenced this pull request Feb 22, 2015
Enable parsing of usage strings at compile time
@ryanartecona ryanartecona merged commit 3a9e147 into docopt:master Feb 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants