aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-10-08 12:51:02 +0530
committerAkshay Nair <phenax5@gmail.com>2025-10-08 13:10:49 +0530
commit4e8e81b05afa4ba56d64ba714c78ba307f936076 (patch)
tree3d481ce541ea8d5b46cb9e3debcaf1db8a112415 /lib/Daffm
parente173e1f105e72e06c6a3206b9b94f4f1da63b00f (diff)
downloaddaffm-4e8e81b05afa4ba56d64ba714c78ba307f936076.tar.gz
daffm-4e8e81b05afa4ba56d64ba714c78ba307f936076.zip
Refactor args parsing to module + docs update
Diffstat (limited to 'lib/Daffm')
-rw-r--r--lib/Daffm/Args.hs44
-rw-r--r--lib/Daffm/Types.hs7
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/Daffm/Args.hs b/lib/Daffm/Args.hs
new file mode 100644
index 0000000..aeaead7
--- /dev/null
+++ b/lib/Daffm/Args.hs
@@ -0,0 +1,44 @@
+module Daffm.Args where
+
+import Control.Exception (throwIO)
+import Daffm.Types
+import qualified Data.Text as Text
+
+parseArgs :: [String] -> IO Args
+parseArgs rawArgs = case parsedArgs of
+ Left e -> throwIO $ userError e
+ Right v -> pure v
+ where
+ parsedArgs = parse rawArgs (Args {argsDirOrFile = Nothing, argsConfigFile = Nothing, argsHelp = False})
+ parse :: [String] -> Args -> Either String Args
+ parse [] args = Right args
+ parse ("-h" : _) args = Right $ args {argsHelp = True}
+ parse ("--help" : _) args = Right $ args {argsHelp = True}
+ parse ["-c"] _ = Left "Missing value for -c arg"
+ parse ("-c" : config : rest) args = parse rest $ args {argsConfigFile = Just config}
+ parse ["--config"] _ = Left "Missing value for --config arg"
+ parse ("--config" : config : rest) args = parse rest $ args {argsConfigFile = Just config}
+ parse (flag@('-' : _) : _) _ = Left $ "Invalid flag " <> flag
+ parse (dir : rest) args = parse rest $ args {argsDirOrFile = Just $ Text.pack dir}
+
+helpMenuContents :: String
+helpMenuContents =
+ unlines
+ [ "daffm - Dumb as-fuck file manager",
+ "",
+ "Usage: daffm [options] [dir]",
+ "",
+ "Arguments:",
+ " [dir]",
+ " Directory or file path to load. Defaults to current working directory",
+ "",
+ "Options:",
+ " -c, --config <CONFIG-PATH>",
+ " Load toml config from file",
+ " If path is prefixed with @, will use alternate config",
+ " Ex: -c @foo will load $XDG_CONFIG_HOME/daffm/config.foo.toml",
+ " Default: $XDG_CONFIG_HOME/daffm/config.toml",
+ "",
+ " -h, --help",
+ " This help menu"
+ ]
diff --git a/lib/Daffm/Types.hs b/lib/Daffm/Types.hs
index b916c79..3fd7086 100644
--- a/lib/Daffm/Types.hs
+++ b/lib/Daffm/Types.hs
@@ -103,6 +103,13 @@ instance Semigroup Configuration where
configTheme = configTheme a <> configTheme b
}
+data Args = Args
+ { argsDirOrFile :: Maybe Text.Text,
+ argsConfigFile :: Maybe FilePath,
+ argsHelp :: Bool
+ }
+ deriving (Show)
+
defaultConfiguration :: Configuration
defaultConfiguration =
Configuration