1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/command_line.h"
9 #include "tools/gn/args.h"
10 #include "tools/gn/commands.h"
11 #include "tools/gn/err.h"
12 #include "tools/gn/functions.h"
13 #include "tools/gn/input_conversion.h"
14 #include "tools/gn/label_pattern.h"
15 #include "tools/gn/setup.h"
16 #include "tools/gn/standard_out.h"
17 #include "tools/gn/substitution_writer.h"
18 #include "tools/gn/switches.h"
19 #include "tools/gn/variables.h"
25 void PrintToplevelHelp() {
26 OutputString("Commands (type \"gn help <command>\" for more details):\n");
27 for (const auto& cmd
: commands::GetCommands())
28 PrintShortHelp(cmd
.second
.help_short
);
30 // Target declarations.
31 OutputString("\nTarget declarations (type \"gn help <function>\" for more "
33 for (const auto& func
: functions::GetFunctions()) {
34 if (func
.second
.is_target
)
35 PrintShortHelp(func
.second
.help_short
);
39 OutputString("\nBuildfile functions (type \"gn help <function>\" for more "
41 for (const auto& func
: functions::GetFunctions()) {
42 if (!func
.second
.is_target
)
43 PrintShortHelp(func
.second
.help_short
);
46 // Built-in variables.
47 OutputString("\nBuilt-in predefined variables (type \"gn help <variable>\" "
48 "for more details):\n");
49 for (const auto& builtin
: variables::GetBuiltinVariables())
50 PrintShortHelp(builtin
.second
.help_short
);
53 OutputString("\nVariables you set in targets (type \"gn help <variable>\" "
54 "for more details):\n");
55 for (const auto& target
: variables::GetTargetVariables())
56 PrintShortHelp(target
.second
.help_short
);
58 OutputString("\nOther help topics:\n");
59 PrintShortHelp("buildargs: How build arguments work.");
60 PrintShortHelp("dotfile: Info about the toplevel .gn file.");
61 PrintShortHelp("label_pattern: Matching more than one label.");
63 "input_conversion: Processing input from exec_script and read_file.");
64 PrintShortHelp("source_expansion: Map sources to outputs for scripts.");
65 PrintShortHelp("switches: Show available command-line switches.");
68 void PrintSwitchHelp() {
69 OutputString("Available global switches\n", DECORATION_YELLOW
);
71 " Do \"gn help --the_switch_you_want_help_on\" for more. Individual\n"
72 " commands may take command-specific switches not listed here. See the\n"
73 " help on your specific command for more.\n\n");
75 for (const auto& s
: switches::GetSwitches())
76 PrintShortHelp(s
.second
.short_help
);
79 // Prints help on the given switch. There should be no leading hyphens. Returns
80 // true if the switch was found and help was printed. False means the switch is
82 bool PrintHelpOnSwitch(const std::string
& what
) {
83 const switches::SwitchInfoMap
& all
= switches::GetSwitches();
84 switches::SwitchInfoMap::const_iterator found
=
85 all
.find(base::StringPiece(what
));
86 if (found
== all
.end())
88 PrintLongHelp(found
->second
.long_help
);
94 const char kHelp
[] = "help";
95 const char kHelp_HelpShort
[] =
96 "help: Does what you think.";
97 const char kHelp_Help
[] =
98 "gn help <anything>\n"
99 " Yo dawg, I heard you like help on your help so I put help on the help\n"
102 int RunHelp(const std::vector
<std::string
>& args
) {
104 if (args
.size() == 0) {
105 // If no argument is specified, check for switches to allow things like
106 // "gn help --args" for help on the args switch.
107 const base::CommandLine::SwitchMap
& switches
=
108 CommandLine::ForCurrentProcess()->GetSwitches();
109 if (switches
.empty()) {
110 // Still nothing, show help overview.
115 // Switch help needs to be done separately. The CommandLine will strip the
116 // switch separators so --args will come out as "args" which is then
117 // ambiguous with the variable named "args".
118 if (!PrintHelpOnSwitch(switches
.begin()->first
))
126 const commands::CommandInfoMap
& command_map
= commands::GetCommands();
127 commands::CommandInfoMap::const_iterator found_command
=
128 command_map
.find(what
);
129 if (found_command
!= command_map
.end()) {
130 PrintLongHelp(found_command
->second
.help
);
135 const functions::FunctionInfoMap
& function_map
= functions::GetFunctions();
136 functions::FunctionInfoMap::const_iterator found_function
=
137 function_map
.find(what
);
138 if (found_function
!= function_map
.end()) {
139 PrintLongHelp(found_function
->second
.help
);
143 // Builtin variables.
144 const variables::VariableInfoMap
& builtin_vars
=
145 variables::GetBuiltinVariables();
146 variables::VariableInfoMap::const_iterator found_builtin_var
=
147 builtin_vars
.find(what
);
148 if (found_builtin_var
!= builtin_vars
.end()) {
149 PrintLongHelp(found_builtin_var
->second
.help
);
154 const variables::VariableInfoMap
& target_vars
=
155 variables::GetTargetVariables();
156 variables::VariableInfoMap::const_iterator found_target_var
=
157 target_vars
.find(what
);
158 if (found_target_var
!= target_vars
.end()) {
159 PrintLongHelp(found_target_var
->second
.help
);
163 // Random other topics.
164 if (what
== "buildargs") {
165 PrintLongHelp(kBuildArgs_Help
);
168 if (what
== "dotfile") {
169 PrintLongHelp(kDotfile_Help
);
172 if (what
== "input_conversion") {
173 PrintLongHelp(kInputConversion_Help
);
176 if (what
== "label_pattern") {
177 PrintLongHelp(kLabelPattern_Help
);
180 if (what
== "source_expansion") {
181 PrintLongHelp(kSourceExpansion_Help
);
184 if (what
== "switches") {
190 Err(Location(), "No help on \"" + what
+ "\".").PrintToStdout();
191 RunHelp(std::vector
<std::string
>());
195 } // namespace commands