Skip to content

Commit

Permalink
Utils: add support for ~better~ argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
np committed Jan 16, 2015
1 parent efe3e74 commit 9a62219
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ strictReadDigits msg s
readDigits :: Read a => String -> BS -> a
readDigits msg = strictReadDigits msg . ignoreSpaces

get_arg :: String -> String -> [BS] -> (BS, [BS])
get_arg n u [] = error . unwords $ ["Missing", n ++ ".\nhx", n, u]
get_arg _ _ (arg:args) = (arg, args)

get_hex_arg :: String -> String -> [BS] -> (BS, [BS])
get_hex_arg n u [] = error . unwords $ ["Missing", n ++ ".\nUsage:", u]
get_hex_arg n u ["--hex"] = error . unwords $ ["Missing", n ++ "in hexadecimal.\nUsage:", u]
get_hex_arg n _ ("--hex":hex_arg:args) = (decodeHex n hex_arg, args)
get_hex_arg _ _ (arg:args) = (arg, args)

get_int_arg :: String -> String -> [BS] -> (Int, [BS])
get_int_arg n u [] = error . unwords $ ["Missing", n ++ ".\nUsage:", u]
get_int_arg n u (arg:args)
| B8.all isDigit arg = (readBS arg, args)
| otherwise = error . unwords $ ["Non-decimal digits for", n ++ ".\nUsage:", u]

no_args :: String -> [BS] -> a -> a
no_args u [] x = x
no_args u _ _ = error $ "Too many arguments.\nUsage: " ++ u

parseInt :: String -> BS -> Int
parseWord8 :: String -> BS -> Word8
parseWord32 :: String -> BS -> Word32
Expand Down

0 comments on commit 9a62219

Please sign in to comment.