Find git executable at run time
[git-darcs-import.git] / src / Darcs / Commands / Help.lhs
blob086581ec90499aaca59bd79fbac535db6be792a2
1 % Copyright (C) 2002-2004 David Roundy
3 % This program is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2, or (at your option)
6 % any later version.
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
13 % You should have received a copy of the GNU General Public License
14 % along with this program; see the file COPYING. If not, write to
15 % the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 % Boston, MA 02110-1301, USA.
18 \subsection{darcs help}
19 \label{help}
21 You could also call \verb|help| as a command. This is equivalent to calling
22 darcs --help.
23 the \verb|--help| to that command. For example, \verb|darcs help query manifest|
24 is equivalent to \verb|darcs query manifest --help|.
26 \begin{code}
27 module Darcs.Commands.Help ( help_cmd, command_control_list, print_version,
28 list_available_commands
29 ) where
30 import System.Exit ( ExitCode(..), exitWith )
32 import Autoconf( darcs_version )
33 import Darcs.Commands ( CommandControl(Command_data), DarcsCommand(..),
34 disambiguate_commands, CommandArgs(..),
35 get_command_help, extract_commands,
36 nodefaults,
37 usage )
38 import Darcs.Arguments ( DarcsFlag(..), help_on_match )
39 import Darcs.External ( viewDoc )
40 import Darcs.Patch.Match ( helpOnMatchers )
41 import Darcs.Utils ( withCurrentDirectory )
42 import Printer ( text )
43 import Workaround ( getCurrentDirectory )
44 import qualified Darcs.TheCommands as TheCommands
45 \end{code}
47 \options{help}
49 \haskell{help_description}
50 \begin{code}
51 help_description :: String
52 help_description = "Display help for darcs or a single command."
53 \end{code}
54 \haskell{help_help}
56 \begin{code}
57 help_help :: String
58 help_help =
59 "help displays usage information for darcs in general or for a single\n" ++
60 "command (for example, darcs help query manifest).\n\n"
61 \end{code}
63 \begin{code}
64 help :: DarcsCommand
65 help = DarcsCommand {command_name = "help",
66 command_help = help_help,
67 command_description = help_description,
68 command_extra_args = -1,
69 command_extra_arg_help = ["[<DARCS_COMMAND> [DARCS_SUBCOMMAND]] "],
70 command_command = help_cmd,
71 command_prereq = \_ -> return $ Right (),
72 command_get_arg_possibilities = return [],
73 command_argdefaults = nodefaults,
74 command_advanced_options = [],
75 command_basic_options = [help_on_match]}
76 \end{code}
78 \begin{code}
79 help_cmd :: [DarcsFlag] -> [String] -> IO ()
80 help_cmd opts [] =
81 do viewDoc $ text $
82 case () of _ | HelpOnMatch `elem` opts -> helpOnMatchers
83 | otherwise -> usage command_control_list
84 exitWith $ ExitSuccess
86 help_cmd _ (cmd:args) =
87 do let disambiguated = disambiguate_commands command_control_list cmd args
88 case disambiguated of
89 Left err -> fail err
90 Right (cmds,_) ->
91 let msg = case cmds of
92 CommandOnly c -> get_command_help Nothing c
93 SuperCommandOnly c -> get_command_help Nothing c
94 SuperCommandSub c s -> get_command_help (Just c) s
95 in viewDoc $ text msg
96 exitWith $ ExitSuccess
98 list_available_commands :: IO ()
99 list_available_commands =
100 do here <- getCurrentDirectory
101 is_valid <- sequence $ map
102 (\c-> withCurrentDirectory here $ (command_prereq c) [])
103 (extract_commands command_control_list)
104 putStr $ unlines $ map (command_name . fst) $
105 filter (isRight.snd) $
106 zip (extract_commands command_control_list) is_valid
107 putStrLn "--help"
108 putStrLn "--version"
109 putStrLn "--exact-version"
110 putStrLn "--overview"
111 where isRight (Right _) = True
112 isRight _ = False
113 \end{code}
115 \begin{code}
116 print_version :: IO ()
117 print_version = putStrLn $ "darcs version " ++ darcs_version
118 \end{code}
120 \begin{code}
121 -- avoiding a module import cycle between Help and TheCommands
122 command_control_list :: [CommandControl]
123 command_control_list =
124 Command_data help : TheCommands.command_control_list
125 \end{code}