nix: Show help when no arguments are given

Fixes #1464.
This commit is contained in:
Eelco Dolstra 2017-07-14 13:44:45 +02:00
parent 38374a9d35
commit 112ff7833d
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
4 changed files with 14 additions and 10 deletions

View file

@ -146,7 +146,7 @@ bool Args::processArgs(const Strings & args, bool finish)
res = true; res = true;
} }
if (finish && !expectedArgs.empty()) if (finish && !expectedArgs.empty() && !expectedArgs.front().optional)
throw UsageError("more arguments are required"); throw UsageError("more arguments are required");
return res; return res;

View file

@ -51,6 +51,7 @@ protected:
{ {
std::string label; std::string label;
size_t arity; // 0 = any size_t arity; // 0 = any
bool optional;
std::function<void(Strings)> handler; std::function<void(Strings)> handler;
}; };
@ -165,7 +166,7 @@ public:
/* Expect a string argument. */ /* Expect a string argument. */
void expectArg(const std::string & label, string * dest) 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(); *dest = ss.front();
}}); }});
} }
@ -173,7 +174,7 @@ public:
/* Expect 0 or more arguments. */ /* Expect 0 or more arguments. */
void expectArgs(const std::string & label, Strings * dest) 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; *dest = ss;
}}); }});
} }

View file

@ -24,7 +24,7 @@ void Command::printHelp(const string & programName, std::ostream & out)
MultiCommand::MultiCommand(const Commands & _commands) MultiCommand::MultiCommand(const Commands & _commands)
: commands(_commands) : commands(_commands)
{ {
expectedArgs.push_back(ExpectedArg{"command", 1, [=](Strings ss) { expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](Strings ss) {
assert(!command); assert(!command);
auto i = commands.find(ss.front()); auto i = commands.find(ss.front());
if (i == commands.end()) if (i == commands.end())

View file

@ -15,11 +15,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
{ {
NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix") NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix")
{ {
mkFlag('h', "help", "show usage information", [=]() { mkFlag('h', "help", "show usage information", [&]() { showHelpAndExit(); });
printHelp(programName, std::cout);
std::cout << "\nNote: this program is EXPERIMENTAL and subject to change.\n";
throw Exit();
});
mkFlag(0, "help-config", "show configuration options", [=]() { mkFlag(0, "help-config", "show configuration options", [=]() {
std::cout << "The following configuration options are available:\n\n"; 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 --<name> or --no-<name>. See nix\n" "Boolean settings can be overriden using --<name> or --no-<name>. See nix\n"
"--help-config for a list of configuration settings.\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) void mainWrapped(int argc, char * * argv)
@ -68,7 +71,7 @@ void mainWrapped(int argc, char * * argv)
args.parseCmdline(argvToStrings(argc, argv)); args.parseCmdline(argvToStrings(argc, argv));
assert(args.command); if (!args.command) args.showHelpAndExit();
StartProgressBar bar; StartProgressBar bar;