0

This question is an extension of the topic : Ordered arguments with cmdargs

I managed to have a list of argument of the same type. But nom, I would like to have a list of options with values. like this :

runhaskell ~/testargs.hs -a 5,6 -b 8,9 -c 4,2 -a 9,3

I tried to declare my data with

data Act = 
    ActA (Double,Double)
  | ActB (Double,Double)
  | ActC (Double,Double)
  deriving (Show, Typeable,  Data, Eq)

but I get the following error :

Couldn't match expected type `Act'
    with actual type `(Double, Double) -> Act'
In the expression: ActA

Is it possible to retrieve a list of argument with values ?

Community
  • 1
  • 1
JeanJouX
  • 2,555
  • 1
  • 25
  • 37
  • 1
    That error is due to an expression, but you haven't showed us the context in which the expression was used, so it's impossible to determine what you've done wrong. – user2407038 Oct 18 '15 at 23:40

1 Answers1

1

You probably will have to use the lower-level Explicit api to do this.

Here is an example which shows how to use the Explicit api to:

  • handle --help
  • collect multiple -w ... options
  • validate a flag's parameter
  • collect all unnamed arguments

Usage examples:

prog --help
prog -i=456
prog -w this -w is -w a -w test
prog -i=xyz

Program:

import System.Console.CmdArgs.Explicit
import Data.Char

data MyOptions = Opts { _help    :: Bool
                      , _words   :: [String]
                      , _opt1    :: String
                      , _opt2    :: Int
                      , _unnamed :: [ String ]
                      }
  deriving (Show)

myMode :: Mode MyOptions
myMode = mode "programName" initialOpts  description unnamedArg convertFlags
  where
    initialOpts = Opts False [] "abc" 3 []
    description = "This program does it all."

    unnamedArg = Arg { argValue = updateUnnamed, argType = "<FILE>", argRequire = False }
        where updateUnnamed str opts = Right $ opts { _unnamed = (str : _unnamed opts) }

    updateWord str opts = Right $ opts { _words = (str: _words opts) }
    updateA    str opts = Right $ opts { _opt1 = str }
    updateNum  str opts 
      | not (null str) && all isDigit str = Right $ opts { _opt2 = read str }
      | otherwise                         = Left $ "-i option is not a number: " ++ str

    convertFlags =
       [ flagReq     ["w","word"] updateWord "<WORD>"   "add a word"
       , flagReq     ["a", "bc"]  updateA    "<STRING>" "a string option"
       , flagOpt "0" ["i" ]       updateNum  "<NUMBER>" "a numeric option"
       , flagHelpSimple (\opts -> opts { _help = True })
       ]

main = do
   opts <- processArgs myMode
   print opts
   if _help opts then
       print $ helpText [] HelpFormatDefault myMode
    else
       print opts
ErikR
  • 51,541
  • 9
  • 73
  • 124