packages feed

palindromes-0.1.1: src/Data/Algorithms/Palindromes/Main.hs

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Algorithms.Palindromes.Main
-- Copyright   :  (c) 2007 - 2009 Johan Jeuring
-- License     :  BSD3
--
-- Maintainer  :  johan@jeuring.net
-- Stability   :  experimental
-- Portability :  non-portable
--
-----------------------------------------------------------------------------
module Main where

import System.Environment (getArgs)
import System.IO 

import Data.Algorithms.Palindromes.Palindromes

helpMessage :: String
helpMessage = 
     "Usage:\n\n"
  ++ "    palindrome [command-line-options] input-file\n\n"
  ++ "The following options are available:\n"
  ++ "  --h: This message\n"
  ++ "  -p : Print the longest palindrome (default)\n"
  ++ "  -ps: Print the longest palindrome around each position in the input\n"
  ++ "  -l : Print the length of the longest palindrome\n"
  ++ "  -ls: Print the length of the longest palindrome around each position in the input\n"
  ++ "  -t : Print the longest palindrome ignoring case, spacing and punctuation\n"
  ++ "  -ts: Print the length of the longest text palindrome\n"
  
main :: IO ()
main = do
    putStrLn "*********************"
    putStrLn "* Palindrome Finder *"
    putStrLn "*********************"
    args <- getArgs
    case args of
      ['-':flag,filePath]  ->  do let function =  
                                        case flag of 
                                          "p"   ->  longestPalindrome 
                                          "ps"  ->  longestPalindromes 
                                          "l"   ->  lengthLongestPalindrome 
                                          "ls"  ->  lengthLongestPalindromes
                                          "t"   ->  longestTextPalindrome
                                          "ts"  ->  longestTextPalindromes
                                          _     ->  const helpMessage
                                  input <- readFile filePath
                                  putStrLn (function input)                                 
      [arg]                ->  do case arg of
                                    '-':_  -> putStrLn helpMessage 
                                    _      -> do input <- readFile arg
                                                 putStrLn (longestPalindrome input)
      _                    ->  putStrLn helpMessage