Updates how config options are set on command line

This commit is contained in:
Jonathan Hodgson 2021-12-28 10:00:41 +00:00
parent 8bed39700e
commit f392af92c3
5 changed files with 56 additions and 22 deletions

View file

@ -63,3 +63,21 @@ char* getDefaultUserConfigLoc(){
return strdup(configFile);
}
void setConfig(Config *config, char option[], char value[]){
if ( strcmp( option, "database" ) == 0 ){
config->database = value;
} else if ( strcmp( option, "localConfig" ) == 0 ){
config->localConfig = value;
} else if ( strcmp( option, "userConfig" ) == 0 ){
config->userConfig = value;
} else if ( strcmp( option, "port" ) == 0 ){
config->port = atoi(value);
}
}
void printConfig(Config *config){
printf("Database: %s\n", config->database);
printf("localConfig: %s\n", config->localConfig);
printf("userConfig: %s\n", config->userConfig);
printf("port: %i\n", config->port);
}

View file

@ -20,5 +20,7 @@ static char* resolveTilde(const char *path);
Config* configDefaults();
char* getConfigDir();
char* getDefaultUserConfigLoc();
void setConfig(Config *config, char option[], char value[]);
void printConfig(Config *config);
#endif /* ifndef CONFIG_H */

View file

@ -22,12 +22,10 @@ unsigned int validatePortNumber(unsigned int portNumber){
}
void printHelp(){
printf("Usage: %s [options]\n", PACKAGE_NAME);
printf( "Options are:\n" );
printf( "\t --database DATABASE A sqlite3 database file (Defatult: %s)\n", DEFAULT_DATABASE );
printf( "\t --config CONFIG A config file (Default: %s)\n", DEFAULT_CONFIG );
printf( "\t --port PORT The listening port (Default: %i)\n", DEFAULT_PORT );
printf( "\t --help Display version information.\n");
printf("Usage: %s [options]\n", PACKAGE_NAME);
printf( "Options are:\n" );
printf( "\t --config OPTION VALUE Sets config option OPTION to VALUE\n" );
printf( "\t --print-config Prints current config\n" );
}
int main(int argc, char**argv){
@ -39,24 +37,15 @@ int main(int argc, char**argv){
for ( unsigned int i = 1; i < argc; i++ ){
if ( strcmp( argv[i], "--database" ) == 0 ){
if ( i + 1 < argc ){
database = argv[++i];
if ( strcmp( argv[i], "--config" ) == 0 ){
if ( i + 2 < argc ){
setConfig( defaultconfig, argv[i + 1], argv[i + 2] );
} else {
printf("--database requires a positional argument\n");
}
} else if ( strcmp( argv[i], "--config" ) == 0 ){
if ( i + 1 < argc ){
config = argv[++i];
} else {
printf("--config requires a positional argument\n");
}
} else if ( strcmp( argv[i], "--port" ) == 0 ){
if ( i + 1 < argc ){
port = validatePortNumber( atoi(argv[++i]) );
} else {
printf("--port requires a positional argument\n");
printf("--config requires 2 positional arguments\n");
}
} else if ( strcmp( argv[i], "--print-config" ) == 0 ){
printConfig(defaultconfig);
return 0;
} else if ( strcmp( argv[i], "--help" ) == 0 ){
printHelp();
return 0;