From 3c9d699d5b7dfb68202e2c91f87e6f060fffcf5c Mon Sep 17 00:00:00 2001 From: Daniel Kroening Date: Fri, 6 Dec 2024 13:46:57 -0500 Subject: [PATCH] cmdlinet: add value_opt methods This adds an alternative to cmdlinet::get_value, which returns std::optional, as opposed to defaulting to an empty string when the option isn't set. --- src/goto-cc/armcc_mode.cpp | 7 ++----- src/goto-cc/gcc_mode.cpp | 5 +---- .../goto_instrument_parse_options.cpp | 8 ++++---- src/util/cmdline.cpp | 14 ++++++++++++-- src/util/cmdline.h | 3 +++ 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/goto-cc/armcc_mode.cpp b/src/goto-cc/armcc_mode.cpp index bacd0eee185..f1096a998ce 100644 --- a/src/goto-cc/armcc_mode.cpp +++ b/src/goto-cc/armcc_mode.cpp @@ -105,11 +105,8 @@ int armcc_modet::doit() } // armcc's default is .o - if(cmdline.isset("default_extension=")) - compiler.object_file_extension= - cmdline.get_value("default_extension="); - else - compiler.object_file_extension="o"; + compiler.object_file_extension = + cmdline.value_opt("default_extension=").value_or("o"); // note that ARM's default is "unsigned_chars", // in contrast to gcc's default! diff --git a/src/goto-cc/gcc_mode.cpp b/src/goto-cc/gcc_mode.cpp index 955ba856ba3..a3ffa2d5e16 100644 --- a/src/goto-cc/gcc_mode.cpp +++ b/src/goto-cc/gcc_mode.cpp @@ -982,10 +982,7 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler) else { // -c is not given - if(cmdline.isset('o')) - output_files.push_back(cmdline.get_value('o')); - else - output_files.push_back("a.out"); + output_files.push_back(cmdline.value_opt('o').value_or("a.out")); } if(output_files.empty() || diff --git a/src/goto-instrument/goto_instrument_parse_options.cpp b/src/goto-instrument/goto_instrument_parse_options.cpp index 6f65e04c567..3b60d0fe95d 100644 --- a/src/goto-instrument/goto_instrument_parse_options.cpp +++ b/src/goto-instrument/goto_instrument_parse_options.cpp @@ -241,8 +241,8 @@ int goto_instrument_parse_optionst::doit() if(cmdline.isset("log")) { - std::string filename=cmdline.get_value("log"); - bool have_file=!filename.empty() && filename!="-"; + std::string filename = cmdline.value_opt("log").value_or("-"); + bool have_file = filename != "-"; jsont result=goto_unwind.output_log_json(); @@ -1319,8 +1319,8 @@ void goto_instrument_parse_optionst::instrument_goto_program() } else { - std::string filename=cmdline.get_value("log"); - bool have_file=!filename.empty() && filename!="-"; + std::string filename = cmdline.value_opt("log").value_or("-"); + bool have_file = filename != "-"; jsont result = goto_function_inline_and_log( goto_model, function, ui_message_handler, true, caching); diff --git a/src/util/cmdline.cpp b/src/util/cmdline.cpp index a22b5e8b0e7..69998564513 100644 --- a/src/util/cmdline.cpp +++ b/src/util/cmdline.cpp @@ -46,13 +46,18 @@ bool cmdlinet::isset(const char *option) const } std::string cmdlinet::get_value(char option) const +{ + return value_opt(option).value_or(""); +} + +std::optional cmdlinet::value_opt(char option) const { auto i=getoptnr(option); if(i.has_value() && !options[*i].values.empty()) return options[*i].values.front(); else - return ""; + return {}; } void cmdlinet::set(const std::string &option, bool value) @@ -97,13 +102,18 @@ const std::list &cmdlinet::get_values(char option) const } std::string cmdlinet::get_value(const char *option) const +{ + return value_opt(option).value_or(""); +} + +std::optional cmdlinet::value_opt(const char *option) const { auto i=getoptnr(option); if(i.has_value() && !options[*i].values.empty()) return options[*i].values.front(); else - return ""; + return {}; } const std::list &cmdlinet::get_values( diff --git a/src/util/cmdline.h b/src/util/cmdline.h index bc624f678e1..bc7799d335c 100644 --- a/src/util/cmdline.h +++ b/src/util/cmdline.h @@ -76,6 +76,9 @@ class cmdlinet std::string get_value(char option) const; std::string get_value(const char *option) const; + std::optional value_opt(char option) const; + std::optional value_opt(const char *option) const; + const std::list &get_values(const std::string &option) const; const std::list &get_values(char option) const;