11{-# LANGUAGE TemplateHaskell #-}
2- module Options (Options (.. ), OutputForm (.. ), Package (.. ), Packages (Packages ), parseCommandLine ) where
2+ module Options (Options (.. ), OutputForm (.. ), Package (), Packages (Packages ), parseCommandLine , setPackages ) where
33
4- import Constants
5- import FlakeTemplate
6- import ShellifyTemplate
4+ import Constants (hlDesc , noPackagesError )
75
86import Control.Lens.Combinators (makeLenses , makePrisms )
97import Data.Default (Default (def ))
10- import Data.List (isPrefixOf , sort )
11- import Data.Maybe (fromMaybe , isJust )
12- import Data.Text (pack , Text (), unpack )
8+ import Data.List (isPrefixOf )
9+ import Data.Maybe (fromMaybe )
10+ import Data.Set (Set (), fromList )
11+ import Data.Text (Text ())
1312import Data.Version (showVersion )
1413import Options.Applicative ((<**>) , Parser , ParserResult (Success , Failure , CompletionInvoked ), argument , command , defaultPrefs , execParserPure , fullDesc , header , help , helper , hidden , info , long , metavar , option , optional , progDesc , short , simpleVersioner , some , str , strOption , switch )
1514import Paths_shellify (version )
16- import System.Environment (getArgs )
1715
1816data OutputForm = Traditional
1917 | Flake
2018 deriving (Eq , Show )
2119
22- newtype Packages = Packages [ Package ] deriving Show
20+ newtype Packages = Packages (Set Package )
21+ deriving (Eq , Monoid , Semigroup , Show )
2322
2423type Package = Text
2524
26- instance Eq Packages where
27- Packages a == Packages b = sort a == sort b
28-
2925makePrisms ''Packages
26+ setPackages = Packages . fromList
3027
3128opts = info (commandParser <**> simpleVersioner (showVersion version)
3229 <**> helper) $
@@ -83,8 +80,8 @@ data CommandLineOptions = CommandLineOptions {
8380makeLenses ''Options
8481
8582instance Default Options where
86- def = Options {
87- _packages = Packages [] ,
83+ def = Options {
84+ _packages = mempty ,
8885 _command = Nothing ,
8986 _outputForm = Traditional ,
9087 _prioritiseLocalPinnedSystem = False
@@ -99,21 +96,20 @@ parseCommandLine =
9996 . execParserPure defaultPrefs opts . fixupRequest
10097 where parseCommandLineOptions :: CommandLineOptions -> Either Text Options
10198 parseCommandLineOptions originalParsedOptions =
102- let transformedOptions =
103- (Options <$> Packages . ((++) <$> fromMaybe [] . __packages
104- <*> shellArgs . __shellPackages)
105- <*> __command
106- <*> \ case
107- f | __withFlake f -> Flake
108- | (hasShellArg . __shellPackages) f -> Flake
109- _ | otherwise -> Traditional
110- <*> __prioritiseLocalPinnedSystem) originalParsedOptions
111-
112- in if _packages transformedOptions == Packages [] then
113- Left noPackagesError
114- else
115- Right transformedOptions
116- where hasShellArg (Just (" shell" : _)) = True
99+ if _packages transformedOptions == mempty then
100+ Left noPackagesError
101+ else
102+ Right transformedOptions
103+ where transformedOptions =
104+ (Options <$> setPackages . ((++) <$> fromMaybe mempty . __packages
105+ <*> shellArgs . __shellPackages)
106+ <*> __command
107+ <*> \ case
108+ f | __withFlake f -> Flake
109+ | (hasShellArg . __shellPackages) f -> Flake
110+ _ | otherwise -> Traditional
111+ <*> __prioritiseLocalPinnedSystem) originalParsedOptions
112+ hasShellArg (Just (" shell" : _)) = True
117113 hasShellArg _ = False
118114 shellArgs (Just (" shell" : rst)) = rst
119115 shellArgs _ = []
0 commit comments