Skip to content

Commit 7795b6a

Browse files
authored
C++17 std::filesystem::path support added (#447)
1 parent f029892 commit 7795b6a

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

include/cxxopts.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ THE SOFTWARE.
7373
# define CXXOPTS_HAS_OPTIONAL
7474
# endif
7575
# endif
76+
# if __has_include(<filesystem>)
77+
# include <filesystem>
78+
# ifdef __cpp_lib_filesystem
79+
# define CXXOPTS_HAS_FILESYSTEM
80+
# endif
81+
# endif
7682
#endif
7783

7884
#define CXXOPTS_FALLTHROUGH
@@ -1074,6 +1080,15 @@ parse_value(const std::string& text, std::optional<T>& value)
10741080
}
10751081
#endif
10761082

1083+
#ifdef CXXOPTS_HAS_FILESYSTEM
1084+
inline
1085+
void
1086+
parse_value(const std::string& text, std::filesystem::path& value)
1087+
{
1088+
value.assign(text);
1089+
}
1090+
#endif
1091+
10771092
inline
10781093
void parse_value(const std::string& text, char& c)
10791094
{

test/options.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,27 @@ TEST_CASE("std::optional", "[optional]") {
732732
}
733733
#endif
734734

735+
#ifdef CXXOPTS_HAS_FILESYSTEM
736+
TEST_CASE("std::filesystem::path", "[path]") {
737+
std::filesystem::path path;
738+
cxxopts::Options options("path", " - tests path");
739+
options.add_options()
740+
("path", "a path", cxxopts::value<std::filesystem::path>(path));
741+
742+
Argv av({"path", "--path", "Hello World.txt"});
743+
744+
auto** argv = av.argv();
745+
auto argc = av.argc();
746+
747+
REQUIRE(path.empty());
748+
749+
options.parse(argc, argv);
750+
751+
REQUIRE(!path.empty());
752+
CHECK(path == "Hello World.txt");
753+
}
754+
#endif
755+
735756
TEST_CASE("Unrecognised options", "[options]") {
736757
cxxopts::Options options("unknown_options", " - test unknown options");
737758

@@ -877,6 +898,60 @@ TEST_CASE("Optional value", "[optional]")
877898
}
878899
#endif
879900

901+
#ifdef CXXOPTS_HAS_OPTIONAL
902+
TEST_CASE("std::filesystem::path value", "[path]")
903+
{
904+
cxxopts::Options options("options", "query as std::fileystem::path");
905+
options.add_options()
906+
("a", "Path", cxxopts::value<std::filesystem::path>())
907+
("b", "Path", cxxopts::value<std::filesystem::path>())
908+
("c", "Path", cxxopts::value<std::filesystem::path>())
909+
("d", "Path", cxxopts::value<std::filesystem::path>())
910+
("e", "Path", cxxopts::value<std::filesystem::path>())
911+
;
912+
913+
SECTION("Available") {
914+
Argv av({
915+
"available",
916+
"-a", "hello.txt",
917+
"-b", "C:\\Users\\JoeCitizen\\hello world.txt",
918+
"-c", "/home/joecitzen/hello world.txt",
919+
"-d", "../world.txt"
920+
});
921+
922+
auto** argv = av.argv();
923+
auto argc = av.argc();
924+
925+
auto result = options.parse(argc, argv);
926+
927+
CHECK(result.as_optional<std::filesystem::path>("a"));
928+
CHECK(result.as_optional<std::filesystem::path>("b"));
929+
CHECK(result.as_optional<std::filesystem::path>("c"));
930+
CHECK(result.as_optional<std::filesystem::path>("d"));
931+
CHECK(!result.as_optional<std::filesystem::path>("e"));
932+
933+
CHECK(result.as_optional<std::filesystem::path>("a") == "hello.txt");
934+
CHECK(result.as_optional<std::filesystem::path>("b") == "C:\\Users\\JoeCitizen\\hello world.txt");
935+
CHECK(result.as_optional<std::filesystem::path>("c") == "/home/joecitzen/hello world.txt");
936+
CHECK(result.as_optional<std::filesystem::path>("d") == "../world.txt");
937+
}
938+
939+
SECTION("Unavailable") {
940+
Argv av({
941+
"unavailable"
942+
});
943+
944+
auto** argv = av.argv();
945+
auto argc = av.argc();
946+
947+
auto result = options.parse(argc, argv);
948+
949+
CHECK(!result.as_optional<std::filesystem::path>("a"));
950+
}
951+
952+
}
953+
#endif
954+
880955
TEST_CASE("Initializer list with group", "[options]") {
881956
cxxopts::Options options("Initializer list group", " - test initializer list with group");
882957

0 commit comments

Comments
 (0)