summaryrefslogtreecommitdiff
path: root/test/argparser_test.cc
blob: 842b21462ef78c4e069e295a0e1469a81e59eaa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <uunit.h>

#include <argparser.h>

#include <iostream>
#include <string>

class ArgParserTest
	: public uUnit
{
public:
	ArgParserTest()
	{
		uTEST(ArgParserTest::test);
	}

	void test()
	{
		const char* argv[] =
		{
			"app-name",
			"-x",
			"42"
		};
		int argc = sizeof(argv)/sizeof(*argv);
		ArgParser<int, std::optional<int>> args;

		args.add("-x", "--some-x",
		         std::function([](int i)
		         {
			         std::cout << "int: " << i << "\n";
			         return 0;
		         }),
		         "Helptext for -x,--some-x");

		args.add("-X", "--opt-x",
		         std::function([](std::optional<int> i)
		         {
			         std::cout << "opt<int>: " << (i?std::to_string(*i):"none") << "\n";
			         return 0;
		         }),
		         "Helptext for -X,--opt-x");

		args.parse(argc, argv);
	}
};

// Registers the fixture into the 'registry'
static ArgParserTest test;