Skip to content

Commit

Permalink
Help on zero arguments (#9693)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnv1 authored Sep 30, 2024
1 parent 72a6e45 commit 63af4d0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ydb/apps/ydb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ int main(int argc, char **argv) {
try {
return NYdb::NConsoleClient::NewYCloudClient(argc, argv);
}
catch (const NYdb::NConsoleClient::TMisuseWithHelpException& e) {
// command help is already printed. Just exit(1)
return EXIT_FAILURE;
}
catch (const NYdb::NConsoleClient::TMisuseException& e) {
Cerr << e.what() << Endl;
Cerr << "Try \"--help\" option for more info." << Endl;
Expand Down
5 changes: 3 additions & 2 deletions ydb/public/lib/ydb_cli/commands/ydb_service_scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ void TCommandExecuteYqlScript::Parse(TConfig& config) {
ParseInputFormats();
ParseOutputFormats();
if (!Script && !ScriptFile) {
throw TMisuseException() << "Neither \"Text of script\" (\"--script\", \"-s\") "
<< "nor \"Path to file with script text\" (\"--file\", \"-f\") were provided.";
Cerr << "Neither \"Text of script\" (\"--script\", \"-s\") "
<< "nor \"Path to file with script text\" (\"--file\", \"-f\") were provided." << Endl;
config.PrintHelpAndExit();
}
if (Script && ScriptFile) {
throw TMisuseException() << "Both mutually exclusive options \"Text of script\" (\"--script\", \"-s\") "
Expand Down
11 changes: 6 additions & 5 deletions ydb/public/lib/ydb_cli/commands/ydb_service_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ int TCommandDropTable::Run(TConfig& config) {
return EXIT_SUCCESS;
}

void TCommandQueryBase::CheckQueryOptions() const {
void TCommandQueryBase::CheckQueryOptions(TClientCommand::TConfig& config) const {
if (!Query && !QueryFile) {
throw TMisuseException() << "Neither \"Text of query\" (\"--query\", \"-q\") "
<< "nor \"Path to file with query text\" (\"--file\", \"-f\") were provided.";
Cerr << "Neither \"Text of query\" (\"--query\", \"-q\") "
<< "nor \"Path to file with query text\" (\"--file\", \"-f\") were provided." << Endl;
config.PrintHelpAndExit();
}
if (Query && QueryFile) {
throw TMisuseException() << "Both mutually exclusive options \"Text of query\" (\"--query\", \"-q\") "
Expand Down Expand Up @@ -403,7 +404,7 @@ void TCommandExecuteQuery::Parse(TConfig& config) {
|| BatchMode != EBatchMode::Default) && QueryType == "scheme") {
throw TMisuseException() << "Scheme queries does not support parameter options.";
}
CheckQueryOptions();
CheckQueryOptions(config);
CheckQueryFile();
ParseParameters(config);
}
Expand Down Expand Up @@ -847,7 +848,7 @@ void TCommandExplain::SaveDiagnosticsToFile(const TString& diagnostics) {
void TCommandExplain::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParseOutputFormats();
CheckQueryOptions();
CheckQueryOptions(config);
}

int TCommandExplain::Run(TConfig& config) {
Expand Down
2 changes: 1 addition & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_service_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class TCommandDropTable : public TTableCommand, public TCommandWithPath {

class TCommandQueryBase {
protected:
void CheckQueryOptions() const;
void CheckQueryOptions(TClientCommand::TConfig& config) const;
void CheckQueryFile();

protected:
Expand Down
5 changes: 5 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ void TCommandSql::Parse(TConfig& config) {
Query = ReadFromFile(QueryFile, "query");
}
}
if (Query.Empty()) {
Cerr << "Neither text of script (\"--script\", \"-s\") "
<< "nor path to file with script text (\"--file\", \"-f\") were provided." << Endl;
config.PrintHelpAndExit();
}
// Should be called after setting ReadingSomethingFromStdin
ParseParameters(config);
}
Expand Down
5 changes: 5 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_yql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ void TCommandYql::Parse(TConfig& config) {
if (ScriptFile) {
Script = ReadFromFile(ScriptFile, "script");
}
if (Script.Empty()) {
Cerr << "Neither text of script (\"--script\", \"-s\") "
<< "nor path to file with script text (\"--file\", \"-f\") were provided." << Endl;
config.PrintHelpAndExit();
}
if(FlameGraphPath && FlameGraphPath->Empty())
{
throw TMisuseException() << "FlameGraph path can not be empty.";
Expand Down
30 changes: 22 additions & 8 deletions ydb/public/lib/ydb_cli/common/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,37 @@ class TClientCommand {
bool minFailed = minSet && count < minValue;
bool maxFailed = maxSet && count > maxValue;
if (minFailed || maxFailed) {
TStringBuilder errorMessage;
if (minSet && maxSet) {
if (minValue == maxValue) {
throw TMisuseException() << "Command " << ArgV[0]
errorMessage << "Command " << ArgV[0]
<< " requires exactly " << minValue << " free arg(s).";
} else {
errorMessage << "Command " << ArgV[0]
<< " requires from " << minValue << " to " << maxValue << " free arg(s).";
}
throw TMisuseException() << "Command " << ArgV[0]
<< " requires from " << minValue << " to " << maxValue << " free arg(s).";
}
if (minFailed) {
throw TMisuseException() << "Command " << ArgV[0]
} else if (minFailed) {
errorMessage << "Command " << ArgV[0]
<< " requires at least " << minValue << " free arg(s).";
} else {
errorMessage << "Command " << ArgV[0]
<< " requires at most " << maxValue << " free arg(s).";
}
if (count == 0) {
Cerr << errorMessage << Endl;
PrintHelpAndExit();
} else {
throw TMisuseException() << errorMessage;
}
throw TMisuseException() << "Command " << ArgV[0]
<< " requires at most " << maxValue << " free arg(s).";
}
}

void PrintHelpAndExit() {
NLastGetopt::TOptsParser parser(Opts, ArgC, ArgV);
parser.PrintUsage(Cerr);
throw TMisuseWithHelpException();
}

private:
size_t GetParamsCount() {
size_t result = 0;
Expand Down
4 changes: 4 additions & 0 deletions ydb/public/lib/ydb_cli/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ namespace NConsoleClient {
const TString HomeDir = GetHomeDir();
#endif

// Print 'Try "--help" option for more info'
class TMisuseException : public yexception {};

// Print command help
class TMisuseWithHelpException : public TMisuseException {};

class TProfileConfig {
public:
TProfileConfig(const TString& profileName);
Expand Down

0 comments on commit 63af4d0

Please sign in to comment.