From 4379b0311e0855e2dbf3010d6c091bb86f5f4b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 29 Oct 2024 17:59:57 +0100 Subject: [PATCH] Add safeguards to avoid bad usage. --- TODO.md | 2 +- src/main.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 48eb117..c238239 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,6 @@ # To-Do List -- [ ] Add more safeguards to avoid bad usage. +- [X] Add more safeguards to avoid bad usage. - [ ] Create a man page. - [ ] Add more documentation to `help` subcommand. - [ ] Add import/export functionality. diff --git a/src/main.cpp b/src/main.cpp index ccd8459..9a08384 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,39 +39,63 @@ int main(int argc, char *argv[]) { try { switch(id) { case CMD_ADD: + if(argc not_eq 2) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_add(); break; case CMD_DEL: + if(argc not_eq 3) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_delete(argc - 2, argv + 2); break; case CMD_LIST: + if(argc > 6) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_list(argc - 1, argv + 1); break; case CMD_INFO: + if(argc not_eq 3) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_info(std::stoi(argv[2])); break; case CMD_EDIT_NAME: + if(argc not_eq 3) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_edit_name(std::stoi(argv[2])); break; case CMD_EDIT_DESC: + if(argc not_eq 3) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_edit_desc(std::stoi(argv[2])); break; case CMD_ADD_INGR: + if(argc not_eq 4) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_add_ingr(std::stoi(argv[2]), argv[3]); break; case CMD_RM_INGR: + if(argc not_eq 4) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_rm_ingr(std::stoi(argv[2]), argv[3]); break; case CMD_ADD_TAG: + if(argc not_eq 4) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_add_tag(std::stoi(argv[2]), argv[3]); break; case CMD_RM_TAG: + if(argc not_eq 4) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; ret = cmd_rm_tag(std::stoi(argv[2]), argv[3]); break; case CMD_HELP: + if(argc not_eq 2) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; print_help(); break; case CMD_VERSION: + if(argc not_eq 2) + throw "Invalid number of arguments. Use 'help' subcommand for more information."; print_version(); break; default: