diff options
Diffstat (limited to 'src/command.cpp')
| -rw-r--r-- | src/command.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/command.cpp b/src/command.cpp new file mode 100644 index 0000000..4066c23 --- /dev/null +++ b/src/command.cpp @@ -0,0 +1,74 @@ +#include "command.h" +#include <cassert> + +namespace katja +{ + void list::execute() const + { + for (const auto& package : katja::read_package_database()) + { + std::cout << package.second.name() + << " " << package.second.version() + << " (" << package.second.tag() << ")" + << std::endl; + } + } + + void help::execute() const + { + std::cout << "Usage:\n" + "\tkatja {info|help} [OPTIONS]\n\n"; + } + + command_exception::command_exception(const command_exception_t exception_type, + std::vector<std::string> failed_arguments) noexcept + : m_exception_type(exception_type), m_failed_arguments(failed_arguments) + { + } + + const char *command_exception::what() const noexcept + { + switch (m_exception_type) + { + case command_exception_t::no_command: + return "No command specified."; + case command_exception_t::too_many_arguments: + return "Too many arguments given."; + case command_exception_t::unknown_command: + return "Unknown command."; + } + assert(false); + } + + const std::vector<std::string>& command_exception::failed_arguments() const noexcept + { + return m_failed_arguments; + } + + std::unique_ptr<command> parse_command_line(int argc, char **argv) + { + if (argc > 2) + { + std::vector<std::string> failed_arguments; + + for (std::size_t i = 2; i < argc; ++i) + { + failed_arguments.push_back(argv[i]); + } + throw command_exception(command_exception_t::unknown_command); + } + else if (argc < 2) + { + throw command_exception(command_exception_t::no_command); + } + if (strcmp(argv[1], "list") == 0) + { + return std::make_unique<list>(); + } + else if (strcmp(argv[1], "help") == 0) + { + return std::make_unique<help>(); + } + throw command_exception(command_exception_t::unknown_command, { argv[1] }); + } +} |
