Skip to content

Commit 3e942bb

Browse files
committed
BUG#35112454 mysqlsh: unknown option --loose-default-character-set
This patch adds supports for the --loose prefix in the command line options. Prints a WARNING instead of throwing error when the an unsupported command line option is prefixed with --loose. Change-Id: I3f3d1841862425aec1fa5d61e76d298c4f5b5d8d
1 parent 43d1cf3 commit 3e942bb

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

mysqlshdk/libs/utils/options.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0,
@@ -415,10 +415,19 @@ void Options::handle_cmdline_options(
415415
it->first + " requires an argument");
416416
}
417417
}
418-
} else if (!allow_unregistered_options) {
418+
} else if (!allow_unregistered_options && !iterator.is_loose()) {
419419
throw std::invalid_argument(argv[0] + std::string(": unknown option ") +
420420
opt);
421421
} else {
422+
if (iterator.is_loose()) {
423+
auto loose_option = iterator.option();
424+
loose_option.insert(2, "loose-");
425+
if (Iterator::Type::LONG == iterator.type()) {
426+
loose_option.append("=").append(iterator.value());
427+
}
428+
m_invalid_loose_options.push_back(std::move(loose_option));
429+
}
430+
422431
if (Iterator::Type::LONG == iterator.type()) {
423432
iterator.next();
424433
} else {
@@ -617,7 +626,6 @@ void Options::Iterator::get_data() {
617626
if (data[1] == '-') {
618627
// long option
619628
const auto pos = data.find('=');
620-
621629
if (std::string::npos == pos) {
622630
m_option = std::move(data);
623631
get_separate_value();
@@ -626,6 +634,11 @@ void Options::Iterator::get_data() {
626634
m_option = data.substr(0, pos);
627635
m_value = m_iterator.peek() + pos + 1;
628636
}
637+
638+
if (shcore::str_beginswith(m_option, "--loose-")) {
639+
m_loose = true;
640+
m_option.replace(2, 6, "");
641+
}
629642
} else {
630643
// short option
631644
if (data.length() > 2) {

mysqlshdk/libs/utils/options.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0,
@@ -505,6 +505,11 @@ class Options {
505505
*/
506506
void next();
507507

508+
/**
509+
* Returns true if the option is loose, i.e. --loose-option
510+
*/
511+
bool is_loose() const { return m_loose; }
512+
508513
private:
509514
void get_data();
510515

@@ -515,6 +520,7 @@ class Options {
515520
const char *m_value;
516521
Type m_type;
517522
size_t m_short_value_pos = 0;
523+
bool m_loose = false;
518524
};
519525

520526
using Custom_cmdline_handler = std::function<bool(Iterator *)>;
@@ -584,6 +590,10 @@ class Options {
584590
std::size_t output_width = 80,
585591
std::size_t front_padding = 0);
586592

593+
const std::vector<std::string> &get_invalid_loose_options() const {
594+
return m_invalid_loose_options;
595+
}
596+
587597
protected:
588598
struct Command_line_comparator {
589599
bool operator()(const std::string &lhs, const std::string &rhs) const;
@@ -597,6 +607,7 @@ class Options {
597607
std::map<std::string, opts::Generic_option *, Command_line_comparator>
598608
cmdline_options;
599609
std::string config_file;
610+
std::vector<std::string> m_invalid_loose_options;
600611
};
601612

602613
} /* namespace shcore */

src/mysqlsh/main.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,14 @@ int main(int argc, char **argv) {
798798
log_debug("Using color mode %i",
799799
static_cast<int>(mysqlshdk::textui::get_color_capability()));
800800

801+
if (options.quiet_start !=
802+
mysqlsh::Shell_options::Quiet_start::SUPRESS_INFO) {
803+
for (const auto &option : shell_options->get_invalid_loose_options()) {
804+
mysqlsh::current_console()->print_warning(
805+
shcore::str_format("unknown option: %s", option.c_str()));
806+
}
807+
}
808+
801809
if (shell_options->action_print_version()) {
802810
std::string version_msg =
803811
version_string(argv[0], shell_options->action_print_version_extra());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#@<> Warnins on unknown loose options
2+
testutil.call_mysqlsh(["--loose-whatever", "-e", "print('done')"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
3+
EXPECT_STDOUT_CONTAINS("WARNING: unknown option: --loose-whatever")
4+
WIPE_OUTPUT()
5+
6+
#@<> Warnins on unknown loose options with --quiet-start=1
7+
testutil.call_mysqlsh(["--quiet-start=1", "--loose-whatever", "-e", "print('done')"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
8+
EXPECT_STDOUT_CONTAINS("WARNING: unknown option: --loose-whatever")
9+
WIPE_OUTPUT()
10+
11+
#@<> No warnins on unknown loose options with --quiet-start=2
12+
testutil.call_mysqlsh(["--quiet-start=2", "--loose-whatever", "-e", "print('done')"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
13+
EXPECT_STDOUT_NOT_CONTAINS("WARNING: unknown option: --loose-whatever")
14+
WIPE_OUTPUT()
15+
16+
#@<> Existing options can be prefixed with --loose
17+
testutil.call_mysqlsh(["--loose-quiet-start=1", "--loose-whatever", "-e", "print('done')"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
18+
EXPECT_STDOUT_NOT_CONTAINS("WARNING: unknown option: --loose-quiet-start")
19+
EXPECT_STDOUT_CONTAINS("WARNING: unknown option: --loose-whatever")
20+
WIPE_OUTPUT()
21+
22+
#@<> Unknown loose option with URI value as --loose-option=URI ignores value
23+
testutil.call_mysqlsh([f"--loose-whatever={__mysqluripwd}", "-e", "print(session)"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
24+
EXPECT_STDOUT_CONTAINS(f"WARNING: unknown option: --loose-whatever={__mysqluripwd}")
25+
EXPECT_STDOUT_NOT_CONTAINS(f"<ClassicSession:{__mysql_uri}>")
26+
EXPECT_STDOUT_CONTAINS("null")
27+
WIPE_OUTPUT()
28+
29+
#@<> Unknown loose option with URI value as --loose-option URI uses value
30+
testutil.call_mysqlsh([f"--loose-whatever", __mysqluripwd, "-e", "print(session)"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
31+
EXPECT_STDOUT_CONTAINS("WARNING: unknown option: --loose-whatever")
32+
EXPECT_STDOUT_CONTAINS(f"<ClassicSession:{__mysql_uri}>")
33+
EXPECT_STDOUT_NOT_CONTAINS("null")
34+
WIPE_OUTPUT()
35+
36+
#@<> Warns unsupported loose options from options file
37+
testutil.create_file("my_options.cnf", """
38+
[client]
39+
loose-unknown=whatever
40+
""")
41+
testutil.call_mysqlsh(["--defaults-file=my_options.cnf", "-e", "print('done')"], "", ["MYSQLSH_TERM_COLOR_MODE=nocolor"])
42+
EXPECT_STDOUT_CONTAINS("WARNING: unknown option: --loose-unknown")
43+
WIPE_OUTPUT()
44+
testutil.rmfile("my_options.cnf")

0 commit comments

Comments
 (0)