I've been trying to build a command line parser with Turtle, nothing fancy: https://github.com/Tyrn/go-procr
#!/usr/bin/env stack
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Turtle
import Prelude hiding (FilePath)
parserSwitch :: Parser (Bool, Bool)
parserSwitch = (,) <$> switch "verbose" 'v' "Unless verbose, just progress bar is shown"
<*> switch "filetitle" 'f' "Use file name for title tag"
parserArg :: Parser (FilePath, FilePath)
parserArg = (,) <$> argPath "src" "Source directory"
<*> argPath "dst" "Destination directory"
main :: IO ()
main = do
(verbose, filetitle) <- options "Flags" parserSwitch
echo (format ("verbose: "%w) verbose)
echo (format ("filetitle: "%w) filetitle)
(src, dst) <- options "Args" parserArg
echo (format ("src: "%fp) src)
echo (format ("dst: "%fp) dst)
There are three kinds of arguments required: boolean flags; options, text and integer; positional arguments. So far I got stuck at boolean flags and positional arguments. Unfortunately, the examples seem to be too basic even for this.
Do I really have to build separate parsers for different kinds of options (I did not manage to satisfy syntax with a single parser)?
It won't work as expected, anyway.
I can't figure out what my next step should be.
Your first step is to have something where you can store and retrieve your options easily:
Afterwards, you write parsers for your options. To make things clear, let's be a little bit verbose first:
Since
Parser
is an instance ofApplicative
we can then combine all options in a single parser:We've combined all four parsers into a single parser, similar to the combination via
(,)
. Now we can parse the options with a single call tooptions
. After all, either all arguments are correct, or we have to present the user the proper usage:You probably want to use shorter names, though, and maybe write the parsers in
settingsParser
: