summaryrefslogtreecommitdiff
path: root/Coalpit.hs
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2017-12-17 00:44:06 +0300
committerdefanor <defanor@uberspace.net>2017-12-17 00:44:06 +0300
commit1de2f778183a8890c0aa8e1f0486002f504649b5 (patch)
treee1dfd45825d4b2bdfbe186d66a00644bcc06edac /Coalpit.hs
parent9a0b116cd7a78590cd46bb17bbd542d93de2acef (diff)
Add DSV support
Diffstat (limited to 'Coalpit.hs')
-rw-r--r--Coalpit.hs28
1 files changed, 24 insertions, 4 deletions
diff --git a/Coalpit.hs b/Coalpit.hs
index 42fb1ae..a0a7ac2 100644
--- a/Coalpit.hs
+++ b/Coalpit.hs
@@ -1,5 +1,5 @@
{- |
-Description : Command-line options parsing and printing
+Description : Command-line options and DSV parsing and printing
Maintainer : defanor <defanor@uberspace.net>
Stability : unstable
Portability : non-portable (uses GHC extensions)
@@ -33,6 +33,8 @@ main = do
Right x -> do
print (x :: Foo)
print $ 'toArgs' 'defOpt' x
+ putStr $ 'showDSV' 'defOpt' [x]
+ print ('readDSV' 'defOpt' $ 'showDSV' 'defOpt' [x] :: [Either String Foo])
@
Then, in a shell:
@@ -40,9 +42,13 @@ Then, in a shell:
> $ ./Example 'a string'
> Foo {bar = Nothing, baz = "a string"}
> ["a string"]
+> "a string"
+> [Right (Foo {bar = Nothing, baz = "a string"})]
> $ ./Example --bar 42 'a string'
> Foo {bar = Just 42, baz = "a string"}
> ["--bar","42","a string"]
+> --bar 42 "a string"
+> [Right (Foo {bar = Just 42, baz = "a string"})]
> $ ./Example --bar foo
> arguments:1:3:
> Failed to read: foo
@@ -64,6 +70,8 @@ module Coalpit (
-- * Utility functions
, fromArgs
, usage
+ , showDSV
+ , readDSV
-- * Options
, Options(..)
, defOpt
@@ -91,10 +99,14 @@ import System.Exit (ExitCode)
import Network.URI (URI, parseURIReference, uriToString)
import Coalpit.Parsing
+import Coalpit.DSV
-- | Printing and parsing options.
-data Options = Options { conNameMod :: String -> String
+data Options = Options { fieldSeparator :: Char
+ -- ^ DSV field separator ('showDSV',
+ -- 'readDSV').
+ , conNameMod :: String -> String
-- ^ Constructor name modifier.
, selNameMod :: String -> String
-- ^ Record selector name modifier.
@@ -112,12 +124,12 @@ data Options = Options { conNameMod :: String -> String
, scientificFormat :: FPFormat
, scientificDecimals :: Maybe Int
, uriUserInfo :: String -> String
- -- ^ Used to map the userinfo part of the URI.
+ -- ^ Used to map userinfo parts of URIs.
}
-- | Default options.
defOpt :: Options
-defOpt = Options (map toLower) (("--" ++) . map toLower) False True
+defOpt = Options ' ' (map toLower) (("--" ++) . map toLower) False True
defaultTimeLocale (iso8601DateFormat Nothing) "%H:%M:%S"
(iso8601DateFormat (Just "%H:%M:%S")) Generic Nothing id
@@ -151,6 +163,14 @@ fromArgs opt args = case parse (argParser opt) "arguments" (map CLArg args) of
usage :: Coalpit a => Options -> Proxy a -> String
usage opt = argHelper opt []
+-- | Shows values in DSV format.
+showDSV :: Coalpit a => Options -> [a] -> String
+showDSV opt = composeDSV (fieldSeparator opt) . map (toArgs opt)
+
+-- | Reads values from DSV format.
+readDSV :: Coalpit a => Options -> String -> [Either String a]
+readDSV opt = map (>>= fromArgs opt) . parseDSV (fieldSeparator opt)
+
-- Units
instance GCoalpit U1 where