Initial commit.
This commit is contained in:
29
src/arg_parse.c
Normal file
29
src/arg_parse.c
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "arg_parse.h"
|
||||
|
||||
enum cmd_id parse_args(const char *cmd) {
|
||||
for(int i = 0; i < (int)ARRAY_LEN(commands); ++i) {
|
||||
for(int j = 0; j < (int)ARRAY_LEN(commands[i].str); ++j) {
|
||||
if(commands[i].str[j] && strcmp(commands[i].str[j], cmd) == 0)
|
||||
return commands[i].id;
|
||||
}
|
||||
}
|
||||
|
||||
return CMD_UNKNOWN;
|
||||
}
|
41
src/arg_parse.h
Normal file
41
src/arg_parse.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "global.h"
|
||||
|
||||
enum cmd_id {
|
||||
CMD_UNKNOWN = 0,
|
||||
CMD_HELP,
|
||||
CMD_VERSION,
|
||||
};
|
||||
|
||||
struct cmd {
|
||||
enum cmd_id id;
|
||||
const char *str[3];
|
||||
};
|
||||
|
||||
static const struct cmd commands[] = {
|
||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||
};
|
||||
|
||||
enum cmd_id parse_args(const char *cmd);
|
24
src/global.h
Normal file
24
src/global.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifndef VERSION
|
||||
#define VERSION "VERSION"
|
||||
#endif
|
||||
|
||||
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
67
src/main.c
Normal file
67
src/main.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "arg_parse.h"
|
||||
#include "global.h"
|
||||
|
||||
void print_version(void) {
|
||||
printf("menu-helper v%s\n\n", VERSION);
|
||||
}
|
||||
|
||||
void print_usage(void) {
|
||||
printf("USAGE: menu-helper <cmd> [options]\n\n");
|
||||
}
|
||||
|
||||
void print_help(void) {
|
||||
print_version();
|
||||
print_usage();
|
||||
|
||||
printf("COMMANDS:\n"
|
||||
"\thelp, -h, --help Show this help information.\n"
|
||||
"\tversion, -v, --version Show version information.\n"
|
||||
"\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
enum cmd_id id;
|
||||
|
||||
if(argc < 2) {
|
||||
fprintf(stderr, "Invalid number of arguments. Use 'help' sub-command.\n");
|
||||
print_usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
id = parse_args(argv[1]);
|
||||
|
||||
switch(id) {
|
||||
case CMD_HELP:
|
||||
print_help();
|
||||
break;
|
||||
case CMD_VERSION:
|
||||
print_version();
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "No such command '%s'. Use 'help' sub-command.\n", argv[1]);
|
||||
print_usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user