% Copyright (C) 2002-2004 David Roundy
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2, or (at your option)
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; see the file COPYING. If not, write to
% the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
% Boston, MA 02110-1301, USA.
\darcsCommand{help}
\begin{code}
module Darcs.Commands.Help (
helpCmd,
commandControlList, environmentHelp,
printVersion,
listAvailableCommands ) where
import Darcs.Arguments ( DarcsFlag(..), environmentHelpEmail, environmentHelpSendmail )
import Darcs.Commands (
CommandArgs(..), CommandControl(..), DarcsCommand(..),
disambiguateCommands, extractCommands, getCommandHelp, nodefaults, usage )
import Darcs.External ( viewDoc )
import Darcs.Lock ( environmentHelpTmpdir, environmentHelpKeepTmpdir )
import Darcs.Patch.Match ( helpOnMatchers )
import Darcs.Repository.Prefs ( binariesFileHelp, environmentHelpHome )
import Darcs.Utils ( withCurrentDirectory, environmentHelpEditor, environmentHelpPager )
import Data.Char ( isAlphaNum, toLower )
import Data.List ( groupBy )
import English ( andClauses )
import Printer ( text )
import Ssh ( environmentHelpSsh, environmentHelpScp, environmentHelpSshPort )
import System.Exit ( ExitCode(..), exitWith )
import Version ( version )
import URL (environmentHelpProxy, environmentHelpProxyPassword)
import Workaround ( getCurrentDirectory )
import qualified Darcs.TheCommands as TheCommands
helpDescription :: String
helpDescription = "Display help about darcs and darcs commands."
helpHelp :: String
helpHelp =
"Without arguments, `darcs help' prints a categorized list of darcs\n" ++
"commands and a short description of each one. With an extra argument,\n" ++
"`darcs help foo' prints detailed help about the darcs command foo.\n"
help :: DarcsCommand
help = DarcsCommand {commandName = "help",
commandHelp = helpHelp,
commandDescription = helpDescription,
commandExtraArgs = 1,
commandExtraArgHelp = ["[<DARCS_COMMAND> [DARCS_SUBCOMMAND]] "],
commandCommand = \ x y -> helpCmd x y >> exitWith ExitSuccess,
commandPrereq = \_ -> return $ Right (),
commandGetArgPossibilities = return [],
commandArgdefaults = nodefaults,
commandAdvancedOptions = [],
commandBasicOptions = []}
helpCmd :: [DarcsFlag] -> [String] -> IO ()
helpCmd _ ["manpage"] = putStr $ unlines manpageLines
helpCmd _ ["patterns"] = viewDoc $ text $ helpOnMatchers
helpCmd _ ["environment"] = viewDoc $ text $ helpOnEnvironment
helpCmd _ [] = viewDoc $ text $ usage commandControlList
helpCmd _ (cmd:args) =
let disambiguated = disambiguateCommands commandControlList cmd args
in case disambiguated of
Left err -> fail err
Right (cmds,_) ->
let msg = case cmds of
CommandOnly c -> getCommandHelp Nothing c
SuperCommandOnly c -> getCommandHelp Nothing c
SuperCommandSub c s -> getCommandHelp (Just c) s
in viewDoc $ text msg
listAvailableCommands :: IO ()
listAvailableCommands =
do here <- getCurrentDirectory
is_valid <- mapM
(\c-> withCurrentDirectory here $ (commandPrereq c) [])
(extractCommands commandControlList)
putStr $ unlines $ map (commandName . fst) $
filter (isRight.snd) $
zip (extractCommands commandControlList) is_valid
putStrLn "--help"
putStrLn "--version"
putStrLn "--exact-version"
putStrLn "--overview"
where isRight (Right _) = True
isRight _ = False
printVersion :: IO ()
printVersion = putStrLn $ "darcs version " ++ version
commandControlList :: [CommandControl]
commandControlList =
CommandData help : TheCommands.commandControlList
environmentHelp :: [([String], [String])]
environmentHelp = [
environmentHelpHome,
environmentHelpEditor,
environmentHelpPager,
environmentHelpTmpdir,
environmentHelpKeepTmpdir,
environmentHelpEmail,
environmentHelpSendmail,
environmentHelpSsh,
environmentHelpScp,
environmentHelpSshPort,
environmentHelpProxy,
environmentHelpProxyPassword]
helpOnEnvironment :: String
helpOnEnvironment =
"Environment Variables\n" ++
"=====================\n\n" ++
unlines [andClauses ks ++ ":\n" ++
(unlines $ map (" " ++) ds)
| (ks, ds) <- environmentHelp]
manpageLines :: [String]
manpageLines = [
".TH DARCS 1 \"" ++ version ++ "\"",
".SH NAME",
"darcs \\- an advanced revision control system",
".SH SYNOPSIS",
".B darcs", ".I command", ".RI < arguments |[ options ]>...",
"",
"Where the", ".I commands", "and their respective", ".I arguments", "are",
"",
unlines synopsis,
".SH DESCRIPTION",
"Darcs is a free, open source revision control",
"system. It is:",
".TP 3", "\\(bu",
"Distributed: Every user has access to the full",
"command set, removing boundaries between server and",
"client or committer and non\\(hycommitters.",
".TP", "\\(bu",
"Interactive: Darcs is easy to learn and efficient to",
"use because it asks you questions in response to",
"simple commands, giving you choices in your work",
"flow. You can choose to record one change in a file,",
"while ignoring another. As you update from upstream,",
"you can review each patch name, even the full `diff'",
"for interesting patches.",
".TP", "\\(bu",
"Smart: Originally developed by physicist David",
"Roundy, darcs is based on a unique algebra of",
"patches.",
"This smartness lets you respond to changing demands",
"in ways that would otherwise not be possible. Learn",
"more about spontaneous branches with darcs.",
".SH OPTIONS",
"Different options are accepted by different Darcs commands.",
"Each command's most important options are listed in the",
".B COMMANDS",
"section. For a full list of all options accepted by",
"a particular command, run `darcs", ".I command", "\\-\\-help'.",
".SS " ++ escape helpOnMatchers,
".SH COMMANDS",
unlines commands,
unlines environment,
".SH FILES",
".SS \"_darcs/prefs/binaries\"",
escape $ unlines binariesFileHelp,
".SH BUGS",
"At http://bugs.darcs.net/ you can find a list of known",
"bugs in Darcs. Unknown bugs can be reported at that",
"site (after creating an account) or by emailing the",
"report to bugs@darcs.net.",
".SH SEE ALSO",
"A user manual is included with Darcs, in PDF and HTML",
"form. It can also be found at http://darcs.net/manual/."]
where
synopsis :: [String]
synopsis = foldl iter [] commandControlList
where iter :: [String] -> CommandControl -> [String]
iter acc (GroupName _) = acc
iter acc (HiddenCommand _) = acc
iter acc (CommandData c@SuperCommand {}) =
acc ++ concatMap
(render (commandName c ++ " "))
(extractCommands (commandSubCommands c))
iter acc (CommandData c) = acc ++ render "" c
render :: String -> DarcsCommand -> [String]
render prefix c =
[".B darcs " ++ prefix ++ commandName c] ++
(map mangle_args $ commandExtraArgHelp c) ++
[".br"]
commands :: [String]
commands = foldl iter [] commandControlList
where iter :: [String] -> CommandControl -> [String]
iter acc (GroupName x) = acc ++ [".SS \"" ++ x ++ "\""]
iter acc (HiddenCommand _) = acc
iter acc (CommandData c@SuperCommand {}) =
acc ++ concatMap
(render (commandName c ++ " "))
(extractCommands (commandSubCommands c))
iter acc (CommandData c) = acc ++ render "" c
render :: String -> DarcsCommand -> [String]
render prefix c =
[".B darcs " ++ prefix ++ commandName c] ++
(map mangle_args $ commandExtraArgHelp c) ++
[".RS 4", escape $ commandHelp c, ".RE"]
mangle_args :: String -> String
mangle_args s =
".RI " ++ (unwords $ map show (groupBy cmp $ map toLower $ gank s))
where cmp x y = not $ xor (isAlphaNum x) (isAlphaNum y)
xor x y = (x && not y) || (y && not x)
gank (' ':'o':'r':' ':xs) = '|' : gank xs
gank (x:xs) = x : gank xs
gank [] = []
environment :: [String]
environment = ".SH ENVIRONMENT" : concat
[(".SS \"" ++ andClauses ks ++ "\"") : map escape ds
| (ks, ds) <- environmentHelp]
escape :: String -> String
escape = minus . bs
where
minus = replace "-" "\\-"
bs = replace "\\" "\\\\"
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace _ _ [] = []
replace find repl s =
if take (length find) s == find
then repl ++ (replace find repl (drop (length find) s))
else [head s] ++ replace find repl (tail s)
\end{code}