on master help sign in
Re #195: factor out parts of yaml2json into module Common
620ef8eAndreas Abel
yaml/exe/Common.hsnew file+60
@@ -0,0 +1,60 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE LambdaCase #-}
3+
4+-- | Code shared between @yaml2json@ and @json2yaml@.
5+
6+module Common where
7+
8+import Data.List ( intercalate )
9+#if !MIN_VERSION_base(4,11,0)
10+import Data.Semigroup ( Semigroup(..) )
11+#endif
12+import Data.Version ( Version(versionBranch) )
13+
14+import Options.Applicative ( Parser, help, hidden, infoOption, long, short )
15+
16+import qualified Paths_yaml as Paths
17+
18+-- * Version info and option
19+---------------------------------------------------------------------------
20+
21+-- | Homepage for @yaml2json@ and @json2yaml@.
22+
23+homepage :: String
24+homepage = "https://github.com/snoyberg/yaml/"
25+
26+-- | Version in @x.y.z@ format.
27+
28+version :: String
29+version = intercalate "." $ map show $ versionBranch Paths.version
30+
31+-- | Option @--numeric-version@.
32+
33+numericVersionOption :: Parser (a -> a)
34+numericVersionOption =
35+ infoOption version
36+ $ long "numeric-version"
37+ <> hidden
38+ <> help "Show just version number."
39+
40+-- | Option @--version@.
41+
42+versionOption :: String -> Parser (a -> a)
43+versionOption self =
44+ infoOption (unwords versionWords)
45+ $ long "version"
46+ <> short 'V'
47+ <> hidden
48+ <> help "Show version info."
49+ where
50+ versionWords = [ self, "version", version, homepage ]
51+
52+-- * Misc
53+---------------------------------------------------------------------------
54+
55+-- | @Just@ unless argument is @"-"@ (denoting @stdin@ or @stdout@).
56+
57+dashToNothing :: FilePath -> Maybe FilePath
58+dashToNothing = \case
59+ "-" -> Nothing
60+ file -> Just file
yaml/exe/yaml2json.hs+8−38
@@ -7,27 +7,26 @@ import Prelude hiding (putStr, getContents)
7import Data.Aeson (encode, Value)
7import Data.Aeson (encode, Value)
8import Data.ByteString (getContents)
8import Data.ByteString (getContents)
9import Data.ByteString.Lazy (putStr)
9import Data.ByteString.Lazy (putStr)
10−import Data.List (intercalate)
11#if !MIN_VERSION_base(4,11,0)
10#if !MIN_VERSION_base(4,11,0)
12import Data.Semigroup (Semigroup(..))
11import Data.Semigroup (Semigroup(..))
13#endif
12#endif
14−import Data.Version (Version(versionBranch))
15import Data.Yaml (decodeFileEither, decodeEither')
13import Data.Yaml (decodeFileEither, decodeEither')
16
14
17import Options.Applicative
15import Options.Applicative
18 ( Parser
16 ( Parser
19− , action, execParser, header, help, helper, hidden
17+ , action, execParser, header, help, helper, info
20− , info, infoOption, long, metavar, short, strArgument
18+ , metavar, strArgument
21 )
19 )
22
20
23import System.Exit
21import System.Exit
24import System.IO (stderr, hPrint)
22import System.IO (stderr, hPrint)
25
23
26−import qualified Paths_yaml as Paths
24+import Common
25+ ( versionOption, numericVersionOption, dashToNothing )
27
26
28newtype Options = Options
27newtype Options = Options
29 { optInput :: Maybe FilePath
28 { optInput :: Maybe FilePath
30− -- ^ Nothing means @stdin@.
29+ -- ^ 'Nothing' means @stdin@.
31 }
30 }
32
31
33-- | Name of the executable.
32-- | Name of the executable.
@@ -35,52 +34,23 @@ newtype Options = Options
35self :: String
34self :: String
36self = "yaml2json"
35self = "yaml2json"
37
36
38−-- | Homepage for this program.
39−
40−homepage :: String
41−homepage = "https://github.com/snoyberg/yaml/"
42−
43−-- | Version in @x.y.z@ format.
44−
45−version :: String
46−version = intercalate "." $ map show $ versionBranch Paths.version
47−
48-- | Parse options; handle parse errors, @--help@, @--version@.
37-- | Parse options; handle parse errors, @--help@, @--version@.
49
38
50options :: IO Options
39options :: IO Options
51options =
40options =
52 execParser $
41 execParser $
53− info (helper <*> versionOption <*> numericVersionOption <*> programOptions)
42+ info (helper <*> versionOption self <*> numericVersionOption <*> programOptions)
54− (header "Reads given YAML document and prints it JSON format to standard out.")
43+ (header "Reads given YAML document and prints it in JSON format to standard out.")
55
44
56 where
45 where
57− versionOption =
58− infoOption (unwords versionWords)
59− $ long "version"
60− <> short 'V'
61− <> hidden
62− <> help "Show version info."
63− versionWords = [ self, "version", version, homepage ]
64−
65− numericVersionOption =
66− infoOption version
67− $ long "numeric-version"
68− <> hidden
69− <> help "Show just version number."
70−
71 programOptions = Options <$> oInput
46 programOptions = Options <$> oInput
72
47
73 oInput :: Parser (Maybe FilePath)
48 oInput :: Parser (Maybe FilePath)
74− oInput = dashToStdin <$> do
49+ oInput = dashToNothing <$> do
75 strArgument
50 strArgument
76 $ metavar "FILE"
51 $ metavar "FILE"
77 <> action "file"
52 <> action "file"
78 <> help "The input file containing the YAML document; use '-' for stdin."
53 <> help "The input file containing the YAML document; use '-' for stdin."
79− where
80− -- dash is interpreted as stdin
81− dashToStdin = \case
82− "-" -> Nothing
83− file -> Just file
84
54
85showJSON :: Show a => Either a Value -> IO b
55showJSON :: Show a => Either a Value -> IO b
86showJSON = \case
56showJSON = \case
yaml/yaml.cabal+2−1
@@ -5,7 +5,7 @@ cabal-version: 1.12
5-- see: https://github.com/sol/hpack
5-- see: https://github.com/sol/hpack
6
6
7name: yaml
7name: yaml
8−version: 0.11.5.0
8+version: 0.11.6.0
9synopsis: Support for parsing and rendering YAML documents.
9synopsis: Support for parsing and rendering YAML documents.
10description: README and API documentation are available at <https://www.stackage.org/package/yaml>
10description: README and API documentation are available at <https://www.stackage.org/package/yaml>
11category: Data
11category: Data
@@ -151,6 +151,7 @@ executable json2yaml
151executable yaml2json
151executable yaml2json
152 main-is: yaml2json.hs
152 main-is: yaml2json.hs
153 other-modules:
153 other-modules:
154+ Common
154 Paths_yaml
155 Paths_yaml
155 hs-source-dirs:
156 hs-source-dirs:
156 exe
157 exe