put everything in the argparse namespace

This commit is contained in:
Carl Pearson
2020-04-01 13:59:50 -05:00
parent a132c7d925
commit 193219ac50
4 changed files with 46 additions and 57 deletions

View File

@@ -16,7 +16,7 @@ Download the latest [`argparse.hpp`](https://raw.githubusercontent.com/cwpearson
int main(int argc, char **argv) {
// A parser object
Parser p;
argparse::Parser p;
// Program data corresponding to flags, options, and positional arguments
bool verbose = false;
@@ -70,7 +70,7 @@ They are invoked like `--long-opt value` (not `--long-opt=value`).
```c++
Parser p;
argparse::Parser p;
p.add_option(var1, "--long-opt")
```
@@ -81,7 +81,7 @@ The boolean variable is ALWAYS set to `true` if the flag is found.
They are invoked like `--long-flag` (not `--long-flag=true` or `--long-flag true`).
```c++
Parser p;
argparse::Parser p;
p.add_option(flag1, "--long-flag")
p.add_option(flag2, "--antother-flag", "-s");
```
@@ -94,7 +94,7 @@ Use `required()` to require them.
`add_positional()` returns a `PosnlBase *` that may be queried with `found()` to see if an optional positional argument was found.
```c++
Parser p;
argparse::Parser p;
p.add_positional(var1)->required();
auto something = p.add_positional(var2);
if (something->found()) {
@@ -107,7 +107,7 @@ Here, the first `--` is the value for `--option` and will be in `s1`.
The second `--` marks the beginning of positional arguments. and the string `aa` will be in `s2`.
```c++
Parser p;
argparse::Parser p;
std::string s1, s2;
p.add_option(s1, "--option");
p.add_positional(s2);
@@ -123,7 +123,7 @@ $ ./myexe --option -- -- aa
## Parsing
```c++
Parser p;
argparse::Parser p;
// set up flags, arguments, and options
p.parse(argc, argv);
```

View File

@@ -3,7 +3,7 @@
int main(int argc, char **argv) {
// A parser object
Parser p;
argparse::Parser p;
// Program data corresponding to flags, options, and positional arguments
bool verbose = false;

View File

@@ -1,12 +1,11 @@
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace argparse {
class OptionBase {
public:
@@ -21,9 +20,7 @@ template <typename T> class Option : public OptionBase {
public:
Option(T &val, const std::string &l) : long_(l), val_(&val) {}
void set_val(const std::string &val) override { set_val((T *)nullptr, val); }
const std::string &long_str() override {
return long_;
}
const std::string &long_str() override { return long_; }
private:
void set_val(size_t *, const std::string &val) { // convert to size_t
@@ -50,20 +47,17 @@ class Flag {
bool *val_;
public:
Flag(bool &val, const std::string &l, const std::string &s) : long_(l), short_(s), val_(&val) {}
Flag(bool &val, const std::string &l, const std::string &s)
: long_(l), short_(s), val_(&val) {}
const std::string &long_str() const noexcept { return long_; }
const std::string &short_str() const noexcept { return short_; }
void set() const noexcept { *val_ = true; }
void help(const std::string &s) {
help_ = s;
}
void help(const std::string &s) { help_ = s; }
const std::string &help_str() const noexcept {
return help_;
}
const std::string &help_str() const noexcept { return help_; }
};
class PosnlBase {
@@ -91,12 +85,12 @@ public:
// use nullpointer type to disambiguate call
// https://stackoverflow.com/questions/5512910/explicit-specialization-of-template-class-member-function
void set_val(const std::string &val) {
void set_val(const std::string &val) {
found_ = true;
set_val((T *)nullptr, val);
set_val((T *)nullptr, val);
}
bool found() override {return found_; }
bool found() override { return found_; }
private:
// https://stackoverflow.com/questions/5512910/explicit-specialization-of-template-class-member-function
@@ -124,8 +118,8 @@ class Parser {
std::string description_;
bool noUnrecognized_; // error on unrecognized flags / opts
bool help_; // help has been requested
bool consume_; // remove consumed values from argc, argv
bool help_; // help has been requested
bool consume_; // remove consumed values from argc, argv
std::vector<OptionBase *> opts_;
std::vector<Flag> flags_;
@@ -159,26 +153,26 @@ class Parser {
}
public:
Parser() : noUnrecognized_(false), help_(false), consume_(true) {
add_flag(help_, "--help", "-h")->help("Print help message");
}
Parser(const std::string &description) : description_(description), noUnrecognized_(false), help_(false), consume_(true) {
}
Parser(const std::string &description)
: description_(description), noUnrecognized_(false), help_(false),
consume_(true) {
add_flag(help_, "--help", "-h")->help("Print help message");
}
}
bool parse(int &argc, char ** argv) {
bool parse(int &argc, char **argv) {
std::vector<char*> newArgv;
std::vector<char *> newArgv;
if (argc > 0) {
newArgv.push_back(argv[0]);
}
size_t pi = 0; // positional argument position
size_t pi = 0; // positional argument position
bool optsOkay = true; // okay to interpret as opt/flag
for (int i = 1; i < argc; ++i) {
// try interpreting as a flag or option if it looks like one
if (optsOkay && starts_with(argv[i], "-")) {
// '--' indicates only positional arguments follow
@@ -208,8 +202,8 @@ public:
++pi;
} else {
newArgv.push_back(argv[i]);
std::cerr << "encountered unexpected positional argument " << pi << ": " << argv[i]
<< "\n";
std::cerr << "encountered unexpected positional argument " << pi
<< ": " << argv[i] << "\n";
}
}
}
@@ -228,7 +222,6 @@ public:
}
}
return true;
};
@@ -265,18 +258,14 @@ public:
}
/*! \brief error on unrecognized flags and options
*/
void no_unrecognized() {
noUnrecognized_ = true;
}
*/
void no_unrecognized() { noUnrecognized_ = true; }
/*! \brief don't modify argc/argv
*/
void no_consume() {
consume_ = false;
}
*/
void no_consume() { consume_ = false; }
bool need_help() const noexcept {
return help_;
}
bool need_help() const noexcept { return help_; }
};
} // namespace argparse

View File

@@ -15,7 +15,7 @@ TEST_CASE("argparse") {
};
int argc = sizeof(argv) / sizeof(argv[0]);
Parser p;
argparse::Parser p;
bool campi = false;
size_t x;
@@ -45,7 +45,7 @@ TEST_CASE("argparse") {
SECTION("no args") {
char *argv[] = {nullptr};
int argc = sizeof(argv) / sizeof(argv[0]);
Parser p;
argparse::Parser p;
REQUIRE(p.parse(argc, argv));
}
@@ -56,7 +56,7 @@ TEST_CASE("argparse") {
};
int argc = sizeof(argv) / sizeof(argv[0]);
Parser p("a test program");
argparse::Parser p("a test program");
REQUIRE(p.parse(argc, argv));
}
@@ -64,7 +64,7 @@ TEST_CASE("argparse") {
char *argv[] = {"some-exe"};
int argc = sizeof(argv) / sizeof(argv[0]);
Parser p;
argparse::Parser p;
p.no_unrecognized();
REQUIRE(p.parse(argc, argv));
}
@@ -73,7 +73,7 @@ TEST_CASE("argparse") {
char *argv[] = {"some-exe", "-f"};
int argc = sizeof(argv) / sizeof(argv[0]);
Parser p;
argparse::Parser p;
p.no_unrecognized();
REQUIRE(false == p.parse(argc, argv));
}
@@ -85,7 +85,7 @@ TEST_CASE("argparse") {
std::string a;
std::string b;
Parser p;
argparse::Parser p;
p.add_positional(a)->required();
p.add_positional(b)->required();
REQUIRE(false == p.parse(argc, argv));
@@ -98,7 +98,7 @@ TEST_CASE("argparse") {
std::string a;
std::string b;
Parser p;
argparse::Parser p;
REQUIRE(true == p.parse(argc, argv));
REQUIRE(p.need_help());
@@ -112,7 +112,7 @@ TEST_CASE("argparse") {
std::string a;
std::string b;
Parser p;
argparse::Parser p;
REQUIRE(true == p.parse(argc, argv));
REQUIRE(p.need_help());
}
@@ -124,7 +124,7 @@ TEST_CASE("argparse") {
std::string a;
std::string b;
Parser p;
argparse::Parser p;
REQUIRE(true == p.parse(argc, argv));
REQUIRE(p.need_help());
}