{-# LANGUAGE NoImplicitPrelude #-}

-- | Names for packages.


module Stack.Types.PackageName
  ( packageNameArgument
  ) where

import qualified Options.Applicative as O
import           Stack.Prelude

-- | An argument which accepts a template name of the format

-- @foo.hsfiles@.

packageNameArgument :: O.Mod O.ArgumentFields PackageName
                    -> O.Parser PackageName
packageNameArgument :: Mod ArgumentFields PackageName -> Parser PackageName
packageNameArgument =
  ReadM PackageName
-> Mod ArgumentFields PackageName -> Parser PackageName
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
O.argument
    (do String
s <- ReadM String
forall s. IsString s => ReadM s
O.str
        (String -> ReadM PackageName)
-> (PackageName -> ReadM PackageName)
-> Either String PackageName
-> ReadM PackageName
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> ReadM PackageName
forall a. String -> ReadM a
O.readerError PackageName -> ReadM PackageName
forall a. a -> ReadM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Either String PackageName
p String
s))
 where
  p :: String -> Either String PackageName
p String
s =
    case String -> Maybe PackageName
parsePackageName String
s of
      Just PackageName
x -> PackageName -> Either String PackageName
forall a b. b -> Either a b
Right PackageName
x
      Maybe PackageName
Nothing -> String -> Either String PackageName
forall a b. a -> Either a b
Left (String -> Either String PackageName)
-> String -> Either String PackageName
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
        [ String
"Expected valid package name, but got: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
        , String
"Package names consist of one or more alphanumeric words separated \
          \by hyphens."
        , String
"To avoid ambiguity with version numbers, each of these words must \
          \contain at least one letter."
        ]