A simple, portable library for parsing command line arguments in C++.
Warning
This library kinda sucks. There are many problems worth fixing.
In this example there are only flags/options parsed. There is no help message, no license message and no subcommands.
#include "libcmd.hpp"
int main(int argc, char* argv[]) {
bool verbose = false;
std::string inputString = "";
int inputInteger = 0;
double inputDouble = 0.0;
CmdParserFrame pars (
argc,
argv,
{
Option(&verbose, {"--verbose"}),
Option(&inputString, {"-i", "--input"}, "input string"),
Option(&inputInteger, {"-n", "--number"}, "input integer"),
Option(&inputDouble, {"-d", "--double"}, "input double", {"SomeOtherOptionNotShownInUsage", "-R"})
}
);
pars.comfortDigest();
}
Note
CmdParserFrame
is jank... just use the examples below instead.
#include "libcmd.hpp"
int main(int argc, char* argv[]) {
bool verbose = false;
std::string inputString = "";
int inputInteger = 0;
double inputDouble = 0.0;
CmdParser pars (
argc,
argv,
{
Option(&verbose, {"--verbose"}),
Option(&inputString, {"-i", "--input"}, "input string"),
Option(&inputInteger, {"-n", "--number"}, "input integer"),
Option(&inputDouble, {"-d", "--double"}, "input double", {"SomeOtherOptionNotShownInUsage", "-R"})
},
"programname",
"",
"This is the description people see, when your programs usage is shown.",
"Here you can input your copyright notice and license information (for example of all libraries used)"
);
pars.comfortDigest();
}
The above code results in:
This is the description people see, when your programs usage is shown.
Usage for: programname
Flags: -h --help Show this message.
--license Print licenses.
--verbose
Options: -i --input input string
-n --number input integer
-d --double input double
#include "libcmd.hpp"
int main(int argc, char* argv[]) {
bool verbose = false;
std::string inputString = "";
int inputInteger = 0;
double inputDouble = 0.0;
bool wasSubcommandCalled = false;
CmdParser pars (
argc,
argv,
{
Option(&verbose, {"--verbose"}),
Option(&inputString, {"-i", "--input"}, "input string"),
Option(&inputInteger, {"-n", "--number"}, "input integer"),
Option(&inputDouble, {"-d", "--double"}, "input double", {"SomeOtherOptionNotShownInUsage", "-R"})
},
"programname",
"Description of program shown above help message of all subcommands. (make it short)",
"This is the description people see, when your programs usage is shown.",
"Here you can input your copyright notice and license information (for example of all libraries used)",
{
SubCommand(
{
// Options like above
},
"mysubcommand",
&wasSubcommandCalled,
{
// sub SubCommands of your SubCommand
},
"Description of your subcommand (1 line)"
),
// more SubCommands
}
);
pars.comfortDigest();
}
This results in:
This is the description people see, when your programs usage is shown.
Usage for: programname
Flags: -h --help Show this message.
--license Print licenses.
--verbose
Options: -i --input input string
-n --number input integer
-d --double input double
Subcmd: mysubcommand Description of your subcommand (1 line)
For more help: programname mysubcommand --help
Your can have as many SubCommands as you like. SubCommands also can have SubCommands, LABEL: which also can have SubCommands, goto LABEL;
- The files "libcmd.hpp", "testlibcmd.cpp" are licensed under the ISC License.
- The file "example.cpp" and the examples above are under the terms of CC0 1.0.
- Fork it (https://github.com/WyvernIXTL/libcmd/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Add your feature.
- Lint your code (cppcheck)(nvmd cpcheck kills me)
- Head to tests and build via cmake (
cd tests && cmake . -B ./build/ && cd ./build/
) - Build the tests and test (on windows
msbuild ./Project.sln && ./Debug/tests.exe
) - Commit your changes (
git commit -S -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Adam McKellar - creator and maintainer