CpParser
A Python argparse like C++ argument parser
Install / Use
/learn @aintea4/CpParserREADME
<a name="index-section"></a> Index
Here is a list of what you'll find in this README.md :
<a name="installation-section"></a> Installation
To compile the library, use setup.sh by doing in your terminal :
./setup.sh
<br>
Or you can do it by yourself by typing in your terminal :
[[ ! -d lib ]] && mkdir lib
g++ -c ./cpparser.cpp
ar cr libcpparser.a *.o
mv libcpparser.a lib/
<a name="compilation-section"></a> Compilation
To compile your program you will need to add some flags, just like this :
# for more informations type './compile.sh -h'
./compile.sh <your_file> [OTHER FLAGS]
<br><b>OR</b><br>
<br>You can use make if you feel like editing the source filename in the Makefile<br>
Here's an example with g++ compiler: <br>
g++ <your_file> cpparser.cpp -Iinclude
<a name="documentation-section"></a> Documentation
Each argument type has an example in example section <br></br>
There are 4 argument types (Default means the value when the argument is not called) :
-
Parser::STORE_ONE_VALUE:- Accessible by:
args["<argname>"].Stringorargs["<argname>"](C++ will try to convert it to a string) - Type:
std::string - Default to:
""<br>
- Accessible by:
-
Parser::STORE_MULTIPLE_VALUES- Accessible by:
args["<argname>"].Vector - Type:
std::vector<std::string> - Default to:
{}<br>
- Accessible by:
-
Parser::STORE_TRUE- Accessible by:
args["<argname>"].Boolorargs["<argname>"](C++ will try to convert it to a boolean) - Has type:
bool - Default to:
false<br>
- Accessible by:
-
Parser::STORE_FALSE- Accessible by:
args["<argname>"].Boolorargs["<argname>"](C++ will try to convert it to a boolean) - Type:
bool - Default to:
true
- Accessible by:
<a name="example-section"></a> Example
Here are 4 simple example of how works the cpParser library, with each argument type :
#include <iostream>
#include <string>
#include "cpparser.hpp"
int main(int argc, char* argv[]) {
Parser parser("Example for 'Parser::STORE_ONE_VALUE' argument type");
parser.addArgument(
"-n", // Short flag
"--name", // Long flag
"name", // Key with which you will access value
true, // true if the argument is required, false
// otherwise. Default to false if nothing given
Parser::STORE_ONE_VALUE, // Type of stored argument
{"ain", "tea"}, // Values accepted for the flag,
// if not given, all values are considered correct
"User name" // Description of argument
);
auto args = parser.parseArgs(argc, argv);
// the value is contained in `args["name"]` (c++ will convert it to the right type), but to enforce
// the type, `args["name"].String` is the way to go
std::cout << "Your name is " << args["name"] << std::endl;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include "cpparser.hpp"
int main(int argc, char* argv[]) {
Parser parser("Example for 'Parser::STORE_MULTIPLE_VALUES' argument type");
parser.addArgument(
"-u",
"--urls",
"urlList",
true,
Parser::STORE_MULTIPLE_VALUES,
"The list of URL"
);
auto args = parser.parseArgs(argc, argv);
for (std::string url: args["urlList"].Vector) {
std::cout << url << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <map>
#include "cpparser.hpp"
int main(int argc, char* argv[]) {
// It's the same for 'Parser::STORE_FALSE'
Parser parser("Example for 'Parser::STORE_TRUE' argument type");
parser.addArgument(
"-i",
"--ignore-case",
"ignorecase",
Parser::STORE_TRUE,
"Ignores case"
);
auto args = parser.parseArgs(argc, argv);
if (args["ignorecase"]) {
std::cout << "'Ignore case' flag was given" << std::endl;
} else {
std::cout << "'Ignore case' flag was not given" << std::endl;
}
return 0;
}
<a name="problems-section"></a> Problems
If you had any problem with compiling/using the library, or if you have any suggestions, please open an issue
Related Skills
node-connect
345.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
104.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
