clean up, move help function

This commit is contained in:
Norbert Preining 2019-02-24 16:33:10 +09:00
parent a71b9bf789
commit d49a598f4b
2 changed files with 74 additions and 60 deletions

View file

@ -1,5 +1,6 @@
import core.stdc.stdlib: EXIT_SUCCESS, EXIT_FAILURE, exit; import core.stdc.stdlib: EXIT_SUCCESS, EXIT_FAILURE, exit;
import std.file, std.string, std.regex, std.stdio, std.process, std.algorithm.searching, std.getopt, std.conv; import std.file, std.string, std.regex, std.stdio, std.process, std.algorithm.searching, std.getopt, std.conv;
import std.algorithm.sorting: sort;
import selective; import selective;
static import log; static import log;
@ -127,20 +128,20 @@ final class Config
} }
Option[] update_from_args(string[] args) void update_from_args(string[] args)
{ {
// Add additional options that are NOT configurable via config file // Add additional options that are NOT configurable via config file
stringValues["create-directory"] = ""; stringValues["create_directory"] = "";
stringValues["destination-directory"] = ""; stringValues["destination_directory"] = "";
stringValues["get-o365-drive-id"] = ""; stringValues["get_o365_drive_id"] = "";
stringValues["remove-directory"] = ""; stringValues["remove_directory"] = "";
stringValues["single-directory"] = ""; stringValues["single_directory"] = "";
stringValues["source-directory"] = ""; stringValues["source_directory"] = "";
boolValues["display-config"] = false; boolValues["display_config"] = false;
boolValues["display-sync-status"] = false; boolValues["display_sync_status"] = false;
boolValues["resync"] = false; boolValues["resync"] = false;
boolValues["print-token"] = false; boolValues["print_token"] = false;
boolValues["logout"] = false; boolValues["logout"] = false;
boolValues["monitor"] = false; boolValues["monitor"] = false;
boolValues["synchronize"] = false; boolValues["synchronize"] = false;
@ -148,11 +149,12 @@ final class Config
// Application Startup option validation // Application Startup option validation
try { try {
string tmpStr;
bool tmpBol;
auto opt = getopt( auto opt = getopt(
args, args,
std.getopt.config.bundling, std.getopt.config.bundling,
std.getopt.config.caseSensitive, std.getopt.config.caseSensitive,
std.getopt.config.passThrough,
"check-for-nomount", "check-for-nomount",
"Check for the presence of .nosync in the syncdir root. If found, do not perform sync.", "Check for the presence of .nosync in the syncdir root. If found, do not perform sync.",
&boolValues["check_for_nomount"], &boolValues["check_for_nomount"],
@ -209,7 +211,7 @@ final class Config
&boolValues["display_sync_status"], &boolValues["display_sync_status"],
"get-O365-drive-id", "get-O365-drive-id",
"Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library", "Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library",
&boolValues["get_o365_drive_id"], &stringValues["get_o365_drive_id"],
"logout", "logout",
"Logout the current user", "Logout the current user",
&boolValues["logout"], &boolValues["logout"],
@ -233,9 +235,20 @@ final class Config
&stringValues["source_directory"], &stringValues["source_directory"],
"synchronize", "synchronize",
"Perform a synchronization", "Perform a synchronization",
&boolValues["synchronize"] &boolValues["synchronize"],
// duplicated from main.d to get full help output!
"confdir",
"Set the directory used to store the configuration files",
&tmpStr,
"version",
"Print the version and exit",
&tmpBol
); );
return opt.options; if (opt.helpWanted) {
outputLongHelp(opt.options);
exit(EXIT_SUCCESS);
}
} catch (GetOptException e) { } catch (GetOptException e) {
log.error(e.msg); log.error(e.msg);
log.error("Try 'onedrive -h' for more information"); log.error("Try 'onedrive -h' for more information");
@ -246,7 +259,6 @@ final class Config
log.error("Try 'onedrive -h' for more information"); log.error("Try 'onedrive -h' for more information");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return null;
} }
@ -337,9 +349,52 @@ final class Config
} }
} }
void outputLongHelp(Option[] opt)
{
auto argsNeedingOptions = [
"--confdir",
"--create-directory",
"--destination-directory",
"--get-O365-drive-id",
"--remove-directory",
"--single-directory",
"--source-directory",
"--syncdir" ];
writeln(`OneDrive - a client for OneDrive Cloud Services
Usage:
onedrive [options] --synchronize
Do a one time synchronization
onedrive [options] --monitor
Monitor filesystem and sync regularly
onedrive [options] --display-config
Display the currently used configuration
onedrive [options] --display-sync-status
Query OneDrive service and report on pending changes
onedrive -h | --help
Show this help screen
onedrive --version
Show version
Options:
`);
foreach (it; opt.sort!("a.optLong < b.optLong")) {
if (it.optLong == "--help") continue;
writefln(" %s%s%s%s\n %s",
it.optLong,
it.optShort == "" ? "" : " " ~ it.optShort,
argsNeedingOptions.canFind(it.optLong) ? " ARG" : "",
it.required ? " (required)" : "", it.help);
}
// write help last
writefln(" --help -h\n This help information.");
}
unittest unittest
{ {
auto cfg = new Config(""); auto cfg = new Config("");
cfg.load("config"); cfg.load("config");
assert(cfg.getValueString("sync_dir") == "~/OneDrive"); assert(cfg.getValueString("sync_dir") == "~/OneDrive");
} }

View file

@ -1,7 +1,6 @@
import core.stdc.stdlib: EXIT_SUCCESS, EXIT_FAILURE, exit; import core.stdc.stdlib: EXIT_SUCCESS, EXIT_FAILURE, exit;
import core.memory, core.time, core.thread; import core.memory, core.time, core.thread;
import std.getopt, std.file, std.path, std.process, std.stdio, std.conv, std.algorithm.searching, std.string; import std.getopt, std.file, std.path, std.process, std.stdio, std.conv, std.algorithm.searching, std.string;
import std.algorithm.sorting: sort;
import config, itemdb, monitor, onedrive, selective, sync, util; import config, itemdb, monitor, onedrive, selective, sync, util;
import std.net.curl: CurlException; import std.net.curl: CurlException;
import core.stdc.signal; import core.stdc.signal;
@ -62,7 +61,7 @@ int main(string[] args)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// update configuration from command line args // update configuration from command line args
savedOpts ~= cfg.update_from_args(args); cfg.update_from_args(args);
// Are we able to reach the OneDrive Service // Are we able to reach the OneDrive Service
@ -203,7 +202,7 @@ int main(string[] args)
// create-directory, remove-directory, source-directory, destination-directory // create-directory, remove-directory, source-directory, destination-directory
// are activities that dont perform a sync no error message for these items either // are activities that dont perform a sync no error message for these items either
if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get-o365-drive-id") != "") || (cfg.getValueBool("display_sync_status") == true)) { if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_o365_drive_id") != "") || (cfg.getValueBool("display_sync_status") == true)) {
performSyncOK = true; performSyncOK = true;
} }
@ -294,8 +293,8 @@ int main(string[] args)
} }
// Are we obtaining the Office 365 Drive ID for a given Office 365 SharePoint Shared Library? // Are we obtaining the Office 365 Drive ID for a given Office 365 SharePoint Shared Library?
if (cfg.getValueString("get-o365-drive-id") != ""){ if (cfg.getValueString("get_o365_drive_id") != ""){
sync.querySiteCollectionForDriveID(cfg.getValueString("get-o365-drive-id")); sync.querySiteCollectionForDriveID(cfg.getValueString("get_o365_drive_id"));
} }
// Are we displaying the sync status of the client? // Are we displaying the sync status of the client?
@ -553,44 +552,4 @@ extern(C) nothrow @nogc @system void exitHandler(int value) {
} catch(Exception e) {} } catch(Exception e) {}
exit(0); exit(0);
} }
void outputLongHelp(Option[] opt)
{
auto argsNeedingOptions = [
"--confdir",
"--create-directory",
"--destination-directory",
"--get-O365-drive-id",
"--remove-directory",
"--single-directory",
"--source-directory",
"--syncdir" ];
writeln(`OneDrive - a client for OneDrive Cloud Services
Usage:
onedrive [options] --synchronize
Do a one time synchronization
onedrive [options] --monitor
Monitor filesystem and sync regularly
onedrive [options] --display-config
Display the currently used configuration
onedrive [options] --display-sync-status
Query OneDrive service and report on pending changes
onedrive -h | --help
Show this help screen
onedrive --version
Show version
Options:
`);
foreach (it; opt.sort!("a.optLong < b.optLong")) {
if (it.optLong == "--help") continue;
writefln(" %s%s%s%s\n %s",
it.optLong,
it.optShort == "" ? "" : " " ~ it.optShort,
argsNeedingOptions.canFind(it.optLong) ? " ARG" : "",
it.required ? " (required)" : "", it.help);
}
// write help last
writefln(" --help -h\n This help information.");
}