never executed always true always false
1 module ElmFormat.CliFlags (Config(..), JsonMode(..), parser) where
2
3 import Prelude ()
4 import Relude hiding (stdin)
5
6 import ElmVersion (ElmVersion(..))
7
8 import qualified ElmVersion
9 import qualified Data.String as String
10 import qualified Options.Applicative as Opt
11 import qualified Text.PrettyPrint.ANSI.Leijen as PP
12
13
14 data Config = Config
15 { _input :: [FilePath]
16 , _output :: Maybe FilePath
17 , _yes :: Bool
18 , _validate :: Bool
19 , _stdin :: Bool
20 , _elmVersion :: Maybe ElmVersion
21 , _json :: Maybe JsonMode
22 }
23
24 data JsonMode
25 = ElmToJson
26 | JsonToElm
27
28
29 parser :: String -> Maybe String -> Opt.ParserInfo Config
30 parser elmFormatVersion experimental =
31 Opt.info
32 (Opt.helper <*> flags)
33 (helpInfo elmFormatVersion experimental)
34
35
36
37 -- COMMANDS
38
39 flags :: Opt.Parser Config
40 flags =
41 Config
42 <$> Opt.many input
43 <*> output
44 <*> yes
45 <*> validate
46 <*> stdin
47 <*> elmVersion
48 <*> json
49
50
51 -- HELP
52
53 helpInfo :: String -> Maybe String -> Opt.InfoMod Config
54 helpInfo elmFormatVersion experimental =
55 mconcat
56 [ Opt.fullDesc
57 , Opt.headerDoc $ Just top
58 , Opt.progDesc "Format Elm source files."
59 , Opt.footerDoc (Just examples)
60 ]
61 where
62 top =
63 PP.vcat $ concat
64 [ [ PP.text $ "elm-format " ++ elmFormatVersion ]
65 , case experimental of
66 Just surveyUrl ->
67 [ (PP.<$>) (PP.text "") $
68 PP.indent 4 $ PP.bold $
69 PP.fillSep $ map PP.text $ String.words $
70 "This version of elm-format contains features " ++
71 "that may or may not appear in future releases. " ++
72 "You can provide feedback about experimental features " ++
73 "at " ++ surveyUrl
74 ]
75 Nothing ->
76 []
77 ]
78
79 examples =
80 linesToDoc
81 [ "Examples:"
82 , " elm-format Main.elm # formats Main.elm"
83 , " elm-format Main.elm --output Main2.elm # formats Main.elm as Main2.elm"
84 , " elm-format src/ # format all *.elm files in the src directory"
85 , ""
86 , "Full guide to using elm-format at <https://github.com/avh4/elm-format>"
87 ]
88
89
90 linesToDoc :: [String] -> PP.Doc
91 linesToDoc lineList =
92 PP.vcat (map PP.text lineList)
93
94 yes :: Opt.Parser Bool
95 yes =
96 Opt.switch $
97 mconcat
98 [ Opt.long "yes"
99 , Opt.help "Reply 'yes' to all automated prompts."
100 ]
101
102 validate :: Opt.Parser Bool
103 validate =
104 Opt.switch $
105 mconcat
106 [ Opt.long "validate"
107 , Opt.help "Check if files are formatted without changing them."
108 ]
109
110
111 output :: Opt.Parser (Maybe FilePath)
112 output =
113 Opt.optional $ Opt.strOption $
114 mconcat
115 [ Opt.long "output"
116 , Opt.metavar "FILE"
117 , Opt.help "Write output to FILE instead of overwriting the given source file."
118 ]
119
120 input :: Opt.Parser FilePath
121 input =
122 Opt.strArgument $ Opt.metavar "INPUT"
123
124 stdin :: Opt.Parser Bool
125 stdin =
126 Opt.switch $
127 mconcat
128 [ Opt.long "stdin"
129 , Opt.help "Read from stdin, output to stdout."
130 ]
131
132
133 elmVersion :: Opt.Parser (Maybe ElmVersion)
134 elmVersion =
135 Opt.optional $
136 Opt.option (Opt.eitherReader ElmVersion.parse) $
137 mconcat
138 [ Opt.long "elm-version"
139 , Opt.metavar "VERSION"
140 , Opt.help $
141 concat
142 [ "The Elm version of the source files being formatted. "
143 , "Valid values: "
144 , show ElmVersion.Elm_0_18 ++ ", "
145 , show ElmVersion.Elm_0_19 ++ ". "
146 , "Default: auto"
147 ]
148 ]
149
150
151 json :: Opt.Parser (Maybe JsonMode)
152 json =
153 (Opt.flag Nothing (Just ElmToJson) $
154 mconcat
155 [ Opt.long "json"
156 , Opt.help "Instead of formatting, write the AST to stdout."
157 , Opt.internal
158 ]
159 )
160 <|>
161 (Opt.flag' (Just JsonToElm) $
162 mconcat
163 [ Opt.long "from-json"
164 , Opt.help "Read the JSON AST input from stdout."
165 , Opt.internal
166 ]
167 )