cli-0.0.3: example/Main.hs
-- |
-- Module : Main
-- License : BSD-Style
-- Copyright : Copyright © 2014 Nicolas DI PRIMA
--
-- Maintainer : Nicolas DI PRIMA <nicolas@di-prima.fr>
-- Stability : experimental
-- Portability : unknown
--
module Main
( main
) where
import Application.CLI
import Control.Applicative
import Data.List
import System.Directory
data CLIls = CLIls FilePath
instance CLI CLIls where
name _ = "ls"
desc _ = "list file in the current directory"
options _ = [ OptHelp ["-a", "--all"] Nothing "include hidden files"
, OptHelp ["-s", "--separator"] (Just "string") "a separator to put between the found files (default is \" \")"
, OptHelp [] (Just "directory") "specify a directory to list the content from"
]
action (CLIls dir) _ =
withOptionalParameterStr ["-s", "--separator"] $ \mseparator ->
withFlag ["-a", "--all"] $ \withHidden ->
withOptionalStr $ \mdir _ -> do
let separator = maybe " " id mseparator
list <- getDirectoryContents $ maybe dir id $ mdir
if withHidden
then putStrLn $ intercalate separator list
else putStrLn $ intercalate separator $ filter (not . isHiddenFile) list
where
isHiddenFile :: FilePath -> Bool
isHiddenFile [] = True
isHiddenFile ('.':_) = True
isHiddenFile _ = False
data CLIla = CLIla
instance CLI CLIla where
name _ = "la"
desc _ = "list file in the current directory -- plus hidden one"
options _ = [ OptHelp [] (Just "directory") "the directory to list the content from (mandatory)"
, OptHelp ["-p", "--prefix"] (Just "string") "a prefix to the output"
]
action _ _ =
withOptionalParameterStr ["-p", "--prefix"] $ \mprefix ->
withStr "directory" $ \dir _ -> do
let prefix = maybe "" id mprefix
list <- filter (not . flip elem [ ".", ".." ]) <$> getDirectoryContents dir
mapM_ putStrLn $ map ((++) prefix) list
main :: IO ()
main = do
dir <- getCurrentDirectory
defaultMain
$ with Help
$ with CLIla
$ with (CLIls dir)
$ initialize "Example of use of CLI Library"