137 const string Tab =
" ";
138 if (
getopt is
null) {
throw new ArgumentNullException(nameof(
getopt),
"getopt must not be null!"); }
140 var options =
getopt.Options;
141 if (options is
null || options.Length == 0) {
return string.Empty; }
145 var sBuilder =
new StringBuilder();
147 if (!
string.IsNullOrEmpty(config.ApplicationName) && !
string.IsNullOrEmpty(config.ApplicationVersion)) {
148 sBuilder.Append($
"{config.ApplicationName} {config.ApplicationVersion}");
149 if (config.CopyrightDate is not
null && !
string.IsNullOrEmpty(config.CopyrightHolder)) {
150 sBuilder.Append($
" © {config.CopyrightDate.Value.Year} {config.CopyrightHolder}");
152 sBuilder.AppendLine()
156 string shortOptPrefix = config.OptionConvention == OptionConvention.Windows ?
"/" :
"-";
157 string longOptPrefix = config.OptionConvention == OptionConvention.Windows ?
"/" : config.OptionConvention == OptionConvention.GnuPosix ?
"--" :
"-";
159 sBuilder.AppendLine(
"Usage:");
160 sBuilder.AppendLine($
"{Tab}{config.ApplicationName ?? GetApplicationName()} [options]");
161 if (config.ShowSupportedConventions) {
165 Supported option conventions:
166 Windows (/): {(getopt.AllowWindowsConventions ? "yes
" : "no
")}
167 Powershell (-): {(getopt.AllowPowershellConventions ? "yes
" : "no
")}
168 Gnu/Posix (-, --): yes
172 sBuilder.AppendLine();
174 var longestName = options.Max(o => o.Name?.Length ?? 0);
176 longestName = (longestName + 3) / 4 * 4;
178 sBuilder.AppendLine(
"Switches:");
179 foreach (var opt
in options.Where(o => o.ArgumentType ==
ArgumentType.None)) {
180 sBuilder.AppendLine(
string.Format(
"{0}{1}{2}, {3}{4}{5}", Tab, shortOptPrefix, (
char)opt.Value, longOptPrefix, opt.Name?.PadRight(longestName), opt.Description ??
string.Empty));
182 sBuilder.AppendLine();
184 sBuilder.AppendLine(
"Options:");
185 foreach (var opt
in options.Where(o => o.ArgumentType !=
ArgumentType.None)) {
186 var line =
string.Format(
"{0}{1}{2}, {3}{4}{5}", Tab, shortOptPrefix, (
char)opt.Value, longOptPrefix, opt.Name?.PadRight(longestName), opt.Description ??
string.Empty);
189 if (line.Length > config.MaxWidth) {
190 var desc = opt.Description ??
string.Empty;
191 var beginWhitespace =
new string(
194 shortOptPrefix.Length +
196 longOptPrefix.Length +
197 (opt.Name?.PadRight(longestName).Length ?? 0) +
201 while (desc.Length > config.MaxWidth - longestName - 10) {
202 var split = desc.Substring(0, config.MaxWidth - longestName - 10);
203 var splitIndex = split.LastIndexOf(
' ');
205 sBuilder.AppendLine(
string.Format(
"{0}{1}", beginWhitespace, split.Substring(0, splitIndex)));
206 desc = desc.Substring(splitIndex + 1);
209 sBuilder.AppendLine(
string.Format(
"{0}{1}{2}, {3}{4}{5}", Tab, shortOptPrefix, (
char)opt.Value, longOptPrefix, opt.Name?.PadRight(longestName), desc));
211 sBuilder.AppendLine(line);
214 sBuilder.AppendLine();
216 sBuilder.AppendLine();
218 if (!
string.IsNullOrEmpty(config.FooterText)) {
219 sBuilder.AppendLine(config.FooterText);
222 return sBuilder.ToString();