summaryrefslogtreecommitdiff
path: root/modules/codec/examples/arg_parser.cpp
blob: 34a0b1ddecbc296899dfc1b5629b0eb3dafa1b28 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "../c++/args.hpp"

#include <iostream>

namespace sch {
using namespace saw::schema;

using ExArgStruct = Struct<
	Member<String, "file">,
	Member<UInt64, "threads">
>;

using ExArgs = Args<
	ExArgStruct,
	Tuple<>
>;
}

int main(int argc, char** argv){
	using namespace saw;

	data<sch::ExArgs, encode::Args> args_dat{argc, argv};
	codec<sch::ExArgs, encode::Args> args_codec;

	data<sch::ExArgs> nat_dat;

	auto eov = args_codec.decode(args_dat, nat_dat);
	if(eov.is_error()){
		auto& err = eov.get_error();
		std::cerr<<"\n[Error] "<<err.get_category();
		auto err_msg = err.get_message();
		if(not err_msg.empty()){
			std::cerr<<" - "<<err_msg;
		}
		std::cerr<<std::endl;
		return err.get_id();
	}

	std::cout<<"Success\n";
	std::cout<<"=======\n";
	std::cout<<"\nProgram: ";
	auto& prog = nat_dat.template get<"program">();
	for(uint64_t i = 0; i < prog.size().get(); ++i){
		std::cout<<prog.at(i);
	}
	std::cout<<"\n";
	
	auto& str_val = nat_dat.template get<"args">();
	{
		std::cout<<"File: ";
		auto& file_val = str_val.template get<"file">();
		for(uint64_t i = 0; i < file_val.size().get(); ++i){
			std::cout<<file_val.at(i);
		}
		std::cout<<"\n";
	}
	{
		std::cout<<"Threads: ";
		auto& threads_val = str_val.template get<"threads">();
		std::cout<<threads_val.get();
		std::cout<<"\n";
	}
	auto& tup_val = nat_dat.template get<"positionals">();

	std::cout<<std::endl;
	return 0;
}