Skip to content

Commit

Permalink
support command-line option (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinwasyu committed Feb 4, 2020
1 parent a559473 commit b7432c1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/kot.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018,2019 Rinwasyu
* Copyright 2018,2019,2020 Rinwasyu
*
* This file is part of kot.
*
Expand Down Expand Up @@ -27,6 +27,7 @@
#include "editor.h"
#include "key.h"
#include "kot.h"
#include "option.h"

void setup() {
key.init();
Expand All @@ -53,6 +54,7 @@ int update() {
}

int main(int argc, char **argv) {
option.readOptions(&option, argc, argv);
setup();
if (argc == 1) {
doc.new(&doc, "file.txt");
Expand Down
7 changes: 6 additions & 1 deletion src/kot.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Rinwasyu
* Copyright 2019,2020 Rinwasyu
*
* This file is part of kot.
*
Expand All @@ -22,7 +22,12 @@

#define KOT_KOT_H

#define PROGNAME "kot"
#define VERSION "v0.6.5"
#define AUTHOR "Rinwasyu"
#define AUTHOR_YEAR "2018"
#define WEBSITE "https://github.com/Rinwasyu/kot"
#define LICENSE "GPL v3"
#define DRAW_TITLEBAR_HEIGHT 1
#define DOC_MAXIMUM_ROWS 2000
#define DOC_MAXIMUM_COLS 1000
Expand Down
67 changes: 67 additions & 0 deletions src/option.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 Rinwasyu
*
* This file is part of kot.
*
* Kot 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.
*
* Kot 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "kot.h"
#include "option.h"

void option_help() {
printf("Usage: %s [--help] [--version]\n", PROGNAME);
exit(0);
}

void option_version() {
printf("%s %s\n", PROGNAME, VERSION);
printf("Copyright %s %s\n", AUTHOR_YEAR, AUTHOR);
printf("%s\n", WEBSITE);
printf("This program is licensed under %s\n", LICENSE);
exit(0);
}

void option_readOptions(struct Option *option, int argc, char **argv) {
int unknown_options = 0;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
int j = 1;
for (; argv[i][j] == '-'; j++);

if (strcmp(&argv[i][j], "help") == 0) {
option->help();
} else if (strcmp(&argv[i][j], "version") == 0) {
option->version();
} else {
printf("Unknown option: %s\n", argv[i]);
unknown_options++;
}
}
}
if (unknown_options > 0) {
option->help();
}
}

struct Option option = {
option_readOptions,
option_help,
option_version
};
37 changes: 37 additions & 0 deletions src/option.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 Rinwasyu
*
* This file is part of kot.
*
* Kot 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.
*
* Kot 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/>.
*
*/

#ifndef KOT_OPTION_H

#define KOT_OPTION_H

struct Option {
void (*readOptions)(struct Option *option, int argc, char **argv);
void (*help)(void);
void (*version)(void);
};

void option_help(void);
void option_version(void);
void option_readOptions(struct Option *option, int argc, char **argv);

struct Option option;

#endif

0 comments on commit b7432c1

Please sign in to comment.