browse: shell-escape %s; add %S for unescaped
[diohsc.git] / Opts.hs
blob21a2cc3de26609b6eedefd162c40a69333e980c6
1 -- This file is part of Diohsc
2 -- Copyright (C) 2020 Martin Bays <mbays@sdf.org>
3 --
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
7 --
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
11 {-# LANGUAGE Safe #-}
13 module Opts (usage, parseArgs, Opt(..)) where
15 import System.Console.GetOpt
17 import Version
19 data Opt
20 = Restricted
21 | Interactive
22 | Batch
23 | Prompt
24 | ScriptFile FilePath
25 | OptCommand String
26 | DataDir FilePath
27 | SocksHost String
28 | SocksPort String
29 | Ghost
30 | Ansi
31 | NoAnsi
32 | Help
33 | Version
34 deriving (Eq, Ord, Show)
36 options :: [OptDescr Opt]
37 options =
38 [ Option ['d'] ["datadir"] (ReqArg DataDir "PATH") "data and config directory (default: ~/.diohsc)"
39 , Option ['e'] ["command"] (ReqArg OptCommand "COMMAND") "execute command"
40 , Option ['f'] ["file"] (ReqArg ScriptFile "PATH") "execute commands from file (\"-\" for stdin)"
41 , Option ['p'] ["prompt"] (NoArg Prompt) "prompt for further commands after -e/-f"
42 , Option ['i'] ["interact"] (NoArg Interactive) "interactive mode (assumed unless -e/-f used)"
43 , Option ['b'] ["batch"] (NoArg Batch) "non-interactive mode (assumed if -e/-f used)"
44 , Option ['c'] ["colour"] (NoArg Ansi) "use ANSI colour (assumed if stdout is term)"
45 , Option ['C'] ["no-colour"] (NoArg NoAnsi) "do not use ANSI colour"
46 , Option ['g'] ["ghost"] (NoArg Ghost) "write nothing to filesystem (unless commanded)"
47 , Option ['r'] ["restricted"] (NoArg Restricted) "disallow shell and filesystem access"
48 , Option ['S'] ["socks-host"] (ReqArg SocksHost "HOST") "use SOCKS5 proxy"
49 , Option ['P'] ["socks-port"] (ReqArg SocksPort "PORT") "port for SOCKS5 proxy (default 1080)"
50 , Option ['v'] ["version"] (NoArg Version) "show version information"
51 , Option ['h'] ["help"] (NoArg Help) "show usage information"
54 usage :: String
55 usage = usageInfo header options
56 where header = "Usage: " ++ programName ++ " [OPTION...] [URI|PATH]"
58 parseArgs :: [String] -> IO ([Opt],[String])
59 parseArgs argv =
60 case getOpt Permute options argv of
61 (o,n,[]) -> return (o,n)
62 (_,_,errs) -> ioError (userError (concat errs ++ usage))