From 7b37fe52a8104e6f6eaceedb192465d2539e7399 Mon Sep 17 00:00:00 2001 From: William Martin Date: Wed, 20 Feb 2013 22:30:44 +0000 Subject: [PATCH 1/2] Implemented arguments processor. --- diffr.cabal | 18 ++++++++--- src/{diffr/diff => Diffr/Diff}/Main.hs | 8 ++--- src/{diffr/patch => Diffr/Patch}/Main.hs | 0 src/Diffr/Util/ArgumentsProcessor.hs | 39 ++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) rename src/{diffr/diff => Diffr/Diff}/Main.hs (85%) rename src/{diffr/patch => Diffr/Patch}/Main.hs (100%) create mode 100644 src/Diffr/Util/ArgumentsProcessor.hs diff --git a/diffr.cabal b/diffr.cabal index f89adb5..6458a1b 100644 --- a/diffr.cabal +++ b/diffr.cabal @@ -1,5 +1,5 @@ Name: diffr -Version: 0.0 +Version: 0.2 Description: Intelligent diff/patch tool that knows how to copy and move, has an 'r' at the end of its name. License: GPL-3 License-file: LICENCE @@ -8,12 +8,20 @@ Maintainer: Build-Type: Simple Cabal-Version: >=1.2 +Library + Build-Depends: base >= 4 + Hs-Source-Dirs: src + Exposed-Modules: Diffr.Util.ArgumentsProcessor + ghc-options: -Wall + Executable diffr - Main-is: src/diffr/diff/Main.hs + Main-is: Diffr/Diff/Main.hs Build-Depends: base >= 4 - ghc-options: -Wall + Other-Modules: Diffr.Util.ArgumentsProcessor + Hs-Source-Dirs: src + ghc-options: -Wall Executable patchr - Main-is: src/diffr/patch/Main.hs + Main-is: src/Diffr/Patch/Main.hs Build-Depends: base >= 4 - ghc-options: -Wall + ghc-options: -Wall diff --git a/src/diffr/diff/Main.hs b/src/Diffr/Diff/Main.hs similarity index 85% rename from src/diffr/diff/Main.hs rename to src/Diffr/Diff/Main.hs index 0b7249e..a147e2b 100644 --- a/src/diffr/diff/Main.hs +++ b/src/Diffr/Diff/Main.hs @@ -20,23 +20,23 @@ module Main ( main ) where +import Diffr.Util.ArgumentsProcessor( containsHelpArgument, extractOutputFile ) import System.Environment( getArgs ) import System.Exit ( exitFailure, exitSuccess ) --- | 'main' runs the main program +-- | 'main' runs diffr. main :: IO () main = do args <- getArgs - if not ( 2 == length args || 4 == length args ) + if ( containsHelpArgument args || not ( 2 == length args || 4 == length args ) ) then do printUsage exitFailure else do print ( length args ) + print ( extractOutputFile args ) exitSuccess - - -- | 'printUsage' prints the usage information for diffr. printUsage :: IO () printUsage = putStrLn ( "Usage: \n" ++ diff --git a/src/diffr/patch/Main.hs b/src/Diffr/Patch/Main.hs similarity index 100% rename from src/diffr/patch/Main.hs rename to src/Diffr/Patch/Main.hs diff --git a/src/Diffr/Util/ArgumentsProcessor.hs b/src/Diffr/Util/ArgumentsProcessor.hs new file mode 100644 index 0000000..9c83edc --- /dev/null +++ b/src/Diffr/Util/ArgumentsProcessor.hs @@ -0,0 +1,39 @@ +{- | + Module : Diffr.Util.ArgumentsProcessor + Description : Arguments processor for diffr. + Since : 0.2 + Authors : William Martin + License : This file is part of diffr-h. + + diffr-h 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. + + diffr-h 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 diffr-h. If not, see . +-} + +module Diffr.Util.ArgumentsProcessor ( containsHelpArgument, extractOutputFile ) where + +import Data.Char ( toLower ) + +-- | 'containsHelpArgument' checks if the given list of strings contains the help argument. +containsHelpArgument :: [[Char]] -> Bool +containsHelpArgument args = any isHelpArgument args + +-- | 'extractOutputFile' extracts the output file argument from a list of strings. +extractOutputFile :: [[Char]] -> [Char] +extractOutputFile args + | "-o" == head args = head ( tail args ) + | otherwise = extractOutputFile ( tail args ) + +-- | 'isHelpArgument' checks if a string is a help argument. +isHelpArgument :: [Char] -> Bool +isHelpArgument arg + | '-' == ( head arg ) = isHelpArgument( tail arg ) + | otherwise = "help" == ( map toLower arg ) From d0d30339be7daced602cfd982c7efdb09835ba2c Mon Sep 17 00:00:00 2001 From: William Martin Date: Wed, 26 Jun 2013 23:34:11 +0100 Subject: [PATCH 2/2] Used case-of pattern matching to improve safety. Improved debug print statements. --- src/Diffr/Diff/Main.hs | 3 ++- src/Diffr/Util/ArgumentsProcessor.hs | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Diffr/Diff/Main.hs b/src/Diffr/Diff/Main.hs index a147e2b..0b1c492 100644 --- a/src/Diffr/Diff/Main.hs +++ b/src/Diffr/Diff/Main.hs @@ -20,6 +20,7 @@ module Main ( main ) where +import Data.Char( intToDigit ) import Diffr.Util.ArgumentsProcessor( containsHelpArgument, extractOutputFile ) import System.Environment( getArgs ) import System.Exit ( exitFailure, exitSuccess ) @@ -33,7 +34,7 @@ main = do printUsage exitFailure else do - print ( length args ) + print ( [intToDigit ( length args )] ++ " arguments" ) print ( extractOutputFile args ) exitSuccess diff --git a/src/Diffr/Util/ArgumentsProcessor.hs b/src/Diffr/Util/ArgumentsProcessor.hs index 9c83edc..08a071b 100644 --- a/src/Diffr/Util/ArgumentsProcessor.hs +++ b/src/Diffr/Util/ArgumentsProcessor.hs @@ -24,16 +24,21 @@ import Data.Char ( toLower ) -- | 'containsHelpArgument' checks if the given list of strings contains the help argument. containsHelpArgument :: [[Char]] -> Bool -containsHelpArgument args = any isHelpArgument args +containsHelpArgument args = any isHelpArgument ( map ( map toLower ) args ) -- | 'extractOutputFile' extracts the output file argument from a list of strings. extractOutputFile :: [[Char]] -> [Char] -extractOutputFile args - | "-o" == head args = head ( tail args ) - | otherwise = extractOutputFile ( tail args ) +extractOutputFile args = case args of + [] -> "" + ("-o":[]) -> "" + ("-o":xs) -> head ( xs ) + _ -> extractOutputFile ( tail args ) -- | 'isHelpArgument' checks if a string is a help argument. isHelpArgument :: [Char] -> Bool -isHelpArgument arg - | '-' == ( head arg ) = isHelpArgument( tail arg ) - | otherwise = "help" == ( map toLower arg ) +isHelpArgument arg = case arg of + [] -> False + ('-':[]) -> False + ('-':xs) -> isHelpArgument( xs ) + "help" -> True + _ -> False