11{-# LANGUAGE ScopedTypeVariables #-}
22{-# LANGUAGE RecordWildCards #-}
3+ {-# LANGUAGE BangPatterns #-}
34
45module Main
56 ( main
@@ -8,6 +9,7 @@ module Main
89import Control.Monad
910import Data.List (intercalate )
1011import Data.Semigroup ((<>) )
12+ import qualified Data.Set as Set
1113import Data.Version (showVersion )
1214import GHC.IO.Handle.FD (stdout )
1315import Options.Applicative
@@ -39,7 +41,10 @@ programOptions :: Parser TldrOpts
3941programOptions = (TldrOpts <$> (updateIndexCommand <|> viewPageCommand))
4042
4143updateIndexCommand :: Parser TldrCommand
42- updateIndexCommand = flag' UpdateIndex (long " update" <> short ' u' )
44+ updateIndexCommand =
45+ flag'
46+ UpdateIndex
47+ (long " update" <> short ' u' <> help " Update offline cache of tldr pages" )
4348
4449viewOptionsParser :: Parser ViewOptions
4550viewOptionsParser = ViewOptions <$> platformFlag
@@ -51,7 +56,15 @@ viewPageCommand =
5156
5257platformFlag :: Parser (Maybe String )
5358platformFlag =
54- optional (strOption (long " platform" <> short ' p' <> metavar " PLATFORM" ))
59+ optional
60+ (strOption
61+ (long " platform" <> short ' p' <> metavar " PLATFORM" <>
62+ help
63+ (" Prioritize specfic platform while searching. Valid values include " <>
64+ platformHelpValue)))
65+ where
66+ platformHelpValue :: String
67+ platformHelpValue = intercalate " , " platformDirs
5568
5669tldrDirName :: String
5770tldrDirName = " tldr"
@@ -60,7 +73,10 @@ repoHttpsUrl :: String
6073repoHttpsUrl = " https://github.com/tldr-pages/tldr.git"
6174
6275checkDirs :: [String ]
63- checkDirs = [" common" , " linux" , " osx" , " windows" , " sunos" ]
76+ checkDirs = " common" : platformDirs
77+
78+ platformDirs :: [String ]
79+ platformDirs = [" linux" , " osx" , " windows" , " sunos" ]
6480
6581tldrInitialized :: IO Bool
6682tldrInitialized = do
@@ -120,7 +136,16 @@ getCheckDirs :: ViewOptions -> [String]
120136getCheckDirs voptions =
121137 case platformOption voptions of
122138 Nothing -> checkDirs
123- Just platform -> [" common" , platform]
139+ Just platform -> nubOrd $ [" common" , platform] <> checkDirs
140+
141+ -- | Strip out duplicates
142+ nubOrd :: Ord a => [a ] -> [a ]
143+ nubOrd = loop mempty
144+ where
145+ loop _ [] = []
146+ loop ! s (a: as)
147+ | a `Set.member` s = loop s as
148+ | otherwise = a : loop (Set. insert a s) as
124149
125150handleTldrOpts :: TldrOpts -> IO ()
126151handleTldrOpts TldrOpts {.. } = do
0 commit comments