OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <iostream> | 6 #include <iostream> |
7 | 7 |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "tools/gn/args.h" | 9 #include "tools/gn/args.h" |
10 #include "tools/gn/commands.h" | 10 #include "tools/gn/commands.h" |
11 #include "tools/gn/err.h" | 11 #include "tools/gn/err.h" |
12 #include "tools/gn/functions.h" | 12 #include "tools/gn/functions.h" |
13 #include "tools/gn/input_conversion.h" | 13 #include "tools/gn/input_conversion.h" |
14 #include "tools/gn/label_pattern.h" | 14 #include "tools/gn/label_pattern.h" |
15 #include "tools/gn/parser.h" | 15 #include "tools/gn/parser.h" |
16 #include "tools/gn/runtime_deps.h" | 16 #include "tools/gn/runtime_deps.h" |
17 #include "tools/gn/setup.h" | 17 #include "tools/gn/setup.h" |
18 #include "tools/gn/standard_out.h" | 18 #include "tools/gn/standard_out.h" |
19 #include "tools/gn/string_utils.h" | |
19 #include "tools/gn/substitution_writer.h" | 20 #include "tools/gn/substitution_writer.h" |
20 #include "tools/gn/switches.h" | 21 #include "tools/gn/switches.h" |
21 #include "tools/gn/variables.h" | 22 #include "tools/gn/variables.h" |
22 | 23 |
23 namespace commands { | 24 namespace commands { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 void PrintToplevelHelp() { | 28 void PrintToplevelHelp() { |
28 OutputString("Commands (type \"gn help <command>\" for more details):\n"); | 29 OutputString("Commands (type \"gn help <command>\" for more details):\n"); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 // Switch help needs to be done separately. The CommandLine will strip the | 170 // Switch help needs to be done separately. The CommandLine will strip the |
170 // switch separators so --args will come out as "args" which is then | 171 // switch separators so --args will come out as "args" which is then |
171 // ambiguous with the variable named "args". | 172 // ambiguous with the variable named "args". |
172 if (!PrintHelpOnSwitch(switches.begin()->first)) | 173 if (!PrintHelpOnSwitch(switches.begin()->first)) |
173 PrintToplevelHelp(); | 174 PrintToplevelHelp(); |
174 return 0; | 175 return 0; |
175 } else { | 176 } else { |
176 what = args[0]; | 177 what = args[0]; |
177 } | 178 } |
178 | 179 |
180 std::vector<base::StringPiece> all_help_topics; | |
181 | |
179 // Check commands. | 182 // Check commands. |
180 const commands::CommandInfoMap& command_map = commands::GetCommands(); | 183 const commands::CommandInfoMap& command_map = commands::GetCommands(); |
181 commands::CommandInfoMap::const_iterator found_command = | 184 auto found_command = command_map.find(what); |
182 command_map.find(what); | |
183 if (found_command != command_map.end()) { | 185 if (found_command != command_map.end()) { |
184 PrintLongHelp(found_command->second.help); | 186 PrintLongHelp(found_command->second.help); |
185 return 0; | 187 return 0; |
186 } | 188 } |
189 for (const auto& entry : command_map) | |
190 all_help_topics.push_back(entry.first); | |
187 | 191 |
188 // Check functions. | 192 // Check functions. |
189 const functions::FunctionInfoMap& function_map = functions::GetFunctions(); | 193 const functions::FunctionInfoMap& function_map = functions::GetFunctions(); |
190 functions::FunctionInfoMap::const_iterator found_function = | 194 auto found_function = function_map.find(what); |
191 function_map.find(what); | |
192 if (found_function != function_map.end()) { | 195 if (found_function != function_map.end()) { |
193 PrintLongHelp(found_function->second.help); | 196 PrintLongHelp(found_function->second.help); |
194 return 0; | 197 return 0; |
195 } | 198 } |
199 for (const auto& entry : function_map) | |
200 all_help_topics.push_back(entry.first); | |
196 | 201 |
197 // Builtin variables. | 202 // Builtin variables. |
198 const variables::VariableInfoMap& builtin_vars = | 203 const variables::VariableInfoMap& builtin_vars = |
199 variables::GetBuiltinVariables(); | 204 variables::GetBuiltinVariables(); |
200 variables::VariableInfoMap::const_iterator found_builtin_var = | 205 auto found_builtin_var = builtin_vars.find(what); |
201 builtin_vars.find(what); | |
202 if (found_builtin_var != builtin_vars.end()) { | 206 if (found_builtin_var != builtin_vars.end()) { |
203 PrintLongHelp(found_builtin_var->second.help); | 207 PrintLongHelp(found_builtin_var->second.help); |
204 return 0; | 208 return 0; |
205 } | 209 } |
210 for (const auto& entry : builtin_vars) | |
211 all_help_topics.push_back(entry.first); | |
206 | 212 |
207 // Target variables. | 213 // Target variables. |
208 const variables::VariableInfoMap& target_vars = | 214 const variables::VariableInfoMap& target_vars = |
209 variables::GetTargetVariables(); | 215 variables::GetTargetVariables(); |
210 variables::VariableInfoMap::const_iterator found_target_var = | 216 auto found_target_var = target_vars.find(what); |
211 target_vars.find(what); | |
212 if (found_target_var != target_vars.end()) { | 217 if (found_target_var != target_vars.end()) { |
213 PrintLongHelp(found_target_var->second.help); | 218 PrintLongHelp(found_target_var->second.help); |
214 return 0; | 219 return 0; |
215 } | 220 } |
221 for (const auto& entry : target_vars) | |
222 all_help_topics.push_back(entry.first); | |
216 | 223 |
217 // Random other topics. | 224 // Random other topics. |
218 if (what == "all") { | 225 std::map<std::string, std::function<void()>> random_topics; |
219 PrintAllHelp(); | 226 random_topics["all"] = PrintAllHelp; |
227 random_topics["buildargs"] = [=]() { PrintLongHelp(kBuildArgs_Help); }; | |
jbroman
2016/02/17 00:03:09
Sorry to drop in since I actually quite like this,
Nico
2016/02/17 20:43:53
Thanks, good comments. Addressing them here: https
| |
228 random_topics["dotfile"] = [=]() { PrintLongHelp(kDotfile_Help); }; | |
229 random_topics["grammar"] = [=]() { PrintLongHelp(kGrammar_Help); }; | |
230 random_topics["input_conversion"] = [=]() { | |
231 PrintLongHelp(kInputConversion_Help); | |
232 }; | |
233 random_topics["label_pattern"] = [=]() { PrintLongHelp(kLabelPattern_Help); }; | |
234 random_topics["runtime_deps"] = [=]() { PrintLongHelp(kRuntimeDeps_Help); }; | |
235 random_topics["source_expansion"] = [=]() { | |
236 PrintLongHelp(kSourceExpansion_Help); | |
237 }; | |
238 random_topics["switches"] = PrintSwitchHelp; | |
239 auto found_random_topic = random_topics.find(what); | |
240 if (found_random_topic != random_topics.end()) { | |
241 found_random_topic->second(); | |
220 return 0; | 242 return 0; |
221 } | 243 } |
222 if (what == "buildargs") { | 244 for (const auto& entry : random_topics) |
223 PrintLongHelp(kBuildArgs_Help); | 245 all_help_topics.push_back(entry.first); |
224 return 0; | |
225 } | |
226 if (what == "dotfile") { | |
227 PrintLongHelp(kDotfile_Help); | |
228 return 0; | |
229 } | |
230 if (what == "grammar") { | |
231 PrintLongHelp(kGrammar_Help); | |
232 return 0; | |
233 } | |
234 if (what == "input_conversion") { | |
235 PrintLongHelp(kInputConversion_Help); | |
236 return 0; | |
237 } | |
238 if (what == "label_pattern") { | |
239 PrintLongHelp(kLabelPattern_Help); | |
240 return 0; | |
241 } | |
242 if (what == "runtime_deps") { | |
243 PrintLongHelp(kRuntimeDeps_Help); | |
244 return 0; | |
245 } | |
246 if (what == "source_expansion") { | |
247 PrintLongHelp(kSourceExpansion_Help); | |
248 return 0; | |
249 } | |
250 if (what == "switches") { | |
251 PrintSwitchHelp(); | |
252 return 0; | |
253 } | |
254 | 246 |
255 // No help on this. | 247 // No help on this. |
256 Err(Location(), "No help on \"" + what + "\".").PrintToStdout(); | 248 Err(Location(), "No help on \"" + what + "\".").PrintToStdout(); |
257 RunHelp(std::vector<std::string>()); | 249 base::StringPiece suggestion = SpellcheckString(what, all_help_topics); |
250 if (suggestion.empty()) { | |
251 OutputString("Run `gn help` for a list of available topics.\n", | |
252 DECORATION_NONE); | |
253 } else { | |
254 OutputString("Did you mean `gn help " + suggestion.as_string() + "`?\n", | |
255 DECORATION_NONE); | |
256 } | |
258 return 1; | 257 return 1; |
259 } | 258 } |
260 | 259 |
261 } // namespace commands | 260 } // namespace commands |
OLD | NEW |