summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2023-05-28 00:36:18 +0100
committerYann Herklotz <git@yannherklotz.com>2023-05-28 00:36:18 +0100
commitc133c63399701f9411bc7389d21e53e1c2bff55e (patch)
tree30c0733e6fbcb961ce103f5575e56db5f66c4d93
parentbd4816d61456b5f79def8d8e33fb485b518852cf (diff)
downloadzk-visual-c133c63399701f9411bc7389d21e53e1c2bff55e.tar.gz
zk-visual-c133c63399701f9411bc7389d21e53e1c2bff55e.zip
Add more explicit option parsing
-rw-r--r--org-zk.cabal1
-rw-r--r--src/Main.hs124
2 files changed, 88 insertions, 37 deletions
diff --git a/org-zk.cabal b/org-zk.cabal
index a542c7a..bca2ac2 100644
--- a/org-zk.cabal
+++ b/org-zk.cabal
@@ -35,7 +35,6 @@ executable org-zk
, with-utf8
, directory
, filepath
- , cmdargs
mixins:
base hiding (Prelude),
diff --git a/src/Main.hs b/src/Main.hs
index 99d39a2..506a8c6 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -1,5 +1,3 @@
-{-# LANGUAGE DeriveDataTypeable #-}
-
module Main where
-- import Data.Char (isAlphaNum, isLetter, isNumber)
@@ -21,7 +19,9 @@ module Main where
-- import qualified Data.ByteString.Lazy as B
-- import System.Directory (createDirectoryIfMissing)
-import System.Console.CmdArgs.Implicit
+import Control.Exception (ioError)
+import System.Console.GetOpt
+import System.IO.Error (userError)
import Zettel
-- data HeaderState = HeaderState
@@ -204,47 +204,99 @@ import Zettel
--
data Options = Options
- { org :: [FilePath],
- bibliography :: Maybe FilePath,
- csl :: FilePath,
- index :: Maybe FilePath,
- output :: FilePath
+ { optBibliography :: Maybe FilePath,
+ optCsl :: Maybe FilePath,
+ optIndex :: Maybe FilePath,
+ optOutput :: Maybe FilePath,
+ optVerbose :: Bool,
+ optQuiet :: Bool,
+ optShowVersion :: Bool,
+ optShowHelp :: Bool
}
- deriving (Show, Data, Typeable)
+ deriving (Show, Eq)
-options :: Options
-options =
+defaultOptions :: Options
+defaultOptions =
Options
- { org = def &= args &= typ "FILES...",
- output = def &= name "o" &= help "Output directory" &= typDir,
- bibliography =
- def
- &= name "b"
- &= help "Bibliography file path (default: references.bib)"
- &= typFile,
- csl =
- "ieee.csl"
- &= name "c"
- &= help "Csl file path (default: ieee.csl)"
- &= typFile,
- index =
- def
- &= name "i"
- &= help "Index file path"
- &= typFile
+ { optBibliography = Nothing,
+ optCsl = Nothing,
+ optIndex = Nothing,
+ optOutput = Nothing,
+ optVerbose = False,
+ optQuiet = False,
+ optShowVersion = False,
+ optShowHelp = False
}
- &= summary "org-zk v0.1.0, (C) 2023 Yann Herklotz"
- &= program "org-zk"
- &= verbosity
+
+options :: [OptDescr (Options -> Options)]
+options =
+ [ Option
+ ['b']
+ ["bibliography"]
+ (ReqArg (\f opts -> opts {optBibliography = Just f}) "FILE")
+ "Bibliography FILE (default: references.bib)",
+ Option
+ ['c']
+ ["csl"]
+ (ReqArg (\f opts -> opts {optCsl = Just f}) "FILE")
+ "Csl FILE",
+ Option
+ ['h']
+ ["help"]
+ (NoArg (\opts -> opts {optShowHelp = True}))
+ "Show this help menu",
+ Option
+ ['i']
+ ["index"]
+ (ReqArg (\f opts -> opts {optIndex = Just f}) "FILE")
+ "Index FILE (default: org-zettelkasten-index)",
+ Option
+ ['o']
+ ["output"]
+ (ReqArg (\f opts -> opts {optOutput = Just f}) "DIR")
+ "output DIR",
+ Option
+ ['q']
+ ["quiet"]
+ (NoArg (\opts -> opts {optQuiet = True}))
+ "Verbose output while processing files",
+ Option
+ ['v']
+ ["verbose"]
+ (NoArg (\opts -> opts {optVerbose = True}))
+ "Verbose output while processing files",
+ Option
+ ['V', '?']
+ ["version"]
+ (NoArg (\opts -> opts {optShowVersion = True}))
+ "Show current version"
+ ]
+
+headerOpts :: String
+headerOpts = "org-zk v0.1.0, (C) 2023 Yann Herklotz\n\nUsage: org-zk [OPTION...] files..."
+
+compilerOpts :: [String] -> IO (Options, [String])
+compilerOpts argv =
+ case getOpt Permute options argv of
+ (o, n, []) -> return (foldl' (flip id) defaultOptions o, n)
+ (_, _, errs) -> ioError (userError (concat errs ++ usageInfo headerOpts options))
main :: IO ()
main = do
- zkOpts <- cmdArgs options
- let fl = org zkOpts
- verbose <- (== Loud) <$> getVerbosity
- _quiet <- (== Quiet) <$> getVerbosity
+ (zkOpts, fl) <- getArgs >>= compilerOpts
+
+ when (optShowHelp zkOpts) $ do
+ putStrLn $ usageInfo headerOpts options
+ exitSuccess
+
+ when (optShowVersion zkOpts) $ do
+ putStrLn "org-zk v0.1.0, (C) 2023 Yann Herklotz"
+ exitSuccess
+
graph' <- parseZettelKasten $ zip [1 ..] fl
+
let graph = transcludeMdAll graph'
let linkedGraph = linkAll graph
let wrappedGraph = wrapZettelGraph linkedGraph
- renderZettelGraphFile verbose (csl zkOpts) (bibliography zkOpts) (output zkOpts) wrappedGraph
+
+ renderZettelGraphFile (optVerbose zkOpts) (fromMaybe "ieee.csl" (optCsl zkOpts)) (optBibliography zkOpts) (fromMaybe "output" (optOutput zkOpts)) wrappedGraph