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

METAVAR, Improved help, Samples (Based on #187) #206

Merged
merged 4 commits into from
Sep 22, 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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ target_include_directories(argparse INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)

if(ARGPARSE_BUILD_SAMPLES)
add_subdirectory(samples)
endif()

if(ARGPARSE_BUILD_TESTS)
add_subdirectory(test)
Expand Down
88 changes: 55 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ catch (const std::runtime_error& err) {
}

if (program["--verbose"] == true) {
std::cout << "Verbosity enabled" << std::endl;
std::cout << "Verbosity enabled" << std::endl;
}
```

Expand Down Expand Up @@ -338,15 +338,15 @@ The square of 4 is 16

```
foo@bar:/home/dev/$ ./main --help
Usage: main [options] square
Usage: main [-h] [--verbose] square

Positional arguments:
square display the square of a given number
square display the square of a given number

Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
--verbose [default: false]
-h, --help shows help message and exits
-v, --version prints version information and exits
--verbose
```

You may also get the help message in string via `program.help().str()`.
Expand All @@ -357,30 +357,36 @@ You may also get the help message in string via `program.help().str()`.
information. `ArgumentParser::add_epilog` will add text after all other help output.

```cpp
argparse::ArgumentParser program("main");
#include <argparse/argparse.hpp>

int main(int argc, char *argv[]) {
argparse::ArgumentParser program("main");
program.add_argument("thing").help("Thing to use.").metavar("THING");
program.add_argument("--member").help("The alias for the member to pass to.").metavar("ALIAS");
program.add_argument("--verbose").default_value(false).implicit_value(true);

program.add_argument("thing")
.help("Thing to use.");
program.add_description("Forward a thing to the next member.");
program.add_epilog("Possible things include betingalw, chiz, and res.");
program.add_description("Forward a thing to the next member.");
program.add_epilog("Possible things include betingalw, chiz, and res.");

program.parse_args(argc, argv);
program.parse_args(argc, argv);

std::cout << program << std::endl;
std::cout << program << std::endl;
}
```

```console
foo@bar:/home/dev/$ ./main --help
Usage: main thing
Usage: main [-h] [--member ALIAS] [--verbose] THING

Forward a thing to the next member.

Positional arguments:
thing Thing to use.
THING Thing to use.

Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
-h, --help shows help message and exits
-v, --version prints version information and exits
--member ALIAS The alias for the member to pass to.
--verbose

Possible things include betingalw, chiz, and res.
```
Expand Down Expand Up @@ -710,12 +716,14 @@ int main(int argc, char *argv[]) {

// git add subparser
argparse::ArgumentParser add_command("add");
add_command.add_description("Add file contents to the index");
add_command.add_argument("files")
.help("Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files.")
.remaining();

// git commit subparser
argparse::ArgumentParser commit_command("commit");
commit_command.add_description("Record changes to the repository");
commit_command.add_argument("-a", "--all")
.help("Tell the command to automatically stage files that have been modified and deleted.")
.default_value(false)
Expand All @@ -726,6 +734,7 @@ int main(int argc, char *argv[]) {

// git cat-file subparser
argparse::ArgumentParser catfile_command("cat-file");
catfile_command.add_description("Provide content or type and size information for repository objects");
catfile_command.add_argument("-t")
.help("Instead of the content, show the object type identified by <object>.");

Expand All @@ -734,7 +743,9 @@ int main(int argc, char *argv[]) {

// git submodule subparser
argparse::ArgumentParser submodule_command("submodule");
submodule_command.add_description("Initialize, update or inspect submodules");
argparse::ArgumentParser submodule_update_command("update");
submodule_update_command.add_description("Update the registered submodules to match what the superproject expects");
submodule_update_command.add_argument("--init")
.default_value(false)
.implicit_value(true);
Expand Down Expand Up @@ -763,41 +774,52 @@ int main(int argc, char *argv[]) {

```console
foo@bar:/home/dev/$ ./git --help
Usage: git [options] <command> [<args>]
Usage: git [-h] {add,cat-file,commit,submodule}

Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
-h, --help shows help message and exits
-v, --version prints version information and exits

Subcommands:
add Add file contents to the index
cat-file Provide content or type and size information for repository objects
commit Record changes to the repository
submodule Initialize, update or inspect submodules
add Add file contents to the index
cat-file Provide content or type and size information for repository objects
commit Record changes to the repository
submodule Initialize, update or inspect submodules

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

Add file contents to the index

Positional arguments:
files Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files.
files Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files.

Optional arguments:
-h, --help shows help message and exits
-v, --version prints version information and exits

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

Record changes to the repository

Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
-h, --help shows help message and exits
-v, --version prints version information and exits
-a, --all Tell the command to automatically stage files that have been modified and deleted.
-m, --message Use the given <msg> as the commit message.

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

Initialize, update or inspect submodules

Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
-h, --help shows help message and exits
-v, --version prints version information and exits

Subcommands:
update Update the registered submodules to match what the superproject expects
update Update the registered submodules to match what the superproject expects
```

When a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages.
Expand Down
2 changes: 1 addition & 1 deletion clang_format.bash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
clang-format -i include/argparse/*.hpp test/*.cpp
clang-format -i include/argparse/*.hpp test/*.cpp samples/*.cpp
Loading