Skip to content

Commit 54190c6

Browse files
committed
Rename opt.argSrcRelative to opt.resolve
1 parent 28f4365 commit 54190c6

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

docs/reference.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ the user will be prompted for it. Use Cli::fPrompt* flags to adjust behavior.
244244
| Causes a check that the option value was set during parsing, and reports
245245
badUsage() if it wasn't.
246246

247+
| opt.resolve
248+
| When srcType is ArgSrc::kFile values are interpreted as file paths and, if
249+
the argument also has kFile srcType, resolved relative to the file source of
250+
the argument. Other source types are not supported.
251+
247252
| opt.<<guide.adoc#si-units, siUnits>>
248253
| Removes the symbol and, if SI unit prefixes (m, k, ki, M, Mi, etc) are
249254
present, multiplies by the corresponding factor.

libs/dimcli/cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ void Cli::defParseAction(Cli & cli, OptBase & opt, const string & val) {
12151215

12161216
//===========================================================================
12171217
// static
1218-
void Cli::argSrcResolveAction(Cli & cli, OptBase & opt, const string & rel) {
1218+
void Cli::argSrcFileRelAction(Cli & cli, OptBase & opt, const string & rel) {
12191219
string val = rel;
12201220
#ifdef DIMCLI_LIB_FILESYSTEM
12211221
if (opt.srcType() == ArgSrc::kFile) {

libs/dimcli/cli.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ class DIMCLI_LIB_DECL Cli {
10291029
OptBase & opt,
10301030
const std::string & val
10311031
);
1032-
static void argSrcResolveAction(
1032+
static void argSrcFileRelAction(
10331033
Cli & cli,
10341034
OptBase & opt,
10351035
const std::string & val
@@ -1896,7 +1896,10 @@ class Cli::OptShim : public OptBase {
18961896
int flags = 0 // Cli::fPrompt* flags
18971897
);
18981898

1899-
A & argSrcRelative();
1899+
// When srcType is ArgSrc::kFile values are interpreted as file paths and,
1900+
// if the argument also has kFile srcType, resolved relative to the file
1901+
// source of the argument. Other source types are not supported.
1902+
A & resolve(ArgSrc::Type srcType);
19001903

19011904
// Function signature of actions that are tied to options.
19021905
using ActionFn = void(Cli & cli, A & opt, const std::string & val);
@@ -2370,8 +2373,10 @@ A & Cli::OptShim<A, T>::prompt(const std::string & msg, int flags) {
23702373

23712374
//===========================================================================
23722375
template <typename A, typename T>
2373-
A & Cli::OptShim<A, T>::argSrcRelative() {
2374-
return parse(Cli::argSrcResolveAction).valueDesc("FILE");
2376+
A & Cli::OptShim<A, T>::resolve(ArgSrc::Type srcType) {
2377+
if (srcType == ArgSrc::kFile)
2378+
parse(Cli::argSrcFileRelAction).valueDesc("FILE");
2379+
return static_cast<A &>(*this);
23752380
}
23762381

23772382

@@ -2496,7 +2501,7 @@ inline bool Cli::Opt<T>::parseValue(const std::string & value) {
24962501
template <>
24972502
inline // static
24982503
void Cli::Opt<DIMCLI_LIB_FILESYSTEM_PATH>::initConfig(Cli &) {
2499-
argSrcRelative();
2504+
resolve(ArgSrc::kFile);
25002505
}
25012506
#endif
25022507

@@ -2715,7 +2720,7 @@ inline bool Cli::OptVec<T>::parseValue(const std::string & value) {
27152720
template <>
27162721
inline // static
27172722
void Cli::OptVec<DIMCLI_LIB_FILESYSTEM_PATH>::initConfig(Cli &) {
2718-
argSrcRelative();
2723+
resolve(ArgSrc::kFile);
27192724
}
27202725
#endif
27212726

tests/cli/clitest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,12 @@ static void valueTests() {
580580
// parse manufactured relative file argument
581581
{
582582
cli = {};
583-
auto & opt = cli.opt<string>("v").argSrcRelative();
583+
auto stype = Dim::Cli::ArgSrc::kFile;
584+
auto & opt = cli.opt<string>("v").resolve(stype);
585+
cli.optVec<string>("x").resolve(stype);
584586
EXPECT_PARSE(cli, "");
585587
EXPECT_EQUAL(*opt, "");
586-
auto rc = cli.parseValue(opt, Dim::Cli::ArgSrc::kFile, "a/b.rsp", "c");
588+
auto rc = cli.parseValue(opt, stype, "a/b.rsp", "c");
587589
EXPECT_EQUAL(rc, true);
588590
EXPECT_EQUAL(*opt, "a/c");
589591
}
@@ -2117,6 +2119,7 @@ static void filesystemTests() {
21172119
(void) cvt.toString(def, path);
21182120
EXPECT_EQUAL(def, "path");
21192121
cli.opt(&path, "path", path)
2122+
.valueDesc(cli.valueDesc<fs::path>())
21202123
.desc("std::filesystem::path");
21212124
EXPECT_PARSE(cli, "--path one");
21222125
EXPECT_EQUAL(path, "one");

0 commit comments

Comments
 (0)