Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix help message for --version and --help #23

Merged
merged 3 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions include/structopt/visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ struct visitor {
os << field << " ";
}

bool has_h = false;
bool has_v = false;
if (flag_field_names.empty() == false) {
os << "\n\nFLAGS:\n";
for (auto &flag : flag_field_names) {
Expand All @@ -124,6 +126,15 @@ struct visitor {
}

os << " -" << flag[0] << ", --" << flag << "\n";

switch (flag[0]) {
case 'h':
has_h = true;
break;
case 'v':
has_v = true;
break;
}
}
} else {
os << "\n";
Expand All @@ -142,8 +153,21 @@ struct visitor {
long_form = option;
}

os << " -" << option[0] << ", --" << long_form << " <" << option << ">"
<< "\n";
if ((has_v && option == "version") || (has_h && option == "help")) {
os << " --" << long_form << " <" << option << ">\n";
} else {
os << " -" << option[0] << ", --" << long_form << " <" << option << ">"
<< "\n";
}

switch (option[0]) {
case 'h':
has_h = true;
break;
case 'v':
has_v = true;
break;
}
}
}

Expand Down
31 changes: 27 additions & 4 deletions single_include/structopt/structopt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,8 @@ struct visitor {
os << field << " ";
}

bool has_h = false;
bool has_v = false;
if (flag_field_names.empty() == false) {
os << "\n\nFLAGS:\n";
for (auto &flag : flag_field_names) {
Expand All @@ -2669,6 +2671,15 @@ struct visitor {
}

os << " -" << flag[0] << ", --" << flag << "\n";

switch (flag[0]) {
case 'h':
has_h = true;
break;
case 'v':
has_v = true;
break;
}
}
} else {
os << "\n";
Expand All @@ -2679,17 +2690,29 @@ struct visitor {
for (auto &option : optional_field_names) {

// Generate kebab case and present as option
auto kebab_case = option;
details::string_replace(kebab_case, "_", "-");
auto kebab_case = details::string_to_kebab(option);
std::string long_form = "";
if (kebab_case != option) {
long_form = kebab_case;
} else {
long_form = option;
}

os << " -" << option[0] << ", --" << long_form << " <" << option << ">"
<< "\n";
if ((has_v && option == "version") || (has_h && option == "help")) {
os << " --" << long_form << " <" << option << ">\n";
} else {
os << " -" << option[0] << ", --" << long_form << " <" << option << ">"
<< "\n";
}

switch (option[0]) {
case 'h':
has_h = true;
break;
case 'v':
has_v = true;
break;
}
}
}

Expand Down