Skip to content

Commit

Permalink
Refactor argument handling
Browse files Browse the repository at this point in the history
Move input-independent arguments to a top-level product type rather
than duplicating them in `FileArgs` and `StdinArgs`. Without this,
specifying `--verbose` before `-i` would lock-in `StdinInput` alternative.
The arguments would also be duplicated in the help page.
  • Loading branch information
jtojnar committed May 19, 2024
1 parent 3176945 commit 664d51e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ Type `--help` for some more information.
```shell
dconf2nix - Nixify dconf configuration files

Usage: dconf2nix [-v|--version]
[[-r|--root ARG] [--verbose] | (-i|--input ARG)
(-o|--output ARG) [-r|--root ARG] [--verbose]]
Usage: dconf2nix [-v|--version] [-r|--root ARG] [--verbose]
[(-i|--input ARG) (-o|--output ARG)]

Convert a dconf file into a Nix file, as expected by Home Manager.

Available options:
Expand All @@ -117,8 +117,6 @@ Available options:
--verbose Verbose mode (debug)
-i,--input ARG Path to the dconf file (input)
-o,--output ARG Path to the Nix output file (to be created)
-r,--root ARG Custom root path. e.g.: system/locale/
--verbose Verbose mode (debug)
```

#### Custom root
Expand Down
8 changes: 4 additions & 4 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Main where

import CommandLine ( FileArgs(..)
import CommandLine ( Args(..)
, FileArgs(..)
, Input(..)
, StdinArgs(..)
, runArgs
)
import DConf2Nix ( dconf2nixFile
Expand All @@ -13,7 +13,7 @@ import DConf2Nix ( dconf2nixFile

main :: IO ()
main = runArgs >>= \case
FileInput (FileArgs i o r v) ->
Args r v (FileInput (FileArgs i o)) ->
dconf2nixFile i o r v
StdinInput (StdinArgs r v) ->
Args r v StdinInput ->
dconf2nixStdin r v
30 changes: 14 additions & 16 deletions src/CommandLine.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CommandLine
( FileArgs(..)
( Args(..)
, FileArgs(..)
, Input(..)
, StdinArgs(..)
, runArgs
)
where
Expand All @@ -12,18 +12,17 @@ import DConf.Data
import Options.Applicative
import Paths_dconf2nix ( version )

data Input = FileInput FileArgs | StdinInput StdinArgs
data Args = Args
{ argsRoot :: Root
, argsVerbosity :: Verbosity
, argsInput :: Input
}

data Input = FileInput FileArgs | StdinInput

data FileArgs = FileArgs
{ fileInput :: InputFilePath
, fileOutput :: OutputFilePath
, fileRoot :: Root
, fileVerbosity :: Verbosity
}

data StdinArgs = StdinArgs
{ stdinRoot :: Root
, stdinVerbosity :: Verbosity
}

verbosityArgs :: Parser Verbosity
Expand All @@ -46,12 +45,9 @@ fileArgs = fmap FileInput $ FileArgs
"Path to the Nix output file (to be created)"
)
)
<*> rootArgs
<*> verbosityArgs

stdinArgs :: Parser Input
stdinArgs =
StdinInput <$> (StdinArgs <$> rootArgs <*> verbosityArgs)
stdinArgs = pure StdinInput

versionInfo :: String
versionInfo = unlines
Expand All @@ -72,11 +68,13 @@ versionOpt :: Parser (a -> a)
versionOpt = infoOption versionInfo
(long "version" <> short 'v' <> help "Show the current version")

runArgs :: IO Input
runArgs :: IO Args
runArgs =
let
args = Args <$> rootArgs <*> verbosityArgs <*> (stdinArgs <|> fileArgs)

opts = info
(helper <*> versionOpt <*> (stdinArgs <|> fileArgs))
(helper <*> versionOpt <*> args)
( fullDesc
<> progDesc
"Convert a dconf file into a Nix file, as expected by Home Manager."
Expand Down

0 comments on commit 664d51e

Please sign in to comment.