void options_usage(struct st_option *options, const char * program) { struct st_option * option = options; int there_are_flags = 0, there_are_positionals = 0; for (option = options; option->description != NULL; option++) { if (IS_POSITIONAL(option)) there_are_positionals = 1; else there_are_flags = 1; if (there_are_positionals && there_are_flags) break; } if (there_are_flags) printf("Usage: %s [OPTIONS] ", program); else printf("Usage: %s ", program); if (there_are_positionals) for (option = options; option->description != NULL; option++) { if (!IS_POSITIONAL(option)) continue; if (IT_HAS_DEFAULT(option)) printf("[%s] ", option->flaglong); else printf("<%s> ", option->flaglong); } printf("\n\n"); if (there_are_positionals) { printf("Where\n\n"); for (option = options; option->description != NULL; option++) { if (!IS_POSITIONAL(option)) continue; puts(format_option(option)); } printf("\n"); } if (there_are_flags) { printf("The available options are:\n\n"); for (option = options; option->description != NULL; option++) { if (IS_POSITIONAL(option)) continue; puts(format_option(option)); } printf("\n"); } };
void svn_opt_format_option(const char **string, const apr_getopt_option_t *opt, svn_boolean_t doc, apr_pool_t *pool) { format_option(string, opt, NULL, doc, pool); }
std::string Options::help() const { typedef std::vector<std::pair<std::string, std::string>> OptionHelp; auto group = m_help.find(""); if (group == m_help.end()) { return ""; } OptionHelp format; size_t longest = 0; std::string result; for (const auto& o : group->second) { auto s = format_option(o.s, o.l, o.has_arg); longest = std::max(longest, s.size()); format.push_back(std::make_pair(s, std::string())); } longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST)); //widest allowed description int allowed = 76 - longest - OPTION_DESC_GAP; auto fiter = format.begin(); for (const auto& o : group->second) { auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed); result += fiter->first; if (fiter->first.size() > longest) { result += "\n"; result += std::string(longest + OPTION_DESC_GAP, ' '); } else { result += std::string(longest + OPTION_DESC_GAP - fiter->first.size(), ' '); } result += d; result += "\n"; ++fiter; } return result; }
/* Print the canonical command name for CMD, and all its aliases, to STREAM. If HELP is set, print CMD's help string too, in which case obtain option usage from OPTIONS_TABLE. */ static svn_error_t * print_command_info2(const svn_opt_subcommand_desc2_t *cmd, const apr_getopt_option_t *options_table, const int *global_options, svn_boolean_t help, apr_pool_t *pool, FILE *stream) { svn_boolean_t first_time; apr_size_t i; /* Print the canonical command name. */ SVN_ERR(svn_cmdline_fputs(cmd->name, stream, pool)); /* Print the list of aliases. */ first_time = TRUE; for (i = 0; i < SVN_OPT_MAX_ALIASES; i++) { if (cmd->aliases[i] == NULL) break; if (first_time) { SVN_ERR(svn_cmdline_fputs(" (", stream, pool)); first_time = FALSE; } else SVN_ERR(svn_cmdline_fputs(", ", stream, pool)); SVN_ERR(svn_cmdline_fputs(cmd->aliases[i], stream, pool)); } if (! first_time) SVN_ERR(svn_cmdline_fputs(")", stream, pool)); if (help) { const apr_getopt_option_t *option; const char *long_alias; svn_boolean_t have_options = FALSE; SVN_ERR(svn_cmdline_fprintf(stream, pool, ": %s", _(cmd->help))); /* Loop over all valid option codes attached to the subcommand */ for (i = 0; i < SVN_OPT_MAX_OPTIONS; i++) { if (cmd->valid_options[i]) { if (!have_options) { SVN_ERR(svn_cmdline_fputs(_("\nValid options:\n"), stream, pool)); have_options = TRUE; } /* convert each option code into an option */ option = get_option_from_code(&long_alias, cmd->valid_options[i], options_table, cmd, pool); /* print the option's docstring */ if (option && option->description) { const char *optstr; format_option(&optstr, option, long_alias, TRUE, pool); SVN_ERR(svn_cmdline_fprintf(stream, pool, " %s\n", optstr)); } } } /* And global options too */ if (global_options && *global_options) { SVN_ERR(svn_cmdline_fputs(_("\nGlobal options:\n"), stream, pool)); have_options = TRUE; for (i = 0; global_options[i]; i++) { /* convert each option code into an option */ option = get_option_from_code(&long_alias, global_options[i], options_table, cmd, pool); /* print the option's docstring */ if (option && option->description) { const char *optstr; format_option(&optstr, option, long_alias, TRUE, pool); SVN_ERR(svn_cmdline_fprintf(stream, pool, " %s\n", optstr)); } } } if (have_options) SVN_ERR(svn_cmdline_fprintf(stream, pool, "\n")); } return SVN_NO_ERROR; }