Command line options with getopt_long
This commit is contained in:
parent
022782ce09
commit
672b3abae0
@ -59,6 +59,7 @@
|
||||
#include <time.h>
|
||||
#include <setjmp.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#ifndef TIOCGWINSZ
|
||||
#include <termios.h>
|
||||
@ -236,6 +237,17 @@ void send_command(int cmd, int opt) {
|
||||
}
|
||||
}
|
||||
|
||||
void usage(char * argv[]) {
|
||||
printf(
|
||||
"Terminal Nyancat\n"
|
||||
"\n"
|
||||
"usage: %s [-h] [-t] [-i] \n"
|
||||
"\n"
|
||||
" -i --intro \033[3mShow the introduction / about informaiton at startup.\033[0m\n"
|
||||
" -t --telnet \033[3mTelnet mode.\033[0m\n",
|
||||
" -h --help \033[3mShow this help message.\033[0m\n",
|
||||
argv[0]);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
@ -249,10 +261,45 @@ int main(int argc, char ** argv) {
|
||||
char sb[1024] = {0};
|
||||
char sb_len = 0;
|
||||
|
||||
if (argc > 1) {
|
||||
if (!strcmp(argv[1], "-t")) {
|
||||
/* Yes, I know, lame way to get arguments, whatever, we only want one of them. */
|
||||
char show_intro = 0;
|
||||
|
||||
/* Long option names */
|
||||
static struct option long_opts[] = {
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"telnet", no_argument, 0, 't'},
|
||||
{"intro", no_argument, 0, 'i'},
|
||||
{0,0,0,0}
|
||||
};
|
||||
|
||||
/* Process arguments */
|
||||
int index, c;
|
||||
while ((c = getopt_long(argc, argv, "hit", long_opts, &index)) != -1) {
|
||||
if (!c) {
|
||||
if (long_opts[index].flag == 0) {
|
||||
c = long_opts[index].val;
|
||||
}
|
||||
}
|
||||
switch (c) {
|
||||
case 'i': /* Show introduction */
|
||||
show_intro = 1;
|
||||
break;
|
||||
case 't': /* Expect telnet bits */
|
||||
telnet = 1;
|
||||
break;
|
||||
case 'h': /* Show help and exit */
|
||||
usage(argv);
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (telnet) {
|
||||
/* Telnet mode */
|
||||
|
||||
/* Intro is implied by telnet mode, so override whatever was asked for. */
|
||||
show_intro = 1;
|
||||
|
||||
/* Set the default options */
|
||||
set_options();
|
||||
@ -372,7 +419,6 @@ int main(int argc, char ** argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* We are running standalone, retrieve the
|
||||
* terminal type from the environment. */
|
||||
@ -548,6 +594,7 @@ int main(int argc, char ** argv) {
|
||||
/* Clear the screen */
|
||||
printf("\033[H\033[2J\033[?25l");
|
||||
|
||||
if (show_intro) {
|
||||
/* Display the MOTD */
|
||||
int countdown_clock = 5;
|
||||
for (k = 0; k < countdown_clock; ++k) {
|
||||
@ -581,6 +628,7 @@ int main(int argc, char ** argv) {
|
||||
|
||||
/* Clear the screen again */
|
||||
printf("\033[H\033[2J\033[?25l");
|
||||
}
|
||||
|
||||
/* Store the start time */
|
||||
time_t start, current;
|
||||
|
Loading…
Reference in New Issue
Block a user