Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #62 from sergeyklay/feature/version
Browse files Browse the repository at this point in the history
Add --dumpversion and --vernum program options
  • Loading branch information
sergeyklay authored Jul 25, 2021
2 parents 86f415b + c80afb0 commit 229286f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 44 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ releases, in reverse chronological order.
during the pCloud API calls.
* Reworked commands mode to provide better user experience.
* Added `-V`, `--version` program options to display program version information.
* Added `--dumpversion` program option to print the version of the client and
don't do anything else.
* Added `--vernum` program option to print the version of the client as integer
and quit.

### Bugfix

Expand Down Expand Up @@ -80,3 +84,4 @@ releases, in reverse chronological order.
into `poverlay-posix.c`.
* Moved `PSYNC_CRYPTO_*` defines from `psynclib.h` to `pcloudcrypto.h`.
* Replace `boost::program_options` by more lightweight and specialized library `CLI11`.
* Rename `--commands_only` CLI option to `--commands-only`.
40 changes: 27 additions & 13 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ output short options description:

```sh
$ pcloudcc --help
Usage: pcloudcc [options]
________ __
____ / ____/ /___ __ ______/ /
/ __ \/ / / / __ \/ / / / __ /
/ /_/ / /___/ / /_/ / /_/ / /_/ /
/ .___/\____/_/\____/\__,_/\__,_/
/_/

pCloud Console Client 3.0.0

Usage:
pcloudcc [options] [--] [arguments]

Options:
-h [ --help ] Display this information.
-V [ --version ] Display program version information.
-u [ --username ] arg pCloud account name.
-p [ --password ] Ask pCloud account password.
-c [ --crypto ] Ask crypto password.
-y [ --passascrypto ] arg Use user password as crypto password also.
-d [ --daemonize ] Daemonize the process.
-o [ --commands ] Parent stays alive and processes commands.
-m [ --mountpoint ] arg Mount point where drive to be mounted.
-k [ --commands_only ] Daemon already started pass only commands.
-n [ --newuser ] Switch if this is a new user to be registered.
-s [ --savepassword ] Save password in database.
-k, --commands-only Daemon already started pass only commands
-d, --daemonize Daemonize the process
-u, --username arg pCloud account name
-p, --password Ask pCloud account password
-c, --crypto Ask crypto password
-y, --passascrypto Use user password as crypto password also
-m, --mountpoint arg Mount point where drive to be mounted
-n, --newuser Use if this is a new user to be registered
-o, --commands Parent stays alive and processes commands
-s, --savepassword Save password in database
-V, --version Print client version information and quit
--vernum Print the version of the client as integer and quit
--dumpversion Print the version of the client and don't do anything else
-h, --help Print this help message and quit
```
Also, there are several commands that the running service can execute. Commands are passed using
Expand Down
155 changes: 124 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,92 @@ static inline std::vector<std::string> prepare_args(int argc, char** argv) {
return args;
}

class PcloudFormatter : public CLI::Formatter {
public:
std::string make_usage(const CLI::App *app,
const std::string /* name */) const override {

auto out = get_label("Usage") + ":\n";
out += " " + app->get_name();


auto groups = app->get_groups();

// Print an OPTIONS badge if any options exist
auto non_positionals = app->get_options(
[](const CLI::Option *opt) { return opt->nonpositional(); });
if (!non_positionals.empty()) {
out += " [" + get_label("OPTIONS") + "]";
}

// Print an ARGUMENTS badge if any arguments exist
// or we're show help for the main program
auto positionals = app->get_options(
[](const CLI::Option *opt) { return opt->get_positional(); });
if (!app->get_parent() || !positionals.empty()) {
out += " [--] [" + get_label("ARGUMENTS") + "]";
}

return out += "\n";
}

std::string make_description(const CLI::App *app) const override {
std::string out;

const auto BANNER = R"BANNER(
________ __
____ / ____/ /___ __ ______/ /
/ __ \/ / / / __ \/ / / / __ /
/ /_/ / /___/ / /_/ / /_/ / /_/ /
/ .___/\____/_/\____/\__,_/\__,_/
/_/)BANNER";

std::string banner(BANNER);
out += banner.replace(0, 1, "") + "\n\n";

auto desc = app->get_description();
out += desc + "\n\n";

return out;
}

std::string make_option_name(const CLI::Option *opt, bool is_positional) const override {
std::string name;
if(is_positional)
name = opt->get_name(true, false);
else
name = opt->get_name(false, true);

std::string new_name;
if (name[0] == '-' && name[1] == '-') {
new_name = " " + name;
} else {
for(char i : name) {
if(i != ',')
new_name += i;
else {
new_name += i;
new_name += " ";
}
}
}

return new_name;
}
};

int main(int argc, char** argv) {
auto args = prepare_args(argc, argv);
auto out =
auto const description =
std::string(PCLOUD_PACKAGE_NAME) + " " + std::string(PCLOUD_VERSION);

CLI::App app{out};
app.description(out);
app.name("pcloudcc");
CLI::App app{description, "pcloudcc"};

app.formatter(std::make_shared<PcloudFormatter>());
app.get_formatter()->column_width(26);
app.get_formatter()->label("OPTIONS", "options");
app.get_formatter()->label("ARGUMENTS", "arguments");
app.get_formatter()->label("TEXT", "arg");

// Global flag & options

Expand All @@ -51,7 +126,8 @@ int main(int argc, char** argv) {
"Daemonize the process");

std::string username;
app.add_option("--username,-u", username, "pCloud account name");
app.add_option("--username,-u", username,
"pCloud account name");

bool passwordsw = false;
app.add_flag("--password,-p", passwordsw, "Ask pCloud account password");
Expand Down Expand Up @@ -79,50 +155,68 @@ int main(int argc, char** argv) {
app.add_flag("--savepassword,-s", commands,
"Save password in database");

CLI::Option *version = app.add_flag(
"-V, --version",
auto version = [](int /* count */){
std::cout << PCLOUD_VERSION_FULL << " ("
<< PSYNC_VERSION_FULL
<< ") " << std::endl;

std::cout << "Copyright " << PCLOUD_COPYRIGHT << "." << std::endl;

std::cout << "This is free software; see the source for copying "
"conditions. There is NO"
<< std::endl;

std::cout << "warranty; not even for MERCHANTABILITY or FITNESS FOR A "
"PARTICULAR PURPOSE."
<< std::endl
<< std::endl;
exit(EXIT_SUCCESS);
};
app.add_flag_function(
"--version,-V",
version,
"Print client version information and quit");

auto vernum = [](int /* count */){
std::cout << PCLOUD_VERSION_ID << std::endl;
exit(EXIT_SUCCESS);
};
app.add_flag_function(
"--vernum",
vernum,
"Print the version of the client as integer and quit");

auto dumpversion = [](int /* count */){
std::cout << PCLOUD_VERSION << std::endl;
exit(EXIT_SUCCESS);
};
app.add_flag_function(
"--dumpversion",
dumpversion,
"Print the version of the client and don't do anything else");

// Remove help flag because it shortcuts all processing
app.set_help_flag();

// Add custom flag that activates help
auto help = app.add_flag("-h, --help", "Print this help message and quit");

// Process commands
try {
app.parse(args);

if (commands_only) {
control_tools::process_commands();
return EXIT_SUCCESS;
}

if (*help) {
throw CLI::CallForHelp();
}

if (*version) {
std::cout << PCLOUD_VERSION_FULL << " ("
<< PSYNC_VERSION_FULL
<< ") " << std::endl;

std::cout << "Copyright " << PCLOUD_COPYRIGHT << "." << std::endl;

std::cout << "This is free software; see the source for copying "
"conditions. There is NO"
<< std::endl;

std::cout << "warranty; not even for MERCHANTABILITY or FITNESS FOR A "
"PARTICULAR PURPOSE."
<< std::endl
<< std::endl;
return 0;
if (commands_only) {
control_tools::process_commands();
return EXIT_SUCCESS;
}

if (username.empty()) {
std::cout << "Username option is required" << std::endl;
std::cout << "Daemonize" << daemonize << std::endl;
return 1;
return EXIT_FAILURE;
}
console_client::clibrary::pclcli::get_lib().set_username(username);

Expand Down Expand Up @@ -173,6 +267,5 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}


return EXIT_SUCCESS;
}

0 comments on commit 229286f

Please sign in to comment.