1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-07 23:33:52 +00:00

Adding more docs

This commit is contained in:
Henry Fredrick Schreiner 2017-03-22 11:03:02 -04:00
parent a0b10b4c50
commit 18b3fa9dfe
3 changed files with 43 additions and 4 deletions

View File

@ -61,7 +61,7 @@ As you probably have guessed, the list of features above are all covered by this
To use, there are two methods:
1. Copy `CLI11.hpp` from the [most recent release][github-releases] into your include directory, and you are set. This is combined from the source files for every release.
1. Copy `CLI11.hpp` from the [most recent release][github-releases] into your include directory, and you are set. This is combined from the source files for every release. This includes the entire command parser library, but does not include separate utilities (like `Timer`, `AutoTimer`). The utilities are completely self contained and can be copied separately.
2. Checkout the repository and add as a subdirectory for CMake. You can use the CLI interface target. (CMake 3.4+ recommended)
To build the tests, checkout the repository and use CMake:
@ -237,6 +237,20 @@ app.add_option("--fancy-count", [](std::vector<std::string> val){
To contribute, open an [issue][github-issues] or [pull request][github-pull] on GitHub, or ask a question on [gitter][gitter-link].
## Utilities
There are a few other utilities that are often useful in CLI programming. These are in separate headers, and do not appear in `CLI11.hpp`, but are completely independent and can be used as needed. The `Timer`/`AutoTimer` class allows you to easily time a block of code, with custom print output. An example of usage:
```cpp
{
CLI::AutoTimer timer {"My Long Process", CLI::Timer::Big};
some_long_running_process();
}
```
This will create a timer with a title (default: `Timer`), and will customize the output using the predefined `Big` output (default: `Simple`). Because it is an `AutoTimer`, it will print out the time elapsed when the timer is destroyed at the end of the block. If you use `Timer` instead, you can use `to_string` or `std::cout << timer << std::endl;` to print the time. The print function can be any function that takes two strings, the title and the time, and returns a formatted
string for printing.
## Other libraries
If you use the [`rang`][rang-link] library to add color to your terminal in a safe, multi-platform way, you can combine it with CLI11 nicely:

View File

@ -9,6 +9,8 @@ The main classes are:
|CLI::Option | Options, stored in the app |
|CLI::App | The main application or subcommands |
|CLI::ExitCodes | A scoped enum with exit codes |
|CLI::Timer | A timer class, only in CLI/Timer.hpp (not CLI11.hpp) |
|CLI::AutoTimer | A timer that prints on deletion |
Groups of related topics:

View File

@ -12,29 +12,44 @@ namespace CLI {
class Timer {
protected:
/// This is a typedef to make clocks easier to use
typedef std::chrono::high_resolution_clock clock;
/// This typedef is for points in time
typedef std::chrono::time_point<clock> time_point;
/// This is the type of a printing function, you can make your own
typedef std::function<std::string(std::string, std::string)> time_print_t;
/// This is the title of the timer
std::string title_;
/// This is the starting point (when the timer was created)
time_point start_;
/// This is the function that is used to format most of the timing message
time_print_t time_print_;
public:
/// Standard print function, this one is set by default
static std::string Simple(std::string title, std::string time) {
return title + ": " + time;
}
/// This is a fancy print function with --- headers
static std::string Big(std::string title, std::string time) {
return "-----------------------------------------\n"
+ title + " | Time = " + time + "\n"
+ "| " + title + " | Time = " + time + "\n"
+ "-----------------------------------------";
}
public:
/// Standard constructor, can set title and print function
Timer(std::string title="Timer", time_print_t time_print = Simple)
: title_(title), time_print_(time_print), start_(clock::now()) {}
/// This formats the numerical value for the time string
std::string make_time_str() const {
time_point stop = clock::now();
std::chrono::duration<double, std::milli> elapsed = stop - start_;
@ -53,15 +68,22 @@ public:
return std::to_string(int(time*1000)) + " s";
// LCOV_EXCL_END
}
/// This is the main function, it creates a string
std::string to_string() const {
return time_print_(title_, make_time_str());
}
};
/// This class prints out the time upon destruction
class AutoTimer : public Timer {
public:
using Timer::Timer;
/// Reimplementing the constructor is required in GCC 4.7
AutoTimer(std::string title="Timer", time_print_t time_print = Simple)
: Timer(title, time_print) {}
// GCC 4.7 does not support using inheriting constructors.
/// This desctructor prints the string
~AutoTimer () {
std::cout << to_string() << std::endl;
}
@ -69,6 +91,7 @@ public:
}
/// This prints out the time if shifted into a std::cout like stream.
std::ostream & operator<< (std::ostream& in, const CLI::Timer& timer) {
return in << timer.to_string();
}