Skip to content
Snippets Groups Projects
Verified Commit 7d33f3c0 authored by Jayesh Badwaik's avatar Jayesh Badwaik
Browse files

[wip]

parent b8da3d5d
No related branches found
No related tags found
No related merge requests found
Pipeline #162172 failed
Showing
with 567 additions and 0 deletions
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_CMDLINE_HPP
#define KAIRA_CMDLINE_HPP
#include <kaira/error.hpp>
#include <kaira/flag.hpp>
#include <nlohmann/json.hpp>
namespace kaira {
class cmdline {
public:
using value_type = nlohmann::json;
public:
cmdline() = default;
public:
auto add_flag(flag const& flag) -> error;
auto flag_array_all() const noexcept -> std::vector<flag> const&;
auto flag_array_set() const noexcept -> std::vector<flag>;
auto flag_array_unset() const noexcept -> std::vector<flag>;
private:
value_type cmdline_{nlohmann::json()};
std::vector<std::string> residue_;
};
} // namespace kaira
#endif // KAIRA_CMDLINE_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_DETAIL_STRING_HPP
#define KAIRA_DETAIL_STRING_HPP
#include <string>
#include <vector>
namespace kaira::detail {
/**
* @brief Reflow text to fit a specified column width
*
* The function takes in a string and a column width and reflows the string to fit the column width.
* It does so by inserting newline character in the appropriate places in the string. The resulting
* string is then split up at the newlines into an
* [std::vector](https://en.cppreference.com/w/cpp/container/vector) of
* [std::string](https://en.cppreference.com/w/cpp/string/basic_string)
* which is then returned to the user. The function preserves the newlines in the input string. The
* newlines are not counted towards the column width.
*
* The function tries to preserve the words in the input string. However, this is not guaranteed, in
* case the words are split, a hyphen is inserted at the end of the line and the word is continued
* on the next line. The hyphen IS counted towards the column width. The exact details of the
* decision on when to split a word are not specified yet.
*
* @param input The string to reflow
* @param ncol The column width
* @return `std::vector<std::string>`
*
*/
auto reflow_string(std::string const& input, std::size_t ncol) -> std::vector<std::string>;
auto split_string_by_whitespace(std::string const& input) -> std::vector<std::string>;
} // namespace kaira::detail
#endif // KAIRA_DETAIL_STRING_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_FLAG_HPP
#define KAIRA_FLAG_HPP
#include <string>
#include <utility>
namespace kaira {
class flag {
public:
flag() = default;
public:
flag(std::string name, std::string description, std::string key)
: key_(std::move(key)), name_(std::move(name)), description_(std::move(description))
{
}
public:
auto key() const noexcept -> std::string const& { return key_; }
auto name() const noexcept -> std::string const& { return name_; }
auto description() const noexcept -> std::string const& { return description_; }
private:
std::string key_;
std::string name_;
std::string description_;
};
auto operator==(flag const& lhs, flag const& rhs) noexcept -> bool;
auto operator!=(flag const& lhs, flag const& rhs) noexcept -> bool;
} // namespace kaira
#endif // KAIRA_FLAG_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_FLAG_PARSER_HPP
#define KAIRA_FLAG_PARSER_HPP
#include <string>
#include <utility>
namespace kaira::flag {
class parser {
public:
parser() = default;
parser(std::string name, std::string description, std::string key);
public:
auto key() const noexcept -> std::string const& { return key_; }
auto name() const noexcept -> std::string const& { return name_; }
auto description() const noexcept -> std::string const& { return description_; }
private:
std::string key_;
std::string name_;
std::string description_;
};
auto operator==(parser const& lhs, parser const& rhs) noexcept -> bool;
auto operator!=(parser const& lhs, parser const& rhs) noexcept -> bool;
} // namespace kaira::flag
#endif // KAIRA_FLAG_PARSER_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_FLAG_VALUE_HPP
#define KAIRA_FLAG_VALUE_HPP
#include <string>
namespace kaira::flag {
class value {
public:
value() = default;
value(std::string key, std::string value);
public:
auto key() const noexcept -> std::string const&;
auto value() const noexcept -> std::string const&;
private:
std::string value_;
std::string key_;
};
} // namespace kaira::flag
#endif // KAIRA_FLAG_VALUE_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_FORMATTER_DEFAULT_HELP_HPP
#define KAIRA_FORMATTER_DEFAULT_HELP_HPP
#include <kaira/parser.hpp>
namespace kaira::formatter {
} // namespace kaira::formatter
#endif // KAIRA_FORMATTER_DEFAULT_HELP_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_FORMATTER_FORMATTER_HPP
#define KAIRA_FORMATTER_FORMATTER_HPP
#include <cstddef>
#include <kaira/parser.hpp>
#include <limits>
namespace kaira::formatter {
struct config {
std::size_t ncol{std::numeric_limits<std::size_t>::max()};
std::size_t indent{2};
};
auto to_string(parser const& parser) -> std::string;
auto to_string(parser const& parser, config const& config) -> std::string;
} // namespace kaira::formatter
#endif // KAIRA_FORMATTER_FORMATTER_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_INPUT_HPP
#define KAIRA_INPUT_HPP
#include <kaira/error.hpp>
#include <kaira/input_kind.hpp>
#include <kaira/input_traits.hpp>
namespace kaira {
template <input_kind Kind>
class input {
public:
using input_type = typename input_traits<Kind>::return_type;
using parser_type = typename input_traits<Kind>::parser_type;
public:
input() = default;
input(input_type const& input, parser_type const& parser) : input_{input}, parser_{parser} {}
public:
auto value() const noexcept -> input_type const&;
auto kind() const noexcept -> input_kind const&;
auto key() const noexcept -> std::string const&
requires(Kind != input_kind::mvargument or Kind != input_kind::svargument);
private:
input_kind kind_{Kind};
input_type input_;
parser_type parser_;
};
} // namespace kaira
#endif // KAIRA_INPUT_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef KAIRA_PARSER_HPP
#define KAIRA_PARSER_HPP
#include <kaira/error.hpp>
#include <kaira/flag.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace kaira {
class parser {
public:
parser() = default;
parser(std::string name, std::string description);
public:
auto add_flag(flag const& flag) -> error;
public:
auto is_empty() const noexcept -> bool;
auto help() const -> nlohmann::json;
private:
std::vector<flag> flag_array_;
std::string name_;
std::string description_;
};
} // namespace kaira
#endif // KAIRA_PARSER_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#ifndef NOLA_CONTAINS_HPP
#define NOLA_CONTAINS_HPP
#include <functional>
namespace nola {
template <std::ranges::input_range Range>
auto contains(Range&& r, typename std::remove_cvref_t<Range>::value_type const& value)
{
auto const it_found = std::find(std::ranges::begin(r), std::ranges::end(r), value);
return it_found != std::ranges::end(r);
}
} // namespace nola
#endif // NOLA_CONTAINS_HPP
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <iterator>
#include <kaira/detail/string.hpp>
#include <sstream>
namespace kaira::detail {
auto split_string_by_whitespace(std::string const& input) -> std::vector<std::string>
{
std::istringstream buffer(input);
auto result = std::vector<std::string>(
std::istream_iterator<std::string>(buffer), std::istream_iterator<std::string>());
return result;
}
auto reflow_string(std::string const& input, std::size_t ncol) -> std::vector<std::string>
{
auto word_array = split_string_by_whitespace(input);
auto result = std::vector<std::string>();
auto it = std::begin(word_array);
while (it != std::end(word_array)) {
auto current_line = std::string();
while (current_line.size() < ncol and it != std::end(word_array)) {
if (current_line.size() + it->size() + 1 < ncol) {
current_line += " " + *it;
++it;
}
else {
break;
}
}
result.push_back(current_line);
}
return result;
}
} // namespace kaira::detail
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <kaira/flag.hpp>
namespace kaira {
auto operator==(flag const& lhs, flag const& rhs) noexcept -> bool
{
return lhs.key() == rhs.key();
}
auto operator!=(flag const& lhs, flag const& rhs) noexcept -> bool
{
return !(lhs == rhs);
}
} // namespace kaira
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <kaira/formatter/formatter.hpp>
namespace kaira::formatter {
auto to_string(parser const& parser) -> std::string
{
return to_string(parser, config{});
}
auto to_string(parser const& parser, [[maybe_unused]] config const& config) -> std::string
{
auto result = std::string();
// Generate Usage Line
result += "usage: " + std::string{parser.help()["name"]};
result += "\n\n";
// Generate Description
result += std::string{parser.help()["description"]};
return result;
}
} // namespace kaira::formatter
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <kaira/error.hpp>
#include <kaira/parser.hpp>
#include <nola/contains.hpp>
#include <utility>
namespace kaira {
auto parser::is_empty() const noexcept -> bool
{
return flag_array_.empty();
}
parser::parser(std::string name, std::string description)
: name_(std::move(name)), description_(std::move(description))
{
}
auto parser::add_flag(flag const& flag) -> error
{
if (nola::contains(flag_array_, flag)) {
return error::unknown_error;
}
flag_array_.push_back(flag);
return error::success;
}
auto parser::help() const -> nlohmann::json
{
auto help = nlohmann::json{};
help["name"] = name_;
help["description"] = description_;
return help;
}
} // namespace kaira
{
"copyright" :
{
"license" : "Apache-2.0",
"author_array" :
[
{
"year" : "2023",
"name" : "Jayesh Badwaik",
"email" : "j.badwaik@fz-juelich.de"
}
]
},
"header_array" : [ ],
"typename" : "kaira::error",
"underlying_type" : "std::size_t",
"option_map" :
{
"success" : "0",
"unknown_error" : "1"
},
"default_option" : "success"
}
{
"copyright" :
{
"license" : "Apache-2.0",
"author_array" :
[
{
"year" : "2023",
"name" : "Jayesh Badwaik",
"email" : "j.badwaik@fz-juelich.de"
}
]
},
"header_array" : [ ],
"typename" : "kaira::input_kind",
"underlying_type" : "std::size_t",
"option_map" :
{
"flag" : "1",
"svoption" : "2",
"mvoption" : "3",
"svargument" : "4",
"mvargument" : "5"
},
"default_option" : "flag"
}
...@@ -33,6 +33,7 @@ add_subdirectory(compile) ...@@ -33,6 +33,7 @@ add_subdirectory(compile)
add_subdirectory(smoke) add_subdirectory(smoke)
add_subdirectory(unit) add_subdirectory(unit)
add_subdirectory(functional)
add_subdirectory(integration) add_subdirectory(integration)
add_subdirectory(acceptance) add_subdirectory(acceptance)
......
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <catch2/catch_test_macros.hpp>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <kaira/detail/string.hpp>
#include <testmol/compat/catch_main.hpp>
#include <iostream>
TESTMOL_CATCH_MAIN("test/functional/kaira/detail/reflow_string")
TEST_CASE("test reflow", "[all]")
{
auto const filepath = group_prefix.input() + "/usecase.01.txt";
std::cout << filepath << std::endl;
REQUIRE(std::filesystem::exists(filepath));
using isbi_type = std::istreambuf_iterator<char>;
auto ifstream = std::ifstream(filepath);
auto const input_string = std::string(isbi_type(ifstream), isbi_type());
std::size_t const text_width = 80;
std::size_t const real_text_width = text_width + 1;
auto const reflowed_text = kaira::detail::reflow_string(input_string, text_width);
for (auto const& line : reflowed_text) {
REQUIRE(line.size() <= real_text_width);
std::cout << line << std::endl;
}
}
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <catch2/catch_test_macros.hpp>
#include <iostream>
#include <kaira/formatter/formatter.hpp>
#include <kaira/parser.hpp>
#include <testmol/compat/catch_main.hpp>
TESTMOL_CATCH_MAIN("test/unit/cpp/kaira/empty.t.cpp")
TEST_CASE("testing empty parser", "[all]")
{
auto const parser = kaira::parser();
REQUIRE(parser.is_empty());
}
TEST_CASE("testing empty parser help", "[all]")
{
auto const parser = kaira::parser("kaira", "a command line parser library");
auto const help = parser.help();
REQUIRE(help["name"] == "kaira");
REQUIRE(help["description"] == "a command line parser library");
auto const help_string = kaira::formatter::to_string(parser);
std::cout << help_string << std::endl;
}
TEST_CASE("testing parser with a flag", "[all]")
{
auto const parser = kaira::parser("kaira", "a command line parser library");
}
// -------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: (C) 2022 Jayesh Badwaik <j.badwaik@fz-juelich.de>
// -------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
auto main(int argc, char** argv) -> int
{
auto const arg_array = std::vector<std::string>(argv, argv + argc);
for (auto const& arg : arg_array) {
std::cout << arg << std::endl;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment