@@ -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(..))
14−import Data.Version (Version(versionBranch))
15import Data.Yaml (decodeFileEither, decodeEither')
13import Data.Yaml (decodeFileEither, decodeEither')
17import Options.Applicative
15import Options.Applicative
19− , action, execParser, header, help, helper, hidden
17+ , action, execParser, header, help, helper, info
20− , info, infoOption, long, metavar, short, strArgument
18+ , metavar, strArgument
23import System.Exit
21import System.Exit
24import System.IO (stderr, hPrint)
22import System.IO (stderr, hPrint)
26−import qualified Paths_yaml as Paths
24+import Common
25+ ( versionOption, numericVersionOption, dashToNothing )
28newtype Options = Options
27newtype Options = Options
29 { optInput :: Maybe FilePath
28 { optInput :: Maybe FilePath
30− -- ^ Nothing means @stdin@.
29+ -- ^ 'Nothing' means @stdin@.
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"
38−-- | Homepage for this program.
41−homepage = "https://github.com/snoyberg/yaml/"
43−-- | Version in @x.y.z@ format.
46−version = intercalate "." $ map show $ versionBranch Paths.version
48-- | Parse options; handle parse errors, @--help@, @--version@.
37-- | Parse options; handle parse errors, @--help@, @--version@.
50options :: IO Options
39options :: IO Options
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.")
58− infoOption (unwords versionWords)
62− <> help "Show version info."
63− versionWords = [ self, "version", version, homepage ]
65− numericVersionOption =
67− $ long "numeric-version"
69− <> help "Show just version number."
71 programOptions = Options <$> oInput
46 programOptions = Options <$> oInput
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."
80− -- dash is interpreted as stdin
85showJSON :: Show a => Either a Value -> IO b
55showJSON :: Show a => Either a Value -> IO b
86showJSON = \case
56showJSON = \case