C++Spec 1.0.0
BDD testing for C++
Loading...
Searching...
No Matches
argparse.hpp
1#pragma once
2
3#include <argparse/argparse.hpp>
4#include <fstream>
5#include <string>
6#include <string_view>
7#include "formatters/junit_xml.hpp"
9#include "formatters/tap.hpp"
11#include "runner.hpp"
12
13namespace CppSpec {
14
15inline std::string file_name(std::string_view path) {
16 std::string_view file = path;
17 for (size_t i = 0; i < path.size(); ++i) {
18 if (path[i] == '/') {
19 file = &path[i + 1];
20 }
21 }
22 return std::string{file};
23}
24
25inline Runner parse(int argc, char** const argv) {
26 std::filesystem::path executable_path = argv[0];
27 std::string executable_name = executable_path.filename().string();
28 argparse::ArgumentParser program{executable_name};
29
30 program.add_argument("-f", "--format")
31 .default_value(std::string{"p"})
32 .choices("progress", "p", "tap", "t", "detail", "d", "junit", "j")
33 .required()
34 .help("set the output format");
35
36 program.add_argument("--output-junit").help("output JUnit XML to the specified file").default_value(std::string{});
37 program.add_argument("--verbose").help("increase output verbosity").flag();
38
39 try {
40 program.parse_args(argc, argv);
41 } catch (const std::runtime_error& err) {
42 std::cerr << err.what() << std::endl;
43 std::cerr << program;
44 std::exit(1);
45 }
46
47 auto format_string = program.get<std::string>("--format");
48 std::shared_ptr<Formatters::BaseFormatter> formatter;
49 if (format_string == "d" || format_string == "detail" || program["--verbose"] == true) {
50 formatter = std::make_shared<Formatters::Verbose>();
51 } else if (format_string == "p" || format_string == "progress") {
52 formatter = std::make_shared<Formatters::Progress>();
53 } else if (format_string == "t" || format_string == "tap") {
54 formatter = std::make_shared<Formatters::TAP>();
55 } else if (format_string == "j" || format_string == "junit") {
56 formatter = std::make_shared<Formatters::JUnitXML>();
57 } else {
58 std::cerr << "Unrecognized format type" << std::endl;
59 std::exit(-1);
60 }
61
62 auto junit_output_filepath = program.get<std::string>("--output-junit");
63 if (!junit_output_filepath.empty()) {
64 // open file stream
65 auto* file_stream = new std::ofstream(junit_output_filepath);
66 auto junit_output = std::make_shared<Formatters::JUnitXML>(*file_stream, false);
67 return Runner{formatter, junit_output};
68 }
69 return Runner{formatter};
70}
71} // namespace CppSpec
A collection of Descriptions that are run in sequence.
Definition runner.hpp:16