on master help sign in
Re #199: Add option -o to json2yaml
commit16d360dcca5189e6b4b6ca9c100c4b9a28246943 authorAndreas Abel <andreas.abel@ifi.lmu.de> dateFri 17 Sep 2021, 11:59 (5 years ago) parent81e9b29
2 files changed+47−10
yaml/exe/Common.hs+11−3 yaml/exe/json2yaml.hs+36−7
yaml/exe/Common.hs+11−3
@@ -28,6 +28,16 @@ homepage = "https://github.com/snoyberg/yaml/"
2828version :: String
2929version = intercalate "." $ map show $ versionBranch Paths.version
3030
31+-- | The version header including given program name and 'homepage'.
32+
33+versionText :: String -> String
34+versionText self = unwords
35+ [ self
36+ , "version"
37+ , version
38+ , homepage -- concat [ "<", homepage, ">" ]
39+ ]
40+
3141-- | Option @--numeric-version@.
3242
3343numericVersionOption :: Parser (a -> a)
@@ -41,13 +51,11 @@ numericVersionOption =
4151
4252versionOption :: String -> Parser (a -> a)
4353versionOption self =
44− infoOption (unwords versionWords)
54+ infoOption (versionText self)
4555 $ long "version"
4656 <> short 'V'
4757 <> hidden
4858 <> help "Show version info."
49− where
50− versionWords = [ self, "version", version, homepage ]
5159
5260-- * Misc
5361---------------------------------------------------------------------------
yaml/exe/json2yaml.hs+36−7
@@ -10,14 +10,16 @@ import Data.Semigroup ( Semigroup(..) )
1010#endif
1111
1212import Options.Applicative
13− ( Parser
14− , action, execParser, header, help, helper, info
15− , metavar, strArgument, value
13+ ( Parser, (<|>)
14+ , action, execParser, footerDoc, headerDoc, help, helper, hidden, info
15+ , long, metavar, short, strArgument, strOption, value
1616 )
17+import Options.Applicative.Help.Pretty
18+ ( vcat, text )
1719
1820import System.Exit ( die )
1921
20−import Common ( dashToNothing, versionOption, numericVersionOption )
22+import Common ( dashToNothing, versionText, versionOption, numericVersionOption )
2123
2224data Options = Options
2325 { optInput :: Maybe FilePath
@@ -37,9 +39,22 @@ options :: IO Options
3739options =
3840 execParser $
3941 info (helper <*> versionOption self <*> numericVersionOption <*> programOptions)
40− (header "Reads a JSON document and writes it out as YAML document.")
42+ $ headerDoc hdoc
43+ <> footerDoc fdoc
4144
4245 where
46+ hdoc = Just $ vcat $ map text
47+ [ versionText self
48+ , ""
49+ , "Reads a JSON document and writes it out as YAML document."
50+ ]
51+ fdoc = Just $ text $ concat
52+ [ "The old call pattern '"
53+ , self
54+ , " IN OUT' is also accepted, but deprecated."
55+ ]
56+
57+ programOptions :: Parser Options
4358 programOptions = Options <$> oInput <*> oOutput
4459
4560 oInput :: Parser (Maybe FilePath)
@@ -48,15 +63,29 @@ options =
4863 $ metavar "IN"
4964 <> value "-"
5065 <> action "file"
51− <> help "The input file containing the JSON document; use '-' for stdin."
66+ <> help "The input file containing the JSON document; use '-' for stdin (default)."
5267
5368 oOutput :: Parser (Maybe FilePath)
5469 oOutput = dashToNothing <$> do
70+ oOutputExplicit <|> oOutputImplicit
71+
72+ oOutputExplicit :: Parser FilePath
73+ oOutputExplicit =
74+ strOption
75+ $ long "output"
76+ <> short 'o'
77+ <> metavar "OUT"
78+ <> action "file"
79+ -- <> help "The file to hold the produced YAML document; use '-' for stdout (default)."
80+
81+ oOutputImplicit :: Parser FilePath
82+ oOutputImplicit =
5583 strArgument
5684 $ metavar "OUT"
5785 <> value "-"
5886 <> action "file"
59− <> help "The file to hold the produced YAML document; use '-' for stdout."
87+ <> hidden
88+ <> help "The file to hold the produced YAML document; use '-' for stdout (default)."
6089
6190-- | Exit with 'self'-stamped error message.
6291