Permalink
Browse files

added help and cleaned up commandline options

Signed-off-by: Tolga Cakir <tolga@cevel.net>
  • Loading branch information...
tolga9009 committed Apr 28, 2017
1 parent 67a27c9 commit d6d2513dad4a044aaecb297e41bf5e5f04e7bbf1
Showing with 24 additions and 12 deletions.
  1. +24 −12 src/main.cpp
View
@@ -15,6 +15,16 @@
#include <process.hpp>
#include <core/device_manager.hpp>
void help(std::string name) {
std::cerr << "Usage: " << name << " [options]" << std::endl

This comment has been minimized.

Show comment
Hide comment
@luziferius

luziferius Apr 30, 2017

std::endl is is a bit overused. It does create a newline, but flushes the output buffer, too.
A straight '\n' does produce identical output, but doesn’t cause (unnecessary) buffer flushes. When constructing longer text output, only one flush at the end is needed.
But as this is not time critical code, it can stay as-is.

@luziferius

luziferius Apr 30, 2017

std::endl is is a bit overused. It does create a newline, but flushes the output buffer, too.
A straight '\n' does produce identical output, but doesn’t cause (unnecessary) buffer flushes. When constructing longer text output, only one flush at the end is needed.
But as this is not time critical code, it can stay as-is.

This comment has been minimized.

Show comment
Hide comment
@tolga9009

tolga9009 May 1, 2017

Owner

In this case, it actually doesn't matter, as std::cerr is unbuffered output. This would matter in buffered output, like std::cout. Even there, some implementations flush buffers on new lines. Using \n probably would have looked cleaner though.

@tolga9009

tolga9009 May 1, 2017

Owner

In this case, it actually doesn't matter, as std::cerr is unbuffered output. This would matter in buffered output, like std::cout. Even there, some implementations flush buffers on new lines. Using \n probably would have looked cleaner though.

<< std::endl
<< "Options:" << std::endl

This comment has been minimized.

Show comment
Hide comment
@luziferius

luziferius Apr 30, 2017

Maybe use the standard GNU-utils phrase »Mandatory arguments to long options are mandatory for short options too.« Many command line utilities, where I tried --help argument for reference, do output that line.
You have short and long options and option arguments are required for both versions, so the phrase is applicable.

@luziferius

luziferius Apr 30, 2017

Maybe use the standard GNU-utils phrase »Mandatory arguments to long options are mandatory for short options too.« Many command line utilities, where I tried --help argument for reference, do output that line.
You have short and long options and option arguments are required for both versions, so the phrase is applicable.

This comment has been minimized.

Show comment
Hide comment
@tolga9009

tolga9009 May 1, 2017

Owner

Sounds good to me.

@tolga9009

tolga9009 May 1, 2017

Owner

Sounds good to me.

<< " -c, --config=<file> Override default configuration file path" << std::endl
<< " -d, --daemon Run process as daemon" << std::endl
<< " -h, --help Print this screen" << std::endl
<< " -v, --version Print program version" << std::endl;
}
void setupConfig(libconfig::Config *config, std::string configFilePath = "/etc/sidewinderd.conf") {
try {
config->readFile(configFilePath.c_str());
@@ -54,39 +64,39 @@ int main(int argc, char *argv[]) {
static struct option longOptions[] = {
{"config", required_argument, 0, 'c'},
{"daemon", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
int opt, index = 0;
std::string configFilePath;
std::string workdir;
/* flags */
bool shouldDaemonize = false;
while ((opt = getopt_long(argc, argv, ":c:dp:v", longOptions, &index)) != -1) {
while ((opt = getopt_long(argc, argv, ":c:dhv", longOptions, &index)) != -1) {
switch (opt) {
case 'c':
configFilePath = optarg;
break;
case 'd':
shouldDaemonize = true;
break;
case 'p':
workdir = optarg;
break;
case 'h':
help(process.getName());
return EXIT_SUCCESS;
case 'v':
std::cout << "sidewinderd version " << process.getVersion() << std::endl;
std::cerr << process.getName() << " " << process.getVersion() << std::endl;
return EXIT_SUCCESS;
case ':':
std::cout << "Missing argument." << std::endl;
break;
std::cerr << "Missing argument." << std::endl;
return EXIT_FAILURE;
case '?':
std::cout << "Unrecognized option." << std::endl;
break;
std::cerr << "Unknown option." << std::endl;
return EXIT_FAILURE;
default:
std::cout << "Unexpected error." << std::endl;
std::cerr << "Unexpected error." << std::endl;
return EXIT_FAILURE;
}
}
@@ -119,7 +129,9 @@ int main(int argc, char *argv[]) {
/* setting gid and uid to configured user */
process.applyUser(config.lookup("user"));
/* creating sidewinderd directory in user's home directory */
// setting up working directory
std::string workdir;
if (config.exists("workdir")) {
workdir = config.lookup("workdir").c_str();
}

0 comments on commit d6d2513

Please sign in to comment.