packages feed

toolshed-0.13.0.0: src/Main.hs

{-
	Copyright (C) 2012 Dr. Alistair Ward

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]

	* Contains the entry-point to the program.

	* Facilitates testing.
-}

module Main(
-- * Types
-- ** Type-synonyms
--	CommandLineAction,
-- * Functions
	main
) where

import qualified	Data.List
import qualified	Data.Version
import qualified	Distribution.Package
import qualified	Distribution.Text
import qualified	Distribution.Version
import qualified	Paths_toolshed		as Paths	--Either local stub, or package-instance autogenerated by 'Setup.hs build'.
import qualified	System.Console.GetOpt	as G
import qualified	System.Environment
import qualified	System.Exit
import qualified	System.IO
import qualified	System.IO.Error
import qualified	ToolShed.Defaultable
import qualified	ToolShed.Test.QuickChecks

-- | Used to thread user-defined command-line options, though the list of functions which implement them.
type CommandLineAction	= () -> IO ()	--Supplied as the type-argument to 'G.OptDescr'.

-- | Parses the command-line arguments, to determine 'Test.CommandOptions.CommandOptions'.
main :: IO ()
main	= do
	progName	<- System.Environment.getProgName
	args		<- System.Environment.getArgs

	let
		usage :: String
		usage	= "Usage:\t" ++ G.usageInfo progName optDescrList

--Define the command-line options, and the 'CommandLineAction's used to handle them.
		optDescrList :: [G.OptDescr CommandLineAction]
		optDescrList	= [
--				 String	[String]		(G.ArgDescr CommandLineAction)												String
			G.Option "?"	["help"]		(G.NoArg $ const printUsage)												"Display this help-text & then exit.",
			G.Option ""	["version"]		(G.NoArg $ const printVersion)												"Print version-information & then exit.",
			G.Option "q"	["runQuickChecks"]	(G.NoArg $ const runQuickChecks)											"Run Quick-checks using arbitrary data & then exit."
		 ] where
			printVersion, printUsage, runQuickChecks :: IO ()
			printVersion	= System.IO.hPutStrLn System.IO.stderr (Distribution.Text.display packageIdentifier ++ "\n\nCopyright (C) 2011 Dr. Alistair Ward.\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions.\n\nWritten by Dr. Alistair Ward.")	>> System.Exit.exitWith System.Exit.ExitSuccess	where
				packageIdentifier :: Distribution.Package.PackageIdentifier
				packageIdentifier	= Distribution.Package.PackageIdentifier {
					Distribution.Package.pkgName	= Distribution.Package.PackageName "toolshed",
					Distribution.Package.pkgVersion	= Distribution.Version.Version (Data.Version.versionBranch Paths.version) []
				}

			printUsage	= System.IO.hPutStrLn System.IO.stderr usage	>> System.Exit.exitWith System.Exit.ExitSuccess
			runQuickChecks	= ToolShed.Test.QuickChecks.run			>> System.Exit.exitWith System.Exit.ExitSuccess

--	G.getOpt :: G.ArgOrder CommandLineAction -> [G.OptDescr Action] -> [String] -> ([Action], [String], [String])
	case G.getOpt G.RequireOrder optDescrList args of
		(commandLineActions, _, [])	-> Data.List.foldl' (>>=) (return {-to IO-monad-} ()) commandLineActions	>> System.Exit.exitWith System.Exit.ExitSuccess
		(_, _, errors)			-> System.IO.Error.ioError . System.IO.Error.userError $ concat errors ++ usage	--Throw.