Skip to content

Commit a07d766

Browse files
committed
Improve diagnostic for ambiguous compiler flag name
To indicate the unique prefix in [ ], same as when listing the flags using -?
1 parent b3e7ef1 commit a07d766

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

source/common.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,22 @@ class cmdline_processor
648648
flag(int g, std::string_view n, std::string_view d, callback0 h0, callback1 h1, std::string_view s, bool o)
649649
: group{g}, name{n}, description{d}, handler0{h0}, handler1{h1}, synonym{s}, opt_out{o}
650650
{ }
651+
652+
auto get_name(bool indicate_short_name = false) const {
653+
auto n = name.substr(0, unique_prefix);
654+
if (unique_prefix < std::ssize(name)) {
655+
auto name_length = _as<int>(std::min(name.find(' '), name.size()));
656+
if (indicate_short_name) {
657+
n += "[";
658+
}
659+
n += name.substr(unique_prefix, name_length - unique_prefix);
660+
if (indicate_short_name) {
661+
n += "]";
662+
}
663+
n += name.substr(name_length);
664+
}
665+
return n;
666+
}
651667
};
652668
std::vector<flag> flags;
653669
int max_flag_length = 0;
@@ -669,13 +685,16 @@ class cmdline_processor
669685
}
670686

671687
public:
672-
auto flags_starting_with(std::string_view s)
688+
auto flags_starting_with(
689+
std::string_view s,
690+
bool indicate_short_name = true
691+
)
673692
-> std::vector<std::string>
674693
{
675694
auto ret = std::vector<std::string>{};
676-
for (auto const& f : flags) {
677-
if (f.name.starts_with(s)) {
678-
ret.push_back(f.name);
695+
for (auto const& flag : flags) {
696+
if (flag.name.starts_with(s)) {
697+
ret.push_back( flag.get_name(indicate_short_name) );
679698
}
680699
}
681700
return ret;
@@ -801,14 +820,7 @@ class cmdline_processor
801820
}
802821
}
803822
print(" -");
804-
auto n = flag.name.substr(0, flag.unique_prefix);
805-
if (flag.unique_prefix < std::ssize(flag.name)) {
806-
auto name_length = _as<int>(std::min(flag.name.find(' '), flag.name.size()));
807-
n += "[";
808-
n += flag.name.substr(flag.unique_prefix, name_length - flag.unique_prefix);
809-
n += "]";
810-
n += flag.name.substr(name_length);
811-
}
823+
auto n = flag.get_name(true);
812824
if (flag.opt_out) {
813825
n += "[-]";
814826
}

source/cppfront.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ auto main(
7373
else {
7474
std::cerr << arg.text << " - ambiguous compiler flag name, did you mean one of these?\n";
7575
for (auto a : ambiguous) {
76-
std::cerr << " " << arg.text.front() << a << "\n";
76+
std::cerr << " " << arg.text.front() << a << "\n";
7777
}
7878
}
7979
return EXIT_FAILURE;

0 commit comments

Comments
 (0)