Skip to content

Commit

Permalink
Add --no-newline flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Aug 17, 2024
1 parent d3217fa commit 2584ddd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ip6addr v2.0.0 (c) Michel Boucey 2011-2024
Usage: ip6addr [-v|--version]
[(-c|--canonical) | (-n|--no-ipv4) | (-f|--full-length) |
(-p|--ptr) | (-w|--windows-unc) | (-r|--random)]
[-q|--quantity ARG] [-x|--prefix ARG] [<IPv6 address>]
[-q|--quantity ARG] [-x|--prefix ARG] [-d|--no-newline]
[IPv6 address]
ip6addr
Expand All @@ -34,6 +35,7 @@ Available options:
-r,--random Random generation
-q,--quantity ARG Amount of random addresses to generate
-x,--prefix ARG Set a prefix for random addresses to generate
-d,--no-newline Do not output trailing newlines
-h,--help Show this help text
```

Expand Down
41 changes: 24 additions & 17 deletions app/ip6addr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Exception
import Control.Monad (replicateM_)
import Data.Maybe
import qualified Data.Text as T (pack)
import qualified Data.Text.IO as TIO (hPutStrLn, putStrLn)
import qualified Data.Text.IO as TIO (hPutStrLn, putStr, putStrLn)
import Data.Version (showVersion)
import Options.Applicative
import Paths_ip6addr (version)
Expand All @@ -21,31 +21,32 @@ main = do
then putStrLn showVer >> exitFailure
else
case output of
Canonical -> out maybeIPv6Addr address unIPv6Addr
NoIPv4 -> out maybePureIPv6Addr address unIPv6Addr
FullLength -> out maybeFullIPv6Addr address unIPv6Addr
PTR -> out maybeIP6ARPA address id
UNC -> out maybeUNC address id
Random -> replicateM_ quantity (putRandAddr prefix) >> exitSuccess
Canonical -> out noNewline maybeIPv6Addr address unIPv6Addr
NoIPv4 -> out noNewline maybePureIPv6Addr address unIPv6Addr
FullLength -> out noNewline maybeFullIPv6Addr address unIPv6Addr
PTR -> out noNewline maybeIP6ARPA address id
UNC -> out noNewline maybeUNC address id
Random -> replicateM_ quantity (putRandAddr noNewline prefix) >> exitSuccess
where
putRandAddr p = do
putRandAddr n p = do
let p' = T.pack p
t <- try $ randIPv6AddrWithPrefix (if p == mempty then Nothing else Just p')
case t of
Right a -> TIO.putStrLn (unIPv6Addr $ fromJust a)
Right a -> put n (unIPv6Addr $ fromJust a)
Left (_ :: SomeException) ->
TIO.putStrLn ("'" <> p' <> "' is an invalid prefix") >> exitFailure
out t i o =
out n t i o =
if i /= mempty
then do
let p = T.pack i
case t p of
Nothing ->
TIO.hPutStrLn stderr ("'" <> p <> "' is not an IPv6 address") >> exitFailure
Just a -> TIO.putStrLn (o a) >> exitSuccess
Just a -> put n (o a) >> exitSuccess
else Prelude.putStrLn "See help" >> exitFailure
maybeUNC t = toUNC <$> maybePureIPv6Addr t
maybeIP6ARPA t = toIP6ARPA <$> maybeFullIPv6Addr t
put n = if n then TIO.putStr else TIO.putStrLn

showVer :: String
showVer = "ip6addr v" <> showVersion version <> " (c) Michel Boucey 2011-2024"
Expand All @@ -61,11 +62,12 @@ data Output

data Options =
Options
{ showver :: !Bool
, output :: !Output
, quantity :: !Int
, prefix :: !String
, address :: !String
{ showver :: !Bool
, output :: !Output
, quantity :: !Int
, prefix :: !String
, noNewline :: !Bool
, address :: !String
}

opts :: ParserInfo Options
Expand Down Expand Up @@ -130,11 +132,16 @@ parseOptions =
)
<*>
option str
( short 'x'
( short 's'
<> long "prefix"
<> help "Set a prefix for random addresses to generate"
<> value ""
)
<*>
flag False True
( short 'd'
<> long "no-newline"
<> help "Do not output trailing newlines" )
<*>
argument str (metavar "IPv6 address" <> value "")

0 comments on commit 2584ddd

Please sign in to comment.