module Cookbook.Recipes.Cline(parse,Cline(..),clineExtract,clArg) where
import qualified Cookbook.Essential.Continuous as Ct
import qualified Cookbook.Essential.Common as Cm
import qualified Cookbook.Ingredients.Lists.Modify as Md
import qualified Cookbook.Ingredients.Lists.Access as Ac
import qualified Cookbook.Recipes.Sanitize as Sn
data Cline = Flag Char | Argument String String deriving (Show,Eq)
singleParse y = Flag (last y)
doubleParse (a,b) = Argument (Ct.after a "--") b
clineExtract :: [String] -> [Cline]
clineExtract x = case x of
(a:b:[]) -> if Ac.count a '-' == 2 then [doubleParse (a,b)] else [singleParse a,singleParse b]
(a:[]) -> [singleParse a]
_ -> helper x
helper x
| Ac.count w1 '-' == 1 = singleParse w1 : clineExtract (w2:r)
| Ac.count w1 '-' == 2 = doubleParse (w1,w2) : clineExtract r
| otherwise = clineExtract (w2:r)
where (w1:w2:r) = x
parse :: String -> [Cline]
parse = clineExtract . (`Md.splitOn` ' ')
clArg :: String -> String
clArg x = if '-' `notElem` x && Sn.notNumeral x then ("--" ++ x) else x