Skip to content

Commit

Permalink
MIN+DOC Document argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
luispedro committed Jun 3, 2021
1 parent e05f00f commit 8fa9b00
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions NGLess/CmdArgs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,30 @@ parseColor = optional $ option (eitherReader readColor) (long "color" <> help co
readColor _ = Left "Could not parse color option (valid options are 'auto', 'force', and 'no')"
colorHelp = "Color settings, one of 'auto' (color if writing to a terminal, this is the default), 'force' (always color), 'no' (no color)."

-- The input can be either inline (using -e ... or --script "...") or given as
-- a filepath
parseInput :: Parser NGLessInput
parseInput = InlineScript <$> strOption
(long "script"
<> short 'e'
<> help "inline script to execute")
<|> ScriptFilePath <$> strArgument (metavar "INPUT" <> help "Filename of script to interpret")

-- A integer literal or the string "auto"
parseNThreads = option (eitherReader readNThreads) (long "jobs" <> short 'j' <> long "threads" <> value (NThreads 1) <> help "Nr of threads to use")
where
readNThreads "auto" = Right NThreadsAuto
readNThreads val = case readMaybe val of
Just n -> Right (NThreads n)
Nothing -> Left ("Failed to parse "++val++" as a threads option")

-- An option with 3 states
-- "" -> Nothing
-- "--option" -> Just True
-- "--no-option" -> Just False
--
-- This builds on the `switch` function because it distinguishes between not
-- passing in the option.
triSwitch :: String -> String -> Parser (Maybe Bool)
triSwitch name helpmsg = optional (flag' True (long name <> help helpmsg)
<|> flag' False (long ("no-"++name) <> help ("opposite of --"++name)))
Expand Down

0 comments on commit 8fa9b00

Please sign in to comment.