Skip to content

Commit e3075bd

Browse files
committed
Add query subcommand
1 parent 1bee159 commit e3075bd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/pf.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,48 @@ class cmd_update {
359359
}
360360
};
361361

362+
class cmd_query {
363+
private:
364+
cli_common& _cli;
365+
args::Command _cmd{_cli.cmd_group, "query", "Query the project"};
366+
args::HelpFlag _help{_cmd, "help", "Print help for the `query` subcommand", {'h', "help"}};
367+
args::Positional<std::string> _id{
368+
_cmd,
369+
"id",
370+
"Obtain this information. Valid values:\n"
371+
"* project.root",
372+
};
373+
374+
public:
375+
explicit cmd_query(cli_common& gl)
376+
: _cli{gl} {}
377+
378+
explicit operator bool() const { return !!_cmd; }
379+
380+
int run() {
381+
static std::unordered_map<std::string, std::function<int(cmd_query const&)>> queries{
382+
{"project.root",
383+
[](cmd_query const& cmd) {
384+
std::cout << cmd._cli.get_base_dir().string() << '\n';
385+
return 0;
386+
}},
387+
};
388+
389+
auto const id = _id.Get();
390+
391+
auto query = queries.find(id);
392+
if (query == queries.end()) {
393+
_cli.console->error(
394+
"Invalid id `{}`. Valid values:\n"
395+
"* project.root",
396+
id);
397+
return 1;
398+
}
399+
400+
return query->second(*this);
401+
}
402+
};
403+
362404
} // namespace
363405

364406
int main(int argc, char** argv) {
@@ -369,6 +411,7 @@ int main(int argc, char** argv) {
369411
cmd_list list{args};
370412
cmd_new new_{args};
371413
cmd_update update{args};
414+
cmd_query query{args};
372415

373416
try {
374417
parser.ParseCLI(argc, argv);
@@ -387,6 +430,8 @@ int main(int argc, char** argv) {
387430
return new_.run();
388431
} else if (update) {
389432
return update.run();
433+
} else if (query) {
434+
return query.run();
390435
} else {
391436
assert(false && "No subcommand selected?");
392437
std::terminate();

tests/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,30 @@ function(pf_add_test_exe name)
3535
EXTRA_ARGS $<$<BOOL:${PF_FORCE_TESTS_COLOR}>:--use-colour=yes>)
3636
endfunction()
3737

38+
function(pf_add_query_test ID)
39+
set(options)
40+
set(list_args PASS_REGULAR_EXPRESSION FAIL_REGULAR_EXPRESSION)
41+
42+
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${args}" "${list_args}")
43+
44+
add_test(
45+
NAME "query:${ID}"
46+
COMMAND pf query "${ID}"
47+
)
48+
set_tests_properties("query:${ID}"
49+
PROPERTIES
50+
PASS_REGULAR_EXPRESSION "${ARG_PASS_REGULAR_EXPRESSION}"
51+
FAIL_REGULAR_EXPRESSION "${ARG_FAIL_REGULAR_EXPRESSION}"
52+
)
53+
endfunction()
54+
3855
pf_add_test_exe(generate generate.cpp)
3956

4057
pf_add_test_exe(existing
4158
existing/detect_base_dir.cpp
4259
existing/update_source_files.cpp)
4360
configure_directory(existing/sample)
61+
62+
pf_add_query_test(project.root
63+
PASS_REGULAR_EXPRESSION "${PROJECT_SOURCE_DIR}"
64+
)

0 commit comments

Comments
 (0)