@@ -7,27 +7,26 @@ import Prelude hiding (putStr, getContents)
77import Data.Aeson (encode, Value)
88import Data.ByteString (getContents)
99import Data.ByteString.Lazy (putStr)
10−import Data.List (intercalate)
1110#if !MIN_VERSION_base(4,11,0)
1211import Data.Semigroup (Semigroup(..))
1312#endif
14−import Data.Version (Version(versionBranch))
1513import Data.Yaml (decodeFileEither, decodeEither')
1614
1715import Options.Applicative
1816 ( Parser
19− , action, execParser, header, help, helper, hidden
20− , info, infoOption, long, metavar, short, strArgument
17+ , action, execParser, header, help, helper, info
18+ , metavar, strArgument
2119 )
2220
2321import System.Exit
2422import System.IO (stderr, hPrint)
2523
26−import qualified Paths_yaml as Paths
24+import Common
25+ ( versionOption, numericVersionOption, dashToNothing )
2726
2827newtype Options = Options
2928 { optInput :: Maybe FilePath
30− -- ^ Nothing means @stdin@.
29+ -- ^ 'Nothing' means @stdin@.
3130 }
3231
3332-- | Name of the executable.
@@ -35,52 +34,23 @@ newtype Options = Options
3534self :: String
3635self = "yaml2json"
3736
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−
4837-- | Parse options; handle parse errors, @--help@, @--version@.
4938
5039options :: IO Options
5140options =
5241 execParser $
53− info (helper <*> versionOption <*> numericVersionOption <*> programOptions)
54− (header "Reads given YAML document and prints it JSON format to standard out.")
42+ info (helper <*> versionOption self <*> numericVersionOption <*> programOptions)
43+ (header "Reads given YAML document and prints it in JSON format to standard out.")
5544
5645 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−
7146 programOptions = Options <$> oInput
7247
7348 oInput :: Parser (Maybe FilePath)
74− oInput = dashToStdin <$> do
49+ oInput = dashToNothing <$> do
7550 strArgument
7651 $ metavar "FILE"
7752 <> action "file"
7853 <> 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
8454
8555showJSON :: Show a => Either a Value -> IO b
8656showJSON = \case