File: ExpandArgsAt.hs

package info (click to toggle)
haskell-cmdargs 0.10.22-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 352 kB
  • sloc: haskell: 2,972; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 1,346 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# LANGUAGE RecordWildCards #-}
module System.Console.CmdArgs.Explicit.ExpandArgsAt(expandArgsAt) where

import System.FilePath


-- | Expand @\@@ directives in a list of arguments, usually obtained from 'getArgs'.
--   As an example, given the file @test.txt@ with the lines @hello@ and @world@:
--
-- > expandArgsAt ["@test.txt","!"] == ["hello","world","!"]
--
--   Any @\@@ directives in the files will be recursively expanded (raising an error
--   if there is infinite recursion).
--
--   To supress @\@@ expansion, pass any @\@@ arguments after @--@.
expandArgsAt :: [String] -> IO [String]
expandArgsAt args = do
        ebefore <- mapM (f [] ".") before
        return $ concat ebefore ++ after
    where
        (before,after) = break (== "--") args

        f seen dir ('@':x)
            | x `elem` seen = error $ unlines $
                "System.Console.CmdArgs.Explicit.expandArgsAt, recursion in @ directives:" :
                map ("  "++) (reverse $ x:seen)
            | length seen > 15 = error $ unlines $
                "System.Console.CmdArgs.Explicit.expandArgsAt, over 15 @ directives deep:" :
                map ("  "++) (reverse seen)
            | otherwise = do
                src <- readFile $ dir </> x
                fmap concat $ mapM (f (x:seen) (takeDirectory x)) $ lines src
        f _ _ x = return [x]