File: Common.hs

package info (click to toggle)
haskell-yaml 0.11.11.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: haskell: 2,371; makefile: 9
file content (68 lines) | stat: -rw-r--r-- 1,655 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}

-- | Code shared between @yaml2json@ and @json2yaml@.

module Common where

import Data.List            ( intercalate )
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup       ( Semigroup(..) )
#endif
import Data.Version         ( Version(versionBranch) )

import Options.Applicative  ( Parser, help, hidden, infoOption, long, short )

import qualified Paths_yaml as Paths

-- * Version info and option
---------------------------------------------------------------------------

-- | Homepage for @yaml2json@ and @json2yaml@.

homepage :: String
homepage = "https://github.com/snoyberg/yaml/"

-- | Version in @x.y.z@ format.

version :: String
version = intercalate "." $ map show $ versionBranch Paths.version

-- | The version header including given program name and 'homepage'.

versionText :: String -> String
versionText self = unwords
  [ self
  , "version"
  , version
  , homepage  -- concat [ "<", homepage, ">" ]
  ]

-- | Option @--numeric-version@.

numericVersionOption :: Parser (a -> a)
numericVersionOption =
  infoOption version
    $  long "numeric-version"
    <> hidden
    <> help "Show just version number."

-- | Option @--version@.

versionOption :: String -> Parser (a -> a)
versionOption self =
  infoOption (versionText self)
    $  long "version"
    <> short 'V'
    <> hidden
    <> help "Show version info."

-- * Misc
---------------------------------------------------------------------------

-- | @Just@ unless argument is @"-"@ (denoting @stdin@ or @stdout@).

dashToNothing :: FilePath -> Maybe FilePath
dashToNothing = \case
  "-"  -> Nothing
  file -> Just file