improve the startup help messages

This commit is contained in:
Tomasz Sowa 2024-03-08 22:01:33 +01:00
parent b753464608
commit dbf7dfab82
Signed by: tomasz.sowa
GPG Key ID: 662CC1438638588B
2 changed files with 59 additions and 35 deletions

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2021, Tomasz Sowa
* Copyright (c) 2008-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -44,6 +44,8 @@ namespace Winix
#define WINIX_VER_MINOR 7
#define WINIX_VER_REVISION 3
#define WINIX_COPYRIGHT_YEAR_FROM 2008
#define WINIX_COPYRIGHT_YEAR_TO 2024
} // namespace Winix

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2022, Tomasz Sowa
* Copyright (c) 2008-2024, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -65,7 +65,7 @@ App app;
void print_version()
{
std::cout << "Winix " << WINIX_VER_MAJOR << '.' << WINIX_VER_MINOR << '.' << WINIX_VER_REVISION << std::endl;
std::cout << "Copyright (c) 2008-2022, Tomasz Sowa" << std::endl;
std::cout << "Copyright (c) " << WINIX_COPYRIGHT_YEAR_FROM << "-" << WINIX_COPYRIGHT_YEAR_TO << ", Tomasz Sowa" << std::endl;
std::cout << std::endl;
std::cout << "Components: " << std::endl;
std::cout << " pikotools: " << PIKOTOOLS_VERSION_MAJOR << '.' << PIKOTOOLS_VERSION_MINOR << '.' << PIKOTOOLS_VERSION_PATCH << std::endl;
@ -78,29 +78,30 @@ void print_version()
void print_syntax()
{
std::cout << "Winix " << WINIX_VER_MAJOR << '.' << WINIX_VER_MINOR << '.' << WINIX_VER_REVISION << std::endl;
std::cout << "Copyright (c) 2008-2022, Tomasz Sowa" << std::endl;
std::cout << "Copyright (c) " << WINIX_COPYRIGHT_YEAR_FROM << "-" << WINIX_COPYRIGHT_YEAR_TO << ", Tomasz Sowa" << std::endl;
std::cout << std::endl;
std::cout << "Usage: winix [options]" << std::endl;
std::cout << " -c : a path to the config file" << std::endl;
std::cout << " --config : a path to the config file" << std::endl;
std::cout << " --use-env : allow to use environment variables" << std::endl;
std::cout << " --dump-config : dump all read config options to stdout and exit" << std::endl;
std::cout << " -h : print usage information" << std::endl;
std::cout << " --help : print usage information" << std::endl;
std::cout << " -? : print usage information" << std::endl;
std::cout << " -v : print version" << std::endl;
std::cout << " --version : print version" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --config filename.conf a path to a config file" << std::endl;
std::cout << " -c filename.conf a path to a config file" << std::endl;
std::cout << " --use-env allow to use environment variables" << std::endl;
std::cout << " --dump-config dump all read config options to stdout and exit" << std::endl;
std::cout << " --help, -h, -? show help" << std::endl;
std::cout << " --version, -v print the version" << std::endl;
std::cout << std::endl;
std::cout << "At least one -c or --config parameter is required, use -c or --config option" << std::endl;
std::cout << "multiple times to load more than one config." << std::endl;
std::cout << std::endl;
std::cout << "Environment variables must be prefixed with winix_ to be loaded by winix." << std::endl;
std::cout << "The winix_ prefix is then removed and the key value converted to lowercase." << std::endl;
std::cout << "Using the --use-env option will allow environment variables to overwrite" << std::endl;
std::cout << "parameter values from a configuration file. In such a case environment" << std::endl;
std::cout << "variables must be prefixed with the winix_ prefix. The winix_ prefix" << std::endl;
std::cout << "is then removed and the key name is converted to lowercase." << std::endl;
std::cout << "Sample:" << std::endl;
std::cout << "evn WINIX_MY_OPTION=TEST123test winix -c config_file" << std::endl;
std::cout << "This will add my_option to the config with value TEST123test." << std::endl;
std::cout << "Environment variables are read last so they will overwrite the values" << std::endl;
std::cout << "from the configuration files." << std::endl;
std::cout << "evn WINIX_MY_OPTION=FOO123bar winix -c config_file --use-env" << std::endl;
std::cout << "This will add a my_option to the config with the FOO123bar value." << std::endl;
std::cout << "Environment variables are read last so they will overwrite values" << std::endl;
std::cout << "from configuration files." << std::endl;
}
@ -118,7 +119,15 @@ RunStatus ParseParameters(int argc, const char ** argv, pt::Space & options)
if( status != pt::MainOptionsParser::status_ok )
{
Winix::print_syntax();
if( status == pt::MainOptionsParser::status_argument_not_provided )
{
std::cout << "a parameter value expected, use a -h option to show a help" << std::endl;
}
else
{
Winix::print_syntax();
}
run_status.exit_code = RunStatus::EXIT_CODE_PARAMETERS_SYNTAX_ERROR;
run_status.should_continue = false;
}
@ -323,7 +332,7 @@ bool ReadConfigFromEnv(const char ** env)
bool ReadConfigs(const pt::Space & options, const char ** env)
bool ReadConfigs(Log & log, const pt::Space & options, const char ** env)
{
size_t config_read = 0;
bool status = true;
@ -344,8 +353,7 @@ bool ReadConfigs(const pt::Space & options, const char ** env)
if( config_read == 0 )
{
std::cout << "You have to provide a config file with c parameter" << std::endl;
Winix::print_syntax();
log << "no config provided, use a -h option to show a help" << logend;
return false;
}
@ -393,6 +401,17 @@ void CleanupCurlLibrary()
}
void FlushLogToStdout(Log & log)
{
pt::WTextStream * log_buffer = log.get_log_buffer();
if( log_buffer && !log_buffer->empty() )
{
pt::wide_stream_to_utf8(*log_buffer, std::cout);
log_buffer->clear();
}
}
RunStatus InitializeWinix(Log & log, pt::Space & options, const char ** env)
{
@ -405,16 +424,11 @@ RunStatus InitializeWinix(Log & log, pt::Space & options, const char ** env)
return run_status;
}
if( !ReadConfigs(options, env) )
if( !ReadConfigs(log, options, env) )
{
pt::WTextStream * log_buffer = log.get_log_buffer();
// we need to print the buffer by hand because the logger
// is not fully initialized yet
if( log_buffer && !log_buffer->empty() )
{
pt::wide_stream_to_utf8(*log_buffer, std::cout);
}
FlushLogToStdout(log);
run_status.exit_code = RunStatus::EXIT_CODE_CANNOT_CORRECTLY_READ_CONFIG;
run_status.should_continue = false;
@ -431,7 +445,7 @@ RunStatus InitializeWinix(Log & log, pt::Space & options, const char ** env)
<< " but the modified values are not printed here):" << logend;
app.config.space.serialize_to_space_stream(*log_buffer, true);
log << logend;
pt::wide_stream_to_utf8(*log_buffer, std::cout);
FlushLogToStdout(log);
}
run_status.should_continue = false;
@ -490,11 +504,19 @@ RunStatus InitializeWinix(Log & log, pt::Space & options, const char ** env)
}
void UninitializeWinix(Log & log)
void UninitializeWinix(Log & log, bool was_winix_running)
{
Winix::RemovePidFile();
log << Winix::logsave;
Winix::CleanupCurlLibrary();
if( was_winix_running )
{
log << Winix::logsave;
}
else
{
FlushLogToStdout(log);
}
}
@ -541,7 +563,7 @@ int main(int argc, const char ** argv, const char ** env)
}
Winix::LogInfo(log, Winix::log1, "Winix", true, "stopped");
UninitializeWinix(log);
UninitializeWinix(log, run_status.should_continue);
return run_status.exit_code;
}