Skip to content

Commit 862012d

Browse files
OptionParser.cpp: adding code to remove any formatting in terms of space tabs or newlines from the program description before reformatting it
1 parent 07acd80 commit 862012d

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

OptionParser.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
/*
2-
* Part of SMITHLAB software
1+
/* Part of SMITHLAB software
32
*
4-
* Copyright (C) 2008 Cold Spring Harbor Laboratory,
5-
* University of Southern California and
6-
* Andrew D. Smith
3+
* Copyright (C) 2008-2024 Cold Spring Harbor Laboratory,
4+
* University of Southern California and
5+
* Andrew D. Smith
76
*
8-
* Authors: Andrew D. Smith
7+
* Authors: Andrew D. Smith
98
*
10-
* This program is free software: you can redistribute it and/or modify
11-
* it under the terms of the GNU General Public License as published by
12-
* the Free Software Foundation, either version 3 of the License, or
13-
* (at your option) any later version.
9+
* This program is free software: you can redistribute it and/or
10+
* modify it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
1413
*
15-
* This program is distributed in the hope that it will be useful,
16-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18-
* GNU General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but
15+
* WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* General Public License for more details.
1918
*
20-
* You should have received a copy of the GNU General Public License
21-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see
21+
* <http://www.gnu.org/licenses/>.
2222
*/
2323

2424
#include "OptionParser.hpp"
@@ -32,6 +32,7 @@
3232
#include <functional>
3333
#include <iomanip>
3434
#include <iterator>
35+
#include <regex>
3536
#include <sstream>
3637

3738
#include "smithlab_utils.hpp"
@@ -211,7 +212,7 @@ string Option::format_option_description(const size_t offset,
211212

212213
size_t line_len = 0;
213214
for (size_t i = 0; i < parts.size(); ++i) {
214-
if (offset + line_len + parts[i].size() >= MAX_LINE_LENGTH && i > 0) {
215+
if (offset + line_len + parts[i].size() > MAX_LINE_LENGTH && i > 0) {
215216
line_len = 0;
216217
ss << endl;
217218
}
@@ -530,16 +531,24 @@ string OptionParser::help_message() const {
530531

531532
string OptionParser::about_message() const {
532533
static const char *PROGRAM_NAME_TAG = "PROGRAM: ";
534+
static const std::regex whitespace_re(R"([\s]+)");
535+
536+
// remove newlines
537+
string tmp_descr{prog_descr};
538+
regex_replace(begin(tmp_descr), cbegin(tmp_descr), cend(tmp_descr),
539+
whitespace_re, " ");
540+
tmp_descr.erase(tmp_descr.find_last_not_of(' ') + 1);
541+
tmp_descr.erase(0, tmp_descr.find_first_not_of(' '));
533542

534543
vector<string> parts;
535-
smithlab::split_whitespace(prog_descr, parts);
544+
smithlab::split_whitespace(tmp_descr, parts);
536545

537546
std::ostringstream ss;
538547
ss << PROGRAM_NAME_TAG << prog_name << endl;
539548
ss << parts.front();
540549
size_t line_len = parts.front().length();
541550
for (size_t i = 1; i < parts.size(); ++i) {
542-
if (line_len + parts[i].size() >= MAX_LINE_LENGTH) {
551+
if (line_len + parts[i].size() > MAX_LINE_LENGTH) {
543552
line_len = 0;
544553
ss << endl;
545554
}

0 commit comments

Comments
 (0)