--------------------------------------------------------------------------- ^FUNCTION: Options::usage - print usage ^SYNOPSIS: void Options::usage(os, positionals) ^PARAMETERS: ostream & os -- where to print the usage char * positionals -- command-line syntax for any positional args ^DESCRIPTION: Print command-usage (using either option or long-option syntax) on os. ^REQUIREMENTS: os should co
| 1068 | // Print usage on os, wrapping long lines where necessary. |
| 1069 | // ^^------------------------------------------------------------------------- |
| 1070 | void Options::usage(ostream &os, const char *positionals) const |
| 1071 | { |
| 1072 | #ifdef NO_USAGE |
| 1073 | return; |
| 1074 | #else |
| 1075 | const char *const *optv = optvec; |
| 1076 | unsigned cols = 79; |
| 1077 | int first, nloop; |
| 1078 | char buf[256]; |
| 1079 | |
| 1080 | if ((optv == NULL) || (!*optv)) |
| 1081 | return; |
| 1082 | |
| 1083 | // print first portion "usage: progname" |
| 1084 | os << "usage: " << cmdname; |
| 1085 | unsigned ll = (unsigned)strlen(cmdname) + 7; |
| 1086 | |
| 1087 | // save the current length so we know how much space to skip for |
| 1088 | // subsequent lines. |
| 1089 | // |
| 1090 | unsigned margin = ll + 1; |
| 1091 | |
| 1092 | // print the options and the positional arguments |
| 1093 | for (nloop = 0, first = 1; !nloop; optv++, first = 0) |
| 1094 | { |
| 1095 | unsigned len; |
| 1096 | OptionSpec optspec = *optv; |
| 1097 | |
| 1098 | // figure out how wide this parameter is (for printing) |
| 1099 | if (!*optv) |
| 1100 | { |
| 1101 | len = (unsigned)strlen(positionals); |
| 1102 | ++nloop; // terminate this loop |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | if (optspec.isHiddenOpt()) |
| 1107 | continue; |
| 1108 | len = optspec.Format(buf, optctrls); |
| 1109 | } |
| 1110 | |
| 1111 | // Will this fit? |
| 1112 | if ((ll + len + 1) > (cols - first)) |
| 1113 | { |
| 1114 | os << '\n'; // No - start a new line; |
| 1115 | #ifdef USE_STDIO |
| 1116 | for (int _i_ = 0; _i_ < margin; ++_i_) |
| 1117 | os << " "; |
| 1118 | #else |
| 1119 | os.width(margin); |
| 1120 | os << ""; |
| 1121 | #endif |
| 1122 | ll = margin; |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | os << ' '; // Yes - just throw in a space |
| 1127 | ++ll; |
no test coverage detected