]> gitweb.michael.orlitzky.com - spline3.git/blob - src/CommandLine.hs
f413e2fb214b98840795c8c5201b8d18df517104
[spline3.git] / src / CommandLine.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2
3 module CommandLine (
4 Args(..),
5 apply_args,
6 program_name,
7 show_help)
8 where
9
10 -- Get the version from Cabal.
11 import Paths_spline3 (version)
12 import Data.Version (showVersion)
13
14 import Data.String.Utils (startswith)
15 import System.Console.CmdArgs (
16 CmdArgs,
17 Data,
18 Mode,
19 Typeable,
20 (&=),
21 argPos,
22 cmdArgsApply,
23 cmdArgsMode,
24 def,
25 details,
26 groupname,
27 help,
28 helpArg,
29 program,
30 typ,
31 summary,
32 versionArg
33 )
34
35 import System.Console.CmdArgs.Explicit (process)
36 import System.Environment (getArgs, withArgs)
37 import System.Exit (ExitCode(..), exitWith)
38 import System.IO (hPutStrLn, stderr)
39
40 import ExitCodes
41
42
43
44 data Args =
45 Args { depth :: Int,
46 height :: Int,
47 input :: FilePath,
48 lower_threshold :: Int,
49 output :: FilePath,
50 scale :: Int,
51 slice :: Maybe Int,
52 upper_threshold :: Int,
53 width :: Int }
54 deriving (Show, Data, Typeable)
55
56 description :: String
57 description =
58 "Interpolate volumetric data according to \"Local quasi-interpolation " ++
59 "by cubic C^1 splines on type-6 tetrahedral partitions.\" The defaults " ++
60 "are tailored to the MRI data contained in data/mri.bin from the " ++
61 "Stanford volume data archive at http://graphics.stanford.edu/data/voldata/."
62
63 program_name :: String
64 program_name = "spline3"
65
66 spline3_summary :: String
67 spline3_summary =
68 program_name ++ "-" ++ (showVersion version)
69
70 depth_default :: Int
71 depth_default = 109
72
73 depth_help :: String
74 depth_help =
75 "The size of the z dimension (default: " ++ (show depth_default) ++ ")"
76
77 height_default :: Int
78 height_default = 256
79
80 height_help :: String
81 height_help =
82 "The size of the y dimension (default: " ++ (show height_default) ++ ")"
83
84 lower_threshold_default :: Int
85 lower_threshold_default = 1400
86
87 lower_threshold_help :: String
88 lower_threshold_help =
89 "The lower limit for voxel values, only used in 2D (default: " ++
90 (show lower_threshold_default) ++ ")"
91
92 scale_default :: Int
93 scale_default = 2
94
95 scale_help :: String
96 scale_help =
97 "The magnification scale. A scale of 2 would result " ++
98 "in an image twice as large as the original. (default: " ++
99 (show scale_default) ++ ")"
100
101 slice_help :: String
102 slice_help =
103 "The index of the two-dimensional slice to use if no depth is specified"
104
105 upper_threshold_default :: Int
106 upper_threshold_default = 2500
107
108 upper_threshold_help :: String
109 upper_threshold_help =
110 "The upper limit for voxel values, only used in 2D (default: " ++
111 (show upper_threshold_default) ++ ")"
112
113 width_default :: Int
114 width_default = 256
115
116 width_help :: String
117 width_help =
118 "The size of the x dimension (default: " ++ (show width_default) ++ ")"
119
120 arg_spec :: Mode (CmdArgs Args)
121 arg_spec =
122 cmdArgsMode $
123 Args {
124 depth = depth_default &= groupname "Dimensions" &= help depth_help,
125 height = height_default &= groupname "Dimensions" &= help height_help,
126 input = def &= typ "INPUT" &= argPos 0,
127
128 lower_threshold = lower_threshold_default &= groupname "2D options"
129 &= help lower_threshold_help,
130
131 output = def &= typ "OUTPUT" &= argPos 1,
132 scale = scale_default &= help scale_help,
133 slice = Nothing &= groupname "2D options" &= help slice_help,
134
135 upper_threshold = upper_threshold_default &= groupname "2D options"
136 &= help upper_threshold_help,
137
138 width = width_default &= groupname "Dimensions" &= help width_help
139 }
140 &= program program_name
141 &= summary spline3_summary
142 &= details [description]
143 &= helpArg [groupname "Common flags"]
144 &= versionArg [groupname "Common flags"]
145
146 -- Infix notation won't work, the arguments are backwards!
147 is_missing_arg_error :: String -> Bool
148 is_missing_arg_error s =
149 startswith "Requires at least" s
150
151
152 show_help :: IO Args
153 show_help = withArgs ["--help"] apply_args
154
155 parse_args :: IO (CmdArgs Args)
156 parse_args = do
157 x <- getArgs
158 let y = process arg_spec x
159 case y of
160 Right result -> return result
161 Left err ->
162 if (is_missing_arg_error err) then
163 -- Start this function over, pretending that --help was
164 -- passed.
165 withArgs ["--help"] parse_args
166 else do
167 hPutStrLn stderr err
168 exitWith (ExitFailure exit_arg_parse_failed)
169
170
171 -- | Really get the command-line arguments. This calls 'parse_args'
172 -- first to replace the default "wrong number of arguments" error,
173 -- and then runs 'cmdArgsApply' on the result to do what the
174 -- 'cmdArgs' function usually does.
175 apply_args :: IO Args
176 apply_args =
177 parse_args >>= cmdArgsApply