cmd run

Run Settings
LanguageC++
Language Version
Run Command
#include <stdio.h> #include <string.h> #include <stdint.h> #include "cmd.h" int main(int argc, char **argv) { cmd_init(argc, argv); if(cmd_has("apple")) printf("here's an apple\n"); if(cmd_has("orange")) printf("here's an orange\n"); if(cmd_has("pear")) printf("here's a pear\n"); cmd_finish(); return 0; }
int cmd_argc; char **cmd_argv; const char *cmd_available[256]; size_t cmd_available_i = 0; const char *cmd_used[256]; size_t cmd_used_i = 0; bool cmd_only_display_help = false; bool cmd_is_option(const char *a) { return a[0] == '-' || a[0] == '/'; } bool cmd_col_arg(const char *opt, char **out) { return false; } bool cmd_list_arg(const char *opt) { return false; } bool cmd_no_arguments() { for(int i = 0; i < cmd_argc; ++i) { if(cmd_is_option(cmd_argv[i])) { return false; } } return true; } bool is_alpha(char a) { return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a == '_'); } bool is_pure_numeric(char a) { return a >= '0' && a <= '9'; } bool is_alpha_numeric(char a) { return is_alpha(a) || is_pure_numeric(a); } bool cmd_cmp(const char *a, const char *b) { size_t i = 0; for(; is_alpha_numeric(a[i]); ++i) { if(a[i] != b[i]) { return false; } } if(b[i] != 0) { return false; } if(a[i] == 0) { return true; } return false; } bool cmd_check(const char *arg) { if(cmd_only_display_help) { return false; } for(int i = 0; i < cmd_argc; ++i) { if(cmd_argv[i][0] == '-' || cmd_argv[i][0] == '/') { bool match = cmd_cmp(cmd_argv[i] + 1, arg); if(match) { return true; } } } return false; } void cmd_init(int argc, char **argv) { cmd_argc = argc; cmd_argv = argv; if(cmd_check("help") || cmd_check("h") || cmd_no_arguments()) { cmd_only_display_help = true; } } bool cmd_has(const char *arg) { cmd_available[cmd_available_i] = arg; cmd_available_i += 1; if(cmd_check(arg)) { // Fix: Used previously check. cmd_used[cmd_used_i] = arg; cmd_used_i += 1; return true; } return false; } void cmd_finish() { // Fix: Make it so if help is in the list help is the only command if(cmd_only_display_help) { // Fix: Reverse the help thing? printf("Available Commands\n"); printf("\t-help\n"); for(size_t i = 0; i < cmd_available_i; ++i) { printf("\t-%s\n", cmd_available[i]); } } bool found_invalid_flags = false; // Fix: print out commands not recognised for(int j = 0; j < cmd_argc; ++j) { if(cmd_is_option(cmd_argv[j]) == false) { continue; } for(size_t i = 0; i < cmd_available_i; ++i) { if(cmd_cmp(cmd_argv[j] + 1, cmd_available[i])) { goto cmd_used; } } if(strcmp(cmd_argv[j], "-help") == 0 || strcmp(cmd_argv[j], "-h") == 0) { goto cmd_used; } if(found_invalid_flags == false) { found_invalid_flags = true; printf("Invalid Flags\n"); } printf("\t%s\n", cmd_argv[j]); cmd_used:; } }
Editor Settings
Theme
Key bindings
Full width
Lines