packages feed

palindromes-0.1: src/Data/Algorithm/Palindromes/Main.hs

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Algorithm.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.Algorithm.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"
  ++ "  -tl: 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)                                 
      [filePath]        ->  do input <- readFile filePath
                               putStrLn (longestPalindrome input)
      _                 ->  putStrLn helpMessage