{-# LANGUAGE DeriveLift#-}
module System.Console.Docopt.Types
where
import Data.Char (isUpper)
import Data.List (nub)
import Data.Map (Map)
import qualified Data.Map as M
import Language.Haskell.TH.Syntax (Lift)
type Name = String
data Pattern a = Sequence [Pattern a]
| OneOf [Pattern a]
| Unordered [Pattern a]
| Optional (Pattern a)
| Repeated (Pattern a)
| Atom a
deriving (Int -> Pattern a -> ShowS
[Pattern a] -> ShowS
Pattern a -> String
(Int -> Pattern a -> ShowS)
-> (Pattern a -> String)
-> ([Pattern a] -> ShowS)
-> Show (Pattern a)
forall a. Show a => Int -> Pattern a -> ShowS
forall a. Show a => [Pattern a] -> ShowS
forall a. Show a => Pattern a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Pattern a -> ShowS
showsPrec :: Int -> Pattern a -> ShowS
$cshow :: forall a. Show a => Pattern a -> String
show :: Pattern a -> String
$cshowList :: forall a. Show a => [Pattern a] -> ShowS
showList :: [Pattern a] -> ShowS
Show, Pattern a -> Pattern a -> Bool
(Pattern a -> Pattern a -> Bool)
-> (Pattern a -> Pattern a -> Bool) -> Eq (Pattern a)
forall a. Eq a => Pattern a -> Pattern a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Pattern a -> Pattern a -> Bool
== :: Pattern a -> Pattern a -> Bool
$c/= :: forall a. Eq a => Pattern a -> Pattern a -> Bool
/= :: Pattern a -> Pattern a -> Bool
Eq, (forall (m :: * -> *). Quote m => Pattern a -> m Exp)
-> (forall (m :: * -> *).
Quote m =>
Pattern a -> Code m (Pattern a))
-> Lift (Pattern a)
forall a (m :: * -> *). (Lift a, Quote m) => Pattern a -> m Exp
forall a (m :: * -> *).
(Lift a, Quote m) =>
Pattern a -> Code m (Pattern a)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Pattern a -> m Exp
forall (m :: * -> *). Quote m => Pattern a -> Code m (Pattern a)
$clift :: forall a (m :: * -> *). (Lift a, Quote m) => Pattern a -> m Exp
lift :: forall (m :: * -> *). Quote m => Pattern a -> m Exp
$cliftTyped :: forall a (m :: * -> *).
(Lift a, Quote m) =>
Pattern a -> Code m (Pattern a)
liftTyped :: forall (m :: * -> *). Quote m => Pattern a -> Code m (Pattern a)
Lift)
atoms :: Eq a => Pattern a -> [a]
atoms :: forall a. Eq a => Pattern a -> [a]
atoms (Sequence [Pattern a]
ps) = (Pattern a -> [a]) -> [Pattern a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pattern a -> [a]
forall a. Eq a => Pattern a -> [a]
atoms [Pattern a]
ps
atoms (OneOf [Pattern a]
ps) = (Pattern a -> [a]) -> [Pattern a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pattern a -> [a]
forall a. Eq a => Pattern a -> [a]
atoms ([Pattern a] -> [a]) -> [Pattern a] -> [a]
forall a b. (a -> b) -> a -> b
$ [Pattern a] -> [Pattern a]
forall a. Eq a => [a] -> [a]
nub [Pattern a]
ps
atoms (Unordered [Pattern a]
ps) = (Pattern a -> [a]) -> [Pattern a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pattern a -> [a]
forall a. Eq a => Pattern a -> [a]
atoms ([Pattern a] -> [a]) -> [Pattern a] -> [a]
forall a b. (a -> b) -> a -> b
$ [Pattern a] -> [Pattern a]
forall a. Eq a => [a] -> [a]
nub [Pattern a]
ps
atoms (Optional Pattern a
p) = Pattern a -> [a]
forall a. Eq a => Pattern a -> [a]
atoms Pattern a
p
atoms (Repeated Pattern a
p) = Pattern a -> [a]
forall a. Eq a => Pattern a -> [a]
atoms Pattern a
p
atoms (Atom a
a) = [a
a]
data Option = LongOption Name
| ShortOption Char
| Command Name
| Argument Name
| AnyOption
deriving (Int -> Option -> ShowS
[Option] -> ShowS
Option -> String
(Int -> Option -> ShowS)
-> (Option -> String) -> ([Option] -> ShowS) -> Show Option
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Option -> ShowS
showsPrec :: Int -> Option -> ShowS
$cshow :: Option -> String
show :: Option -> String
$cshowList :: [Option] -> ShowS
showList :: [Option] -> ShowS
Show, Option -> Option -> Bool
(Option -> Option -> Bool)
-> (Option -> Option -> Bool) -> Eq Option
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Option -> Option -> Bool
== :: Option -> Option -> Bool
$c/= :: Option -> Option -> Bool
/= :: Option -> Option -> Bool
Eq, Eq Option
Eq Option =>
(Option -> Option -> Ordering)
-> (Option -> Option -> Bool)
-> (Option -> Option -> Bool)
-> (Option -> Option -> Bool)
-> (Option -> Option -> Bool)
-> (Option -> Option -> Option)
-> (Option -> Option -> Option)
-> Ord Option
Option -> Option -> Bool
Option -> Option -> Ordering
Option -> Option -> Option
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Option -> Option -> Ordering
compare :: Option -> Option -> Ordering
$c< :: Option -> Option -> Bool
< :: Option -> Option -> Bool
$c<= :: Option -> Option -> Bool
<= :: Option -> Option -> Bool
$c> :: Option -> Option -> Bool
> :: Option -> Option -> Bool
$c>= :: Option -> Option -> Bool
>= :: Option -> Option -> Bool
$cmax :: Option -> Option -> Option
max :: Option -> Option -> Option
$cmin :: Option -> Option -> Option
min :: Option -> Option -> Option
Ord, (forall (m :: * -> *). Quote m => Option -> m Exp)
-> (forall (m :: * -> *). Quote m => Option -> Code m Option)
-> Lift Option
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Option -> m Exp
forall (m :: * -> *). Quote m => Option -> Code m Option
$clift :: forall (m :: * -> *). Quote m => Option -> m Exp
lift :: forall (m :: * -> *). Quote m => Option -> m Exp
$cliftTyped :: forall (m :: * -> *). Quote m => Option -> Code m Option
liftTyped :: forall (m :: * -> *). Quote m => Option -> Code m Option
Lift)
type OptPattern = Pattern Option
humanize :: Option -> String
humanize :: Option -> String
humanize Option
opt = case Option
opt of
Command String
name -> String
name
Argument String
name -> if (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isUpper String
name
then String
name
else String
"<" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
name String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">"
LongOption String
name -> String
"--"String -> ShowS
forall a. [a] -> [a] -> [a]
++String
name
ShortOption Char
c -> [Char
'-',Char
c]
Option
AnyOption -> String
"[options]"
data OptionInfo = OptionInfo
{ OptionInfo -> [Option]
synonyms :: [Option]
, OptionInfo -> Maybe String
defaultVal :: Maybe String
, OptionInfo -> Bool
expectsVal :: Bool
, OptionInfo -> Bool
isRepeated :: Bool
} deriving (Int -> OptionInfo -> ShowS
[OptionInfo] -> ShowS
OptionInfo -> String
(Int -> OptionInfo -> ShowS)
-> (OptionInfo -> String)
-> ([OptionInfo] -> ShowS)
-> Show OptionInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OptionInfo -> ShowS
showsPrec :: Int -> OptionInfo -> ShowS
$cshow :: OptionInfo -> String
show :: OptionInfo -> String
$cshowList :: [OptionInfo] -> ShowS
showList :: [OptionInfo] -> ShowS
Show, OptionInfo -> OptionInfo -> Bool
(OptionInfo -> OptionInfo -> Bool)
-> (OptionInfo -> OptionInfo -> Bool) -> Eq OptionInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OptionInfo -> OptionInfo -> Bool
== :: OptionInfo -> OptionInfo -> Bool
$c/= :: OptionInfo -> OptionInfo -> Bool
/= :: OptionInfo -> OptionInfo -> Bool
Eq, (forall (m :: * -> *). Quote m => OptionInfo -> m Exp)
-> (forall (m :: * -> *).
Quote m =>
OptionInfo -> Code m OptionInfo)
-> Lift OptionInfo
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => OptionInfo -> m Exp
forall (m :: * -> *). Quote m => OptionInfo -> Code m OptionInfo
$clift :: forall (m :: * -> *). Quote m => OptionInfo -> m Exp
lift :: forall (m :: * -> *). Quote m => OptionInfo -> m Exp
$cliftTyped :: forall (m :: * -> *). Quote m => OptionInfo -> Code m OptionInfo
liftTyped :: forall (m :: * -> *). Quote m => OptionInfo -> Code m OptionInfo
Lift)
fromSynList :: [Option] -> OptionInfo
fromSynList :: [Option] -> OptionInfo
fromSynList [Option]
opts = OptionInfo { synonyms :: [Option]
synonyms = [Option]
opts
, defaultVal :: Maybe String
defaultVal = Maybe String
forall a. Maybe a
Nothing
, expectsVal :: Bool
expectsVal = Bool
False
, isRepeated :: Bool
isRepeated = Bool
False }
type OptInfoMap = Map Option OptionInfo
type OptFormat = (OptPattern, OptInfoMap)
data OptParserState = OptParserState
{ OptParserState -> OptInfoMap
optInfoMap :: OptInfoMap
, OptParserState -> Arguments
parsedArgs :: Arguments
, OptParserState -> Bool
inShortOptStack :: Bool
, OptParserState -> Bool
inTopLevelSequence :: Bool
} deriving (Int -> OptParserState -> ShowS
[OptParserState] -> ShowS
OptParserState -> String
(Int -> OptParserState -> ShowS)
-> (OptParserState -> String)
-> ([OptParserState] -> ShowS)
-> Show OptParserState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OptParserState -> ShowS
showsPrec :: Int -> OptParserState -> ShowS
$cshow :: OptParserState -> String
show :: OptParserState -> String
$cshowList :: [OptParserState] -> ShowS
showList :: [OptParserState] -> ShowS
Show)
fromOptInfoMap :: OptInfoMap -> OptParserState
fromOptInfoMap :: OptInfoMap -> OptParserState
fromOptInfoMap OptInfoMap
m = OptParserState { optInfoMap :: OptInfoMap
optInfoMap = OptInfoMap
m
, parsedArgs :: Arguments
parsedArgs = Arguments
forall k a. Map k a
M.empty
, inShortOptStack :: Bool
inShortOptStack = Bool
False
, inTopLevelSequence :: Bool
inTopLevelSequence = Bool
True }
data ArgValue = MultiValue [String]
| Value String
| NoValue
| Counted Int
| Present
| NotPresent
deriving (Int -> ArgValue -> ShowS
[ArgValue] -> ShowS
ArgValue -> String
(Int -> ArgValue -> ShowS)
-> (ArgValue -> String) -> ([ArgValue] -> ShowS) -> Show ArgValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ArgValue -> ShowS
showsPrec :: Int -> ArgValue -> ShowS
$cshow :: ArgValue -> String
show :: ArgValue -> String
$cshowList :: [ArgValue] -> ShowS
showList :: [ArgValue] -> ShowS
Show, ArgValue -> ArgValue -> Bool
(ArgValue -> ArgValue -> Bool)
-> (ArgValue -> ArgValue -> Bool) -> Eq ArgValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ArgValue -> ArgValue -> Bool
== :: ArgValue -> ArgValue -> Bool
$c/= :: ArgValue -> ArgValue -> Bool
/= :: ArgValue -> ArgValue -> Bool
Eq, Eq ArgValue
Eq ArgValue =>
(ArgValue -> ArgValue -> Ordering)
-> (ArgValue -> ArgValue -> Bool)
-> (ArgValue -> ArgValue -> Bool)
-> (ArgValue -> ArgValue -> Bool)
-> (ArgValue -> ArgValue -> Bool)
-> (ArgValue -> ArgValue -> ArgValue)
-> (ArgValue -> ArgValue -> ArgValue)
-> Ord ArgValue
ArgValue -> ArgValue -> Bool
ArgValue -> ArgValue -> Ordering
ArgValue -> ArgValue -> ArgValue
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ArgValue -> ArgValue -> Ordering
compare :: ArgValue -> ArgValue -> Ordering
$c< :: ArgValue -> ArgValue -> Bool
< :: ArgValue -> ArgValue -> Bool
$c<= :: ArgValue -> ArgValue -> Bool
<= :: ArgValue -> ArgValue -> Bool
$c> :: ArgValue -> ArgValue -> Bool
> :: ArgValue -> ArgValue -> Bool
$c>= :: ArgValue -> ArgValue -> Bool
>= :: ArgValue -> ArgValue -> Bool
$cmax :: ArgValue -> ArgValue -> ArgValue
max :: ArgValue -> ArgValue -> ArgValue
$cmin :: ArgValue -> ArgValue -> ArgValue
min :: ArgValue -> ArgValue -> ArgValue
Ord)
type Arguments = Map Option ArgValue
data Docopt = Docopt { Docopt -> OptFormat
optFormat :: OptFormat
, Docopt -> String
usage :: String
}