Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
3987: Parse required CLI params in cluster r=parsonsmatt a=parsonsmatt

## Description

<!--- A brief description of this PR and the problem is trying to solve -->

## Linked issue

<!--- Put here the relevant issue from YouTrack -->

cardano-foundation/cardano-wallet#160



Co-authored-by: Moritz Angermann <[email protected]>
Co-authored-by: parsonsmatt <[email protected]>
Co-authored-by: KtorZ <[email protected]>
  • Loading branch information
4 people committed Dec 27, 2018
2 parents e4324d5 + 4fe4cb8 commit b8cae35
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions cluster/src/Cardano/Cluster/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Universum hiding (takeWhile)
import Control.Concurrent.Async (Async, async, race, wait)
import Control.Lens (at)
import qualified Data.Aeson as Aeson
import Data.Attoparsec.ByteString.Char8 (IResult (..), parse,
import Data.Attoparsec.ByteString.Char8 (parseOnly, skipSpace,
skipWhile, string, takeWhile)
import qualified Data.Attoparsec.Internal.Types as Atto.Internal
import qualified Data.ByteString.Char8 as B8
Expand Down Expand Up @@ -286,7 +286,7 @@ varFromParser
:: Parser a -- Target parser
-> [(String, ArgType)]
varFromParser parser =
foldParse [] (helpToByteString help)
foldParse (helpToByteString help)
where
-- Here is the little trick, we leverage the parserFailure which displays
-- a usage with all possible arguments and flags and from this usage,
Expand All @@ -308,15 +308,27 @@ varFromParser parser =
Nothing -> (kToS (drop 2 arg), Flag)
Just i -> (kToS (drop 2 (take i arg)), Arg)

foldParse :: [(String, ArgType)] -> ByteString -> [(String, ArgType)]
foldParse xs str = case parse (argToVar . B8.unpack <$> capture) str of
Fail{} -> xs
Partial{} -> xs
Done rest x -> foldParse (x : xs) rest
foldParse :: ByteString -> [(String, ArgType)]
foldParse =
rights
. fmap (fmap (argToVar . B8.unpack) . parseOnly capture)
. B8.lines

capture :: Atto.Internal.Parser ByteString ByteString
capture =
skipWhile (/= '[') *> string "[" *> takeWhile (/= ']') <* string "]"
(skipWhile (/= '[') *> string "[" *> takeWhile (/= ']') <* string "]")
<|>
(do
skipSpace
_ <- string "--"
str <- takeWhile (not . Char.isSpace)
marg <- fmap (fromMaybe "") . optional $ do
_ <- string " "
str2 <- takeWhile (not . Char.isSpace)
pure (" " <> str2)
pure ("--" <> str <> marg)
)



-- | Run a parser from environment variables rather than command-line arguments
Expand Down

0 comments on commit b8cae35

Please sign in to comment.