Files
mini-ticket-system/cli/parser.cpp
T

63 lines
1.5 KiB
C++

#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <string>
#include "include/parser.h"
#include "include/handler.h"
using namespace std;
void show_usage() {
cout << "usage:\n"
"these env vars have to be set: FRONTEND_API_KEY, BASE_API_URL\n"
"tickets create <title> <description>\n"
"tickets list [-status <status>]\n"
"tickets detail <id>" << endl;
}
#define show_usage_and_die() show_usage(); return 1;
int parse_args(Dao *dao, int argc, char *argv[]) {
if (argc == 1) { // not enough args
show_usage_and_die();
}
if (strcmp(argv[1], "create") == 0) { // creating a ticket
if (argc != 4) {
show_usage_and_die();
}
return handle_create(dao, argv[2], argv[3]); // title and description
}
if (strcmp(argv[1], "list") == 0) { // listing tickets (optional filter by status)
if (argc != 2 && argc != 4) {
show_usage_and_die();
}
if (argc == 4) {
if (strcmp(argv[2], "-status") == 0) {
return handle_list(dao, argv[3]); // the status text
}
}
// now we know that argc == 2
// -> handle list (get all tickets)
return handle_list(dao);
}
if (strcmp(argv[1], "detail") == 0) {
if (argc != 3) {
show_usage_and_die();
}
int ticket_id = atoi(argv[2]);
return handle_detail(dao, ticket_id); // ticket id
}
return 0;
}