Skip to content

Commit 7d82954

Browse files
committed
Use cmdargs for command line parsing.
1 parent f9cbff8 commit 7d82954

File tree

6 files changed

+135
-70
lines changed

6 files changed

+135
-70
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.idea
22
dist/
3+
.dist-buildwrapper/
4+
cabal-dev/
5+
.settings/
6+
.project
37

Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

diffr.cabal

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
Name: diffr
2-
Version: 0.0
3-
Description: Intelligent diff/patch tool that knows how to copy and move, has an 'r' at the end of its name.
4-
License: GPL-3
5-
License-file: LICENCE
6-
Author: Amaury Couste, Jakub Kozlowski, William Martin
7-
Maintainer:
8-
Build-Type: Simple
9-
Cabal-Version: >=1.2
1+
name: diffr
2+
version: 0.1
3+
description: Intelligent diff/patch tool that knows how to copy and move, has an 'r' at the end of its name.
4+
license: GPL-3
5+
license-file: LICENCE
6+
author: Amaury Couste, Jakub Kozlowski, William Martin
7+
maintainer:
8+
build-type: Simple
9+
cabal-version: >=1.2
1010

11-
Executable diffr
12-
Main-is: src/diffr/diff/Main.hs
13-
Build-Depends: base >= 4
14-
ghc-options: -Wall
15-
16-
Executable patchr
17-
Main-is: src/diffr/patch/Main.hs
18-
Build-Depends: base >= 4
19-
ghc-options: -Wall
11+
executable diffr
12+
hs-source-dirs: src
13+
main-is: Diffr/Main.hs
14+
build-Depends:
15+
base >= 4,
16+
cmdargs > 0.10
17+
ghc-options: -Wall
18+
other-modules: Diffr.Util
19+
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
12
{- |
23
Module : Main
3-
Description : Main entry point for patchr.
4+
Description : Main entry point for diffr.
45
Since : 0.1
5-
Authors : William Martin
6+
Authors : William Martin, Jakub Kozlowski
67
License : This file is part of diffr-h.
78
89
diffr-h is free software: you can redistribute it and/or modify
@@ -18,12 +19,15 @@
1819
along with diffr-h. If not, see <http://www.gnu.org/licenses/>.
1920
-}
2021

21-
module Main where
22+
module Main(main) where
2223

23-
import System.Environment
24+
import System.Environment ( getArgs, withArgs )
25+
import qualified System.Console.CmdArgs as CM
26+
import qualified Diffr.Util as DU
2427

25-
-- | 'main' runs the main program
2628
main :: IO ()
27-
main = print usage
28-
29-
usage = "patchr says hello"
29+
main = do
30+
args <- getArgs
31+
-- If the user did not specify any arguments, pretend as "--help" was given
32+
opts <- (if null args then withArgs ["--help"] else id) $ CM.cmdArgsRun DU.diffrModes
33+
print opts

src/Diffr/Util.hs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{- |
3+
Module : Diffr.Util
4+
Description : Utility functions and configuration options
5+
Since : 0.1
6+
Authors : William Martin, Jakub Kozlowski
7+
License : This file is part of diffr-h.
8+
9+
diffr-h is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
diffr-h is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
You should have received a copy of the GNU General Public License
19+
along with diffr-h. If not, see <http://www.gnu.org/licenses/>.
20+
-}
21+
module Diffr.Util (
22+
DConfig(..)
23+
, diffrModes
24+
) where
25+
26+
import System.Console.CmdArgs
27+
28+
{-| Static values -}
29+
_PROGRAM_NAME, _PROGRAM_VERSION,
30+
_PROGRAM_INFO, _PROGRAM_ABOUT, _COPYRIGHT :: String
31+
_PROGRAM_NAME = "diffr"
32+
_PROGRAM_VERSION = "0.1"
33+
_PROGRAM_INFO = _PROGRAM_NAME ++ " version " ++ _PROGRAM_VERSION
34+
_PROGRAM_ABOUT = unwords [
35+
"An intelligent diff/patch tool"
36+
, "that knows how to copy and move,"
37+
, "has an 'r' at the end of its name"
38+
, "and is written in Haskell."]
39+
_COPYRIGHT = "(C) diffr 2013"
40+
41+
------------------------------------------------
42+
43+
{-| Configuration for running diffr's commands -}
44+
data DConfig =
45+
46+
-- | Configuration for running diff command
47+
Diff { -- | Path to the 'base' file we will diff against
48+
baseFile :: FilePath
49+
50+
-- | Path to the 'new' file we will compare to 'base' file
51+
, newFile :: FilePath
52+
53+
-- | Path to the output file where to write the diff file
54+
, dOutFile :: Maybe FilePath
55+
}
56+
57+
-- | Configuration for running patch command
58+
| Patch { -- | Path to the 'original' file we will apply patch to
59+
originalFile :: FilePath
60+
61+
-- | Path to the 'patch' file we will apply to 'originalFile'
62+
, patchFile :: FilePath
63+
64+
-- | Path to the output file where to write the patched file
65+
, pOutFile :: Maybe FilePath
66+
} deriving (Show, Data, Typeable)
67+
68+
{-| Annotate the 'Diff' configuration -}
69+
diff :: DConfig
70+
diff = Diff
71+
{ baseFile = def &= argPos 0
72+
&= typ "BASEFILE"
73+
, newFile = def &= argPos 1
74+
&= typ "NEWFILE"
75+
, dOutFile = def &= help "path to the output file"
76+
&= name "output-file" &= typFile
77+
}
78+
79+
{-| Annotate the 'Patch' configuration -}
80+
patch :: DConfig
81+
patch = Patch
82+
{ originalFile = def &= argPos 0
83+
&= typ "ORIGINALFILE"
84+
, patchFile = def &= argPos 1
85+
&= typ "PATCHFILE"
86+
, pOutFile = def &= help "Path to the output file where to write the patched file"
87+
&= name "output-file"
88+
&= typFile
89+
}
90+
91+
{-| Available commands -}
92+
diffrModes :: Mode (CmdArgs DConfig)
93+
diffrModes = cmdArgsMode $ modes [diff, patch]
94+
&= versionArg [explicit, name "version", name "v", summary _PROGRAM_INFO]
95+
&= summary (_PROGRAM_INFO ++ ", " ++ _COPYRIGHT)
96+
&= help _PROGRAM_ABOUT
97+
&= helpArg [explicit, name "help", name "h"]
98+
&= program _PROGRAM_NAME
99+

src/diffr/diff/Main.hs

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)