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. 4 5 #include <algorithm> 6 #include <iostream> 7 8 #include "tools/gn/commands.h" 9 #include "tools/gn/err.h" 10 #include "tools/gn/functions.h" 11 #include "tools/gn/input_conversion.h" 12 #include "tools/gn/setup.h" 13 #include "tools/gn/standard_out.h" 14 #include "tools/gn/variables.h" 15 16 namespace commands { 17 18 namespace { 19 20 // Prints a line for a command, assuming there is a colon. Everything before 21 // the colon is the command (and is highlighted) and everything after it is 22 // normal. 23 void PrintShortHelp(const std::string& line) { 24 size_t colon_offset = line.find(':'); 25 size_t first_normal = 0; 26 if (colon_offset != std::string::npos) { 27 OutputString(" " + line.substr(0, colon_offset), DECORATION_YELLOW); 28 first_normal = colon_offset; 29 } 30 OutputString(line.substr(first_normal) + "\n"); 31 } 32 33 void PrintToplevelHelp() { 34 OutputString("Commands (type \"gn help <command>\" for more details):\n"); 35 36 const commands::CommandInfoMap& command_map = commands::GetCommands(); 37 for (commands::CommandInfoMap::const_iterator i = command_map.begin(); 38 i != command_map.end(); ++i) 39 PrintShortHelp(i->second.help_short); 40 41 OutputString( 42 "\n" 43 " When run with no arguments \"gn gen\" is assumed.\n" 44 "\n" 45 "Common switches:\n" 46 " -q: Quiet mode, don't print anything on success.\n" 47 " --root: Specifies source root (overrides .gn file).\n" 48 " --secondary: Specifies secondary source root (overrides .gn file).\n" 49 " -v: Verbose mode, print lots of logging.\n"); 50 51 // Functions. 52 OutputString("\nBuildfile functions (type \"gn help <function>\" for more " 53 "details):\n"); 54 const functions::FunctionInfoMap& function_map = functions::GetFunctions(); 55 std::vector<std::string> sorted_functions; 56 for (functions::FunctionInfoMap::const_iterator i = function_map.begin(); 57 i != function_map.end(); ++i) 58 sorted_functions.push_back(i->first.as_string()); 59 std::sort(sorted_functions.begin(), sorted_functions.end()); 60 for (size_t i = 0; i < sorted_functions.size(); i++) 61 OutputString(" " + sorted_functions[i] + "\n", DECORATION_YELLOW); 62 63 // Built-in variables. 64 OutputString("\nBuilt-in predefined variables (type \"gn help <variable>\" " 65 "for more details):\n"); 66 const variables::VariableInfoMap& builtin_vars = 67 variables::GetBuiltinVariables(); 68 for (variables::VariableInfoMap::const_iterator i = builtin_vars.begin(); 69 i != builtin_vars.end(); ++i) 70 PrintShortHelp(i->second.help_short); 71 72 // Target variables. 73 OutputString("\nVariables you set in targets (type \"gn help <variable>\" " 74 "for more details):\n"); 75 const variables::VariableInfoMap& target_vars = 76 variables::GetTargetVariables(); 77 for (variables::VariableInfoMap::const_iterator i = target_vars.begin(); 78 i != target_vars.end(); ++i) 79 PrintShortHelp(i->second.help_short); 80 81 OutputString("\nOther help topics:\n"); 82 PrintShortHelp("dotfile: Info about the toplevel .gn file."); 83 PrintShortHelp( 84 "input_conversion: Processing input from exec_script and read_file."); 85 } 86 87 } // namespace 88 89 const char kHelp[] = "help"; 90 const char kHelp_HelpShort[] = 91 "help: Does what you think."; 92 const char kHelp_Help[] = 93 "gn help <anything>\n" 94 " Yo dawg, I heard you like help on your help so I put help on the help\n" 95 " in the help.\n"; 96 97 int RunHelp(const std::vector<std::string>& args) { 98 if (args.size() == 0) { 99 PrintToplevelHelp(); 100 return 0; 101 } 102 103 // Check commands. 104 const commands::CommandInfoMap& command_map = commands::GetCommands(); 105 commands::CommandInfoMap::const_iterator found_command = 106 command_map.find(args[0]); 107 if (found_command != command_map.end()) { 108 OutputString(found_command->second.help); 109 return 0; 110 } 111 112 // Check functions. 113 const functions::FunctionInfoMap& function_map = functions::GetFunctions(); 114 functions::FunctionInfoMap::const_iterator found_function = 115 function_map.find(args[0]); 116 if (found_function != function_map.end()) { 117 OutputString(found_function->second.help); 118 return 0; 119 } 120 121 // Builtin variables. 122 const variables::VariableInfoMap& builtin_vars = 123 variables::GetBuiltinVariables(); 124 variables::VariableInfoMap::const_iterator found_builtin_var = 125 builtin_vars.find(args[0]); 126 if (found_builtin_var != builtin_vars.end()) { 127 OutputString(found_builtin_var->second.help); 128 return 0; 129 } 130 131 // Target variables. 132 const variables::VariableInfoMap& target_vars = 133 variables::GetTargetVariables(); 134 variables::VariableInfoMap::const_iterator found_target_var = 135 target_vars.find(args[0]); 136 if (found_target_var != target_vars.end()) { 137 OutputString(found_target_var->second.help); 138 return 0; 139 } 140 141 // Random other topics. 142 if (args[0] == "input_conversion") { 143 OutputString(kInputConversion_Help); 144 return 0; 145 } if (args[0] == "dotfile") { 146 OutputString(kDotfile_Help); 147 return 0; 148 } 149 150 // No help on this. 151 Err(Location(), "No help on \"" + args[0] + "\".").PrintToStdout(); 152 RunHelp(std::vector<std::string>()); 153 return 1; 154 } 155 156 } // namespace commands 157