void Options::OutputFormattedUsageText(Stream &strm, const OptionDefinition &option_def, uint32_t output_max_columns) { std::string actual_text; if (option_def.validator) { const char *condition = option_def.validator->ShortConditionString(); if (condition) { actual_text = "["; actual_text.append(condition); actual_text.append("] "); } } actual_text.append(option_def.usage_text); // Will it all fit on one line? if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < output_max_columns) { // Output it as a single line. strm.Indent(actual_text.c_str()); strm.EOL(); } else { // We need to break it up into multiple lines. int text_width = output_max_columns - strm.GetIndentLevel() - 1; int start = 0; int end = start; int final_end = actual_text.length(); int sub_len; while (end < final_end) { // Don't start the 'text' on a space, since we're already outputting the // indentation. while ((start < final_end) && (actual_text[start] == ' ')) start++; end = start + text_width; if (end > final_end) end = final_end; else { // If we're not at the end of the text, make sure we break the line on // white space. while (end > start && actual_text[end] != ' ' && actual_text[end] != '\t' && actual_text[end] != '\n') end--; } sub_len = end - start; if (start != 0) strm.EOL(); strm.Indent(); assert(start < final_end); assert(start + sub_len <= final_end); strm.Write(actual_text.c_str() + start, sub_len); start = end + 1; } strm.EOL(); } }
void Options::OutputFormattedUsageText ( Stream &strm, const char *text, uint32_t output_max_columns ) { int len = strlen (text); // Will it all fit on one line? if (static_cast<uint32_t>(len + strm.GetIndentLevel()) < output_max_columns) { // Output it as a single line. strm.Indent (text); strm.EOL(); } else { // We need to break it up into multiple lines. int text_width = output_max_columns - strm.GetIndentLevel() - 1; int start = 0; int end = start; int final_end = strlen (text); int sub_len; while (end < final_end) { // Don't start the 'text' on a space, since we're already outputting the indentation. while ((start < final_end) && (text[start] == ' ')) start++; end = start + text_width; if (end > final_end) end = final_end; else { // If we're not at the end of the text, make sure we break the line on white space. while (end > start && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') end--; } sub_len = end - start; if (start != 0) strm.EOL(); strm.Indent(); assert (start < final_end); assert (start + sub_len <= final_end); strm.Write(text + start, sub_len); start = end + 1; } strm.EOL(); } }
void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, uint32_t screen_width) { const bool only_print_args = cmd->IsDashDashCommand(); auto opt_defs = GetDefinitions(); const uint32_t save_indent_level = strm.GetIndentLevel(); llvm::StringRef name; StreamString arguments_str; if (cmd) { name = cmd->GetCommandName(); cmd->GetFormattedCommandArguments(arguments_str); } else name = ""; strm.PutCString("\nCommand Options Usage:\n"); strm.IndentMore(2); // First, show each usage level set of options, e.g. <cmd> // [options-for-level-0] // <cmd> // [options-for-level-1] // etc. const uint32_t num_options = NumCommandOptions(); if (num_options == 0) return; uint32_t num_option_sets = GetRequiredOptions().size(); uint32_t i; if (!only_print_args) { for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) { uint32_t opt_set_mask; opt_set_mask = 1 << opt_set; if (opt_set > 0) strm.Printf("\n"); strm.Indent(name); // Different option sets may require different args. StreamString args_str; if (cmd) cmd->GetFormattedCommandArguments(args_str, opt_set_mask); // First go through and print all options that take no arguments as // a single string. If a command has "-a" "-b" and "-c", this will show // up as [-abc] std::set<int> options; std::set<int>::const_iterator options_pos, options_end; for (auto &def : opt_defs) { if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { // Add current option to the end of out_stream. if (def.required && def.option_has_arg == OptionParser::eNoArgument) { options.insert(def.short_option); } } } if (options.empty() == false) { // We have some required options with no arguments strm.PutCString(" -"); for (i = 0; i < 2; ++i) for (options_pos = options.begin(), options_end = options.end(); options_pos != options_end; ++options_pos) { if (i == 0 && ::islower(*options_pos)) continue; if (i == 1 && ::isupper(*options_pos)) continue; strm << (char)*options_pos; } } options.clear(); for (auto &def : opt_defs) { if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { // Add current option to the end of out_stream. if (def.required == false && def.option_has_arg == OptionParser::eNoArgument) { options.insert(def.short_option); } } } if (options.empty() == false) { // We have some required options with no arguments strm.PutCString(" [-"); for (i = 0; i < 2; ++i) for (options_pos = options.begin(), options_end = options.end(); options_pos != options_end; ++options_pos) { if (i == 0 && ::islower(*options_pos)) continue; if (i == 1 && ::isupper(*options_pos)) continue; strm << (char)*options_pos; } strm.PutChar(']'); } // First go through and print the required options (list them up front). for (auto &def : opt_defs) { if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { if (def.required && def.option_has_arg != OptionParser::eNoArgument) PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); } } // Now go through again, and this time only print the optional options. for (auto &def : opt_defs) { if (def.usage_mask & opt_set_mask) { // Add current option to the end of out_stream. if (!def.required && def.option_has_arg != OptionParser::eNoArgument) PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); } } if (args_str.GetSize() > 0) { if (cmd->WantsRawCommandString() && !only_print_args) strm.Printf(" --"); strm << " " << args_str.GetString(); if (only_print_args) break; } } } if (cmd && (only_print_args || cmd->WantsRawCommandString()) && arguments_str.GetSize() > 0) { if (!only_print_args) strm.PutChar('\n'); strm.Indent(name); strm << " " << arguments_str.GetString(); } strm.Printf("\n\n"); if (!only_print_args) { // Now print out all the detailed information about the various options: // long form, short form and help text: // -short <argument> ( --long_name <argument> ) // help text // This variable is used to keep track of which options' info we've printed // out, because some options can be in // more than one usage level, but we only want to print the long form of its // information once. std::multimap<int, uint32_t> options_seen; strm.IndentMore(5); // Put the unique command options in a vector & sort it, so we can output // them alphabetically (by short_option) // when writing out detailed help for each option. i = 0; for (auto &def : opt_defs) options_seen.insert(std::make_pair(def.short_option, i++)); // Go through the unique'd and alphabetically sorted vector of options, find // the table entry for each option // and write out the detailed help information for that option. bool first_option_printed = false; for (auto pos : options_seen) { i = pos.second; // Print out the help information for this option. // Put a newline separation between arguments if (first_option_printed) strm.EOL(); else first_option_printed = true; CommandArgumentType arg_type = opt_defs[i].argument_type; StreamString arg_name_str; arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type)); strm.Indent(); if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) { PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, strm); PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); } else { // Short option is not printable, just print long option PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, strm); } strm.EOL(); strm.IndentMore(5); if (opt_defs[i].usage_text) OutputFormattedUsageText(strm, opt_defs[i], screen_width); if (opt_defs[i].enum_values != nullptr) { strm.Indent(); strm.Printf("Values: "); for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; k++) { if (k == 0) strm.Printf("%s", opt_defs[i].enum_values[k].string_value); else strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); } strm.EOL(); } strm.IndentLess(5); } } // Restore the indent level strm.SetIndentLevel(save_indent_level); }