Permalink
Browse files

Main.hs is now using cmdargs to process command line arguments

  • Loading branch information...
1 parent 1921280 commit 3232eef47119702c66016913544b6bbef356b51c Rob Stewart committed Sep 7, 2011
Showing with 63 additions and 4 deletions.
  1. +63 −4 Data/GPS/Gps2HtmlReport/Main.hs
@@ -1,25 +1,71 @@
+
+{-# LANGUAGE RecordWildCards,DeriveDataTypeable #-}
+
module Main where
-import Data.GPS
+import Data.GPS hiding (name, id)
import System.FilePath
import System.Directory
-import Text.Html
+import Text.Html hiding (name)
+import System.Console.CmdArgs
import Data.GPS.Gps2HtmlReport.HTMLGenerator
import Data.GPS.Gps2HtmlReport.JourneyCharts
import Data.GPS.Gps2HtmlReport.DrawOsm
+data MyOptions = MyOptions
+ { imageOnly :: Bool
+ } deriving (Data, Typeable, Show, Eq)
+-- Customize your options, including help messages, shortened names, etc.
+myProgOpts :: MyOptions
+myProgOpts = MyOptions
+ { imageOnly = def &= help "Generates only an image of the track overlay on an OpenStreetMap layer"
+ }
+
+getOpts :: IO MyOptions
+getOpts = cmdArgs $ myProgOpts
+ &= verbosityArgs [explicit, name "Verbose", name "V"] []
+ &= versionArg [explicit, name "version", name "v", summary _PROGRAM_INFO]
+ &= summary (_PROGRAM_INFO ++ ", " ++ _COPYRIGHT)
+ &= help _PROGRAM_ABOUT
+ &= helpArg [explicit, name "help", name "h"]
+ &= program _PROGRAM_NAME
+
+_PROGRAM_NAME = "gps2HtmlReport"
+_PROGRAM_VERSION = "0.2"
+_PROGRAM_INFO = _PROGRAM_NAME ++ " version " ++ _PROGRAM_VERSION
+_PROGRAM_ABOUT = "A Haskell utility to generate HTML page reports of GPS Tracks and Track overlays on OpenStreetMap tiles"
+_COPYRIGHT = "(C) Rob Stewart 2011"
--- | Reads the current directory for all .gpx files, then maps to `generateReport' for each one
main :: IO [()]
main = do
+ opts <- getOpts
+ optionHandler opts
+
+-- Before directly calling your main program, you should warn your user about incorrect arguments, if any.
+optionHandler :: MyOptions -> IO [()]
+optionHandler opts@MyOptions{..} =
+ exec opts
+
+exec :: MyOptions -> IO [()]
+exec MyOptions{..} =
+ if imageOnly
+ then processGps False
+ else processGps True
+
+
+-- | Reads the current directory for all .gpx files, then maps to `generateReport' for each one
+processGps :: Bool -> IO [()]
+processGps fullReport = do
curDir <- getCurrentDirectory
allFiles <- getDirectoryContents curDir
let allFilesSplit = map splitExtension allFiles
let gpxFiles = filter (\(_,b) -> b==".gpx") allFilesSplit
putStr ("Processing "++show (length gpxFiles)++" file(s)...\n")
- mapM (\(a,b) -> generateReport (curDir++"/"++a) (a++b)) gpxFiles
+ if fullReport
+ then mapM (\(a,b) -> generateReport (curDir++"/"++a) (a++b)) gpxFiles
+ else mapM (\(a,b) -> generateImgOnly (curDir++"/"++a) (a++b)) gpxFiles
-- | Creates empty directory for each web report
createEmptyDir :: FilePath -> IO ()
@@ -42,4 +88,17 @@ generateReport webDir gpxFile = do
putStr "Downloading OpenStreetMap tiles...\n"
generateOsmMap webDir points
putStr $ "Processing '"++gpxFile++"' complete. Report saved in: "++webDir++"/index.html\n"
+ return ()
+
+-- | Generates the HTML report for each .gpx file
+generateImgOnly :: FilePath -> FilePath -> IO ()
+generateImgOnly webDir gpxFile = do
+ points <- readGPX gpxFile
+ case length points of
+ 0 -> putStr "Unable to parse GPX file. Skipping..."
+ _ -> do
+ createEmptyDir webDir
+ putStr "Downloading OpenStreetMap tiles...\n"
+ generateOsmMap webDir points
+ putStr $ "Processing '"++gpxFile++"' complete. Image saved in: "++webDir++"/osm.png\n"
return ()

0 comments on commit 3232eef

Please sign in to comment.