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

Print main command along with subcommand on help page #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ Subcommands:
submodule Initialize, update or inspect submodules

foo@bar:/home/dev/$ ./git add --help
Usage: add [-h] files
Usage: git add [-h] files

Add file contents to the index

Expand All @@ -907,7 +907,7 @@ Optional arguments:
-v, --version prints version information and exits

foo@bar:/home/dev/$ ./git commit --help
Usage: commit [-h] [--all] [--message VAR]
Usage: git commit [-h] [--all] [--message VAR]

Record changes to the repository

Expand All @@ -918,7 +918,7 @@ Optional arguments:
-m, --message Use the given <msg> as the commit message.

foo@bar:/home/dev/$ ./git submodule --help
Usage: submodule [-h] {update}
Usage: git submodule [-h] {update}

Initialize, update or inspect submodules

Expand Down Expand Up @@ -957,7 +957,7 @@ Optional arguments:
-v, --version prints version information and exits

foo@bar:/home/dev/$ ./main hidden -h
Usage: hidden [--help] [--version] files
Usage: test hidden [--help] [--version] files

Positional arguments:
files [nargs: 0 or more]
Expand Down
2 changes: 1 addition & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ class ArgumentParser {
std::stringstream stream;

std::string curline("Usage: ");
curline += this->m_program_name;
curline += this->m_parser_path;
const bool multiline_usage =
this->m_usage_max_line_width < std::numeric_limits<std::size_t>::max();
const size_t indent_size = curline.size();
Expand Down
9 changes: 9 additions & 0 deletions test/test_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ TEST_CASE("multiline usage, break on mutex") {
" [--will-go-on-new-line]\n"
" [--on-a-dedicated-line]");
}

TEST_CASE("Usage output with subparser") {
argparse::ArgumentParser program("program");
argparse::ArgumentParser sub("sub");
program.add_subparser(sub);
// std::cout << "DEBUG:" << program.usage() << std::endl;
REQUIRE(program.usage() == "Usage: program [--help] [--version] {sub}");
REQUIRE(sub.usage() == "Usage: program sub [--help] [--version]");
}