aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/command.ccm
blob: da3444a4f479f7a2bcfab707ccb84904d17a95e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * This source code is released into the public domain.
 */

module;

export module nihil.cli:command;

import nihil.error;
import nihil.std;
import :command_node;

namespace nihil {

export struct command;

/*
 * A command that can be invoked.  Instantiating a command adds this command
 * to the global command table.  If an error occurs, the program will abort.
 */

using command_handler_t = int (int, char **);
using command_function_t = std::function<command_handler_t>;

export struct command final : command_node {
	command(std::string_view path, std::string_view usage,
		command_function_t);

	command(std::string_view path, std::string_view usage, auto &&fn)
		: command(path, usage, command_function_t(fn))
	{}

	[[nodiscard]] auto usage(this command const &) noexcept
		-> std::string_view;

	[[nodiscard]] auto invoke(int argc, char **argv) const
		-> std::expected<int, error> override;

private:
	std::string_view	m_usage;
	command_function_t	m_handler;
};

} // namespace nihil