{-# LANGUAGE FlexibleInstances #-}-- | Functions for working with command line arguments and options.moduleSystem.Build.Args(quote,(~~),(~:),(~?),param,many,manys,(~~~>),(~~>),(-~>),(^^^),space,searchPath,tryEnvs,ArgShow(..))whereimportControl.MonadimportData.MaybeimportData.ListimportqualifiedData.MapasMimportSystem.EnvironmentimportSystem.FilePathclassArgShowawherearg::a->StringinstanceArgShow[Char]wherearg=idinstanceArgShowIntwherearg=show-- | Surrounds the given string in double-quotes and escapes any already there.quote::String->Stringquotes='"':(s>>=\x->ifx=='"'then"\\\""else[x])++"\""-- | An empty list if the boolean is @False@ otherwise the given string value with @'-'@ prepended.(~~)::String->Bool->Stringg~~k=ifkthen'-':gelse[]-- | If the given list of file paths is empty, then returns the empty list. Otherwise prepend @'-'@ to the string followed by @' '@ then the search path separator intercalated in the list of file paths.---- > Posix-- > "123" ~?? ["abc", "def"] == "-123 \"abc\":\"def\""-- > "123" ~?? ["abc", "def", "ghi"] == "-123 \"abc\":\"def\":\"ghi\""(~:)::String->[FilePath]->String_~:[]=[]s~:z='-':s++' ':[searchPathSeparator]>===<z-- | If the given value is @Nothing@ return the empty list, otherwise run the given function.(~?)::(k->[a])->Maybek->[a](~?)=maybe[]-- | If the given value is @Nothing@ return the empty list, otherwise prepend @'-'@ to the given string followed by the given character followed by surrounding the result of running the given function in double-quotes.---- > param "abc" 'x' id (Just "tuv") == "-abcx\"tuv\""-- > param "abc" 'x' id Nothing == ""param::String->Char->(k->String)->Maybek->Stringparamkcs=(~?)(\z->'-':k++c:quote(sz))-- | A parameter with many values interspersed by @' '@.---- > many "abc" ["tuv", "wxy"] == "-abc \"tuv\" -abc \"wxy\""many::String->[String]->Stringmanykv=intercalate" "$map(k~~~>)v-- | A parameter with many values interspersed by @' '@.---- > manys id "abc" ["tuv", "wxy"] == "-abc \"tuv\" -abc \"wxy\""manys::(a->String)->String->[a]->Stringmanysfk=manyk.mapf-- | Prepends @'-'@ followed by the first value then @' '@ then the second value surrounded by double-quotes.---- > "abc" ~~~> "def" == "-abc \"def\""(~~~>)::String->String->String(~~~>)=(.Just).(~~>)-- | If the given value is @Nothing@ return the empty list, otherwise prepend @'-'@ followed by the first value then @' '@ followed by surrounding the result of running the given function in double-quotes.---- > "abc" ~~> Just "def" == "-abc \"def\""-- > "abc" ~~> Nothing == ""(~~>)::(ArgShowk)=>String->Maybek->String(~~>)k=paramk' 'arg-- | If the given value is @Nothing@ return the empty list, otherwise prepend @'-'@ followed by the first value then @':'@ followed by surrounding the result of @show@ in double-quotes.---- > "abc" ~~> Just "def" == "-abc:\"def\""-- > "abc" ~~> Nothing == ""(-~>)::(ArgShowk)=>String->Maybek->String(-~>)k=paramk':'arg-- | Removes all empty lists from the first argument the intercalates the second argument.---- > ["abc", "", "def"] ^^^ "x" == "abcxdef"(^^^)::[[a]]->[a]->[a]g^^^t=Data.List.intercalatet(filter(not.null)g)-- | Surrounds each given value in double-quotes then intercalates @' '@.---- > space ["abc", "def"] == "\"abc\" \"def\""space::[String]->Stringspace=(>===<)" "-- | Surrounds each given value in double-quotes then intercalates @[searchPathSeparator]@.---- > searchPath ["abc", "def"] == "\"abc\":\"def\""searchPath::[String]->StringsearchPath=(>===<)[searchPathSeparator]-- | Look up the given environment variables. The first one found that exists has its associated function called to produce a value.tryEnvs::[(String,String->a)]->IO(Maybea)tryEnvses=doe<-getEnvironmentletk[]=Nothingk((a,b):t)=fmapb(a`M.lookup`M.fromListe)`mplus`ktreturn(kes)-- not exported(>===<)::String->[String]->Strings>===<k=intercalates(fmapquotek)