Implemented basic command-line options

This commit is contained in:
AnnaSnoeijs 2025-06-06 21:31:59 +02:00
parent 197445de70
commit 92f4057583
3 changed files with 76 additions and 12 deletions

View file

@ -17,6 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include "macros.h"
#include "config.h"
@ -24,25 +25,50 @@
#include "types.h"
#include "ui.h"
#define VERSION 0.0.4
#define VERSION 0.0.5
#define VERSIONSTRING \
"AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n"
// The LISENCE file
// Not null-terminated so need pointers for both start and end
extern char _binary_LICENSE_start[];
extern char _binary_LICENSE_end[];
int main( int argc, char *argv[] ){
// Parse options
for(;;){
// Allowed options
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "license", no_argument, NULL, 'l' },
{ "version", no_argument, NULL, '\0' },
{ 0 }
};
// Index of current option
int option_index = 0;
// Get next option
char c = getopt_long( argc, argv, "hl", long_options, &option_index );
if( c == -1 ) break;
// Handle option
switch( c ){
case 'h':
printf( "Usage: connect4 [OPTIONS]\n\n" );
case '\0': // Any option without shorthand
switch( option_index ){
case 2: // --version
printf( VERSIONSTRING );
return 0;
default:
fprintf(
stderr,
"%s: unhandled option \'--%s\' \n",
argv[0],
long_options[ option_index ].name
);
break;
}
break;
case 'h': // --help
printf( "Usage: %s [OPTIONS]\n\n", argv[0] );
for( int i = 0; long_options[i].name != NULL; i++ ){
if( long_options[i].val != '\0' )
printf( " -%c --", long_options[i].val );
@ -51,13 +77,30 @@ int main( int argc, char *argv[] ){
putchar( '\n' );
}
return 0;
case 'l': // --license
for(
char *p = _binary_LICENSE_start;
p != _binary_LICENSE_end;
p++
){
putchar( *p );
}
return 0;
}
}
// Check for unhandled options
if( optind < argc ) {
fprintf( stderr, "non-option ARGV-elements: " );
while( optind < argc )
printf( "%s ", argv[optind++] );
putchar( '\n' );
}
// Start the actual program
printf(
"AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n"
VERSIONSTRING
"AnnaConnect comes with ABSOLUTELY NO WARRANTY\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain condtions\n"
"under certain condtions. See `%s --license`\n", argv[0]
);
// board, innit?
initBoard();
@ -84,6 +127,7 @@ int main( int argc, char *argv[] ){
.win0 = {0},
.win1 = {0},
};
// Begin loopin
for(;; playboard.player = !playboard.player ){
move_t move = askMove();
int column = askColumn( playboard, move );