Skip to content

Instantly share code, notes, and snippets.

@tshev
Last active June 22, 2019 14:03
Show Gist options
  • Save tshev/359f5a86b14a9179c86ff1815e15a0e6 to your computer and use it in GitHub Desktop.
Save tshev/359f5a86b14a9179c86ff1815e15a0e6 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <sgl/sgl.h>
#include <string>
#include <vector>
template <typename T>
void print_status(const char* name, const T& x, bool v) {
if (v) {
std::cout << "Error " << name;
} else {
std::cout << "Success: " << name << " " << x;
}
std::cout << std::endl;
}
template<typename T>
void describe(const sgl::v1::argparser& ap, const char* name) {
auto [option, err] = ap.get<T>(name);
print_status(name, option, err);
}
int main(int argc, char* argv[]) {
sgl::v1::argparser ap(argc, argv);
std::cout << ap.command() << std::endl;
describe<int>(ap, "--x,-x");
describe<int>(ap, "--y,-y");
describe<bool>(ap, "-flto");
describe<bool>(ap, "--target-help");
describe<std::string_view>(ap, "-L");
describe<std::string_view>(ap, "-L");
std::cout << "direct arguments:" << std::endl;
auto range = ap.values();
sgl::v1::for_each(range.first, range.second, [](const auto& x) {
std::cout << "- " << std::string_view(x.first, x.second - x.first) << std::endl;
});
}
[tshev@localhost include]$ g++ main.cpp -I ./ -std=c++17 && ./a.out --x 12 -flto -L ../ main.cpp --target-help --z 21 -y 1
"./a.out" --x "12" -flto -L "../" "main.cpp" --target-help --z "21" -y "1"
Success: --x,-x 12
Success: --y,-y 1
Success: -flto 1
Success: --target-help 1
Success: -L ../
Success: -L ../
direct arguments:
- ./a.out
- main.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment