From 112ff7833d4f3a233755b2fe856b2eb2b3723254 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 14 Jul 2017 13:44:45 +0200 Subject: [PATCH] nix: Show help when no arguments are given Fixes #1464. --- src/libutil/args.cc | 2 +- src/libutil/args.hh | 5 +++-- src/nix/command.cc | 2 +- src/nix/main.cc | 15 +++++++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index df7e04087..0eed49454 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -146,7 +146,7 @@ bool Args::processArgs(const Strings & args, bool finish) res = true; } - if (finish && !expectedArgs.empty()) + if (finish && !expectedArgs.empty() && !expectedArgs.front().optional) throw UsageError("more arguments are required"); return res; diff --git a/src/libutil/args.hh b/src/libutil/args.hh index aa11373d5..ef8a7953e 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -51,6 +51,7 @@ protected: { std::string label; size_t arity; // 0 = any + bool optional; std::function handler; }; @@ -165,7 +166,7 @@ public: /* Expect a string argument. */ void expectArg(const std::string & label, string * dest) { - expectedArgs.push_back(ExpectedArg{label, 1, [=](Strings ss) { + expectedArgs.push_back(ExpectedArg{label, 1, false, [=](Strings ss) { *dest = ss.front(); }}); } @@ -173,7 +174,7 @@ public: /* Expect 0 or more arguments. */ void expectArgs(const std::string & label, Strings * dest) { - expectedArgs.push_back(ExpectedArg{label, 0, [=](Strings ss) { + expectedArgs.push_back(ExpectedArg{label, 0, false, [=](Strings ss) { *dest = ss; }}); } diff --git a/src/nix/command.cc b/src/nix/command.cc index 3c82e0df5..96b685a5b 100644 --- a/src/nix/command.cc +++ b/src/nix/command.cc @@ -24,7 +24,7 @@ void Command::printHelp(const string & programName, std::ostream & out) MultiCommand::MultiCommand(const Commands & _commands) : commands(_commands) { - expectedArgs.push_back(ExpectedArg{"command", 1, [=](Strings ss) { + expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](Strings ss) { assert(!command); auto i = commands.find(ss.front()); if (i == commands.end()) diff --git a/src/nix/main.cc b/src/nix/main.cc index 88a602b84..4b51c5ee1 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -15,11 +15,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs { NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix") { - mkFlag('h', "help", "show usage information", [=]() { - printHelp(programName, std::cout); - std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n"; - throw Exit(); - }); + mkFlag('h', "help", "show usage information", [&]() { showHelpAndExit(); }); mkFlag(0, "help-config", "show configuration options", [=]() { std::cout << "The following configuration options are available:\n\n"; @@ -47,6 +43,13 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs "Boolean settings can be overriden using ‘--’ or ‘--no-’. See ‘nix\n" "--help-config’ for a list of configuration settings.\n"; } + + void showHelpAndExit() + { + printHelp(programName, std::cout); + std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n"; + throw Exit(); + } }; void mainWrapped(int argc, char * * argv) @@ -68,7 +71,7 @@ void mainWrapped(int argc, char * * argv) args.parseCmdline(argvToStrings(argc, argv)); - assert(args.command); + if (!args.command) args.showHelpAndExit(); StartProgressBar bar;