1

I was watching a video made by Richard Cook on SafariBookOnline. He builds a command line app with Haskell. In this video, he explains some basic concepts while writing a program to parse command lines arguments.

I am quite new to Haskell, and I can't figure out why this code does not work:

dataPathParser :: Parser FilePath
dataPathParser = strOption $
  value defaultDataPath
  <> long "data-path"
  <> short 'p'
  <> metavar "DATAPATH"
  <> help ("path to data file (default " ++ defaultDataPath ++ ")")

This code does not work at well:

itemDescriptionValueParser :: Parser String
itemDescriptionValueParser =
  strOption (long "desc" <> short 'd' <> metavar "DESCRIPTION" <> help "description")

And actually, everywhere I wrote "<>", I got an error where the compiler tells me that:

• Variable not in scope:
    (<>) :: Mod f5 a5 -> Mod f4 a4 -> Mod ArgumentFields ItemIndex
• Perhaps you meant one of these:
    ‘<$>’ (imported from Options.Applicative),
    ‘<*>’ (imported from Options.Applicative),
    ‘<|>’ (imported from Options.Applicative)

The problem I got is most probably due to the difference of versions of GHC and Optparse-applicative. I use the latest ones. LTS Haskell 9.12: 0.13.2.0.

But since I am quite new, I can't figure out how to rewrite the code of Richard Cook.

I would appreciate any help.

Thanks in advance, Alex

duplode
  • 33,731
  • 7
  • 79
  • 150
Alex k
  • 13
  • 3

1 Answers1

4

http://hackage.haskell.org/package/optparse-applicative-0.14.0.0/docs/Options-Applicative.html#t:Parser:

A modifier can be created by composing the basic modifiers provided by here using the Monoid operations mempty and mappend, or their aliases idm and <>.

It looks like it doesn't export <>, though, so you need to get it from Data.Monoid:

import Data.Monoid

... or just:

import Data.Monoid ((<>))
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks a lot! It may seem trivial for you, but I am beginning with Haskell, and I thought about a problem in the code, not in the imports at all. Thanks again! – Alex k Nov 12 '17 at 15:31
  • @Alexk Yeah, this is not obvious. It looks like the examples were written for an earlier version of optparse-applicative, which did export `(<>)`, but that was changed for a reason I don't understand: https://github.com/pcapriotti/optparse-applicative/issues/224 – melpomene Nov 12 '17 at 15:58