Added command_line::is_yes

This commit is contained in:
Lee Clagett 2016-11-25 21:07:45 -05:00
parent c36cb54340
commit 91ffb61c25
6 changed files with 673 additions and 567 deletions

View File

@ -29,11 +29,22 @@
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include "command_line.h"
#include "string_tools.h"
#include <boost/algorithm/string/compare.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "common/i18n.h"
#include "cryptonote_config.h"
#include "string_tools.h"
namespace command_line
{
namespace
{
const char* tr(const char* str)
{
return i18n_translate(str, "command_line");
}
}
std::string input_line(const std::string& prompt)
{
std::cout << prompt;
@ -45,6 +56,20 @@ namespace command_line
}
bool is_yes(const std::string& str)
{
if (str == "y" || str == "Y")
return true;
boost::algorithm::is_iequal ignore_case{};
if (boost::algorithm::equals("yes", str, ignore_case))
return true;
if (boost::algorithm::equals(command_line::tr("yes"), str, ignore_case))
return true;
return false;
}
const arg_descriptor<bool> arg_help = {"help", "Produce help message"};
const arg_descriptor<bool> arg_version = {"version", "Output version information"};
const arg_descriptor<std::string> arg_data_dir = {"data-dir", "Specify data directory"};

View File

@ -43,6 +43,9 @@ namespace command_line
std::string input_line(const std::string& prompt);
//! \return True if `str` is `is_iequal("y" || "yes" || `tr("yes"))`.
bool is_yes(const std::string& str);
template<typename T, bool required = false>
struct arg_descriptor;

View File

@ -212,17 +212,17 @@ namespace
return message_writer(epee::log_space::console_color_red, true, sw::tr("Error: "), LOG_LEVEL_0);
}
bool is_it_true(std::string s)
bool is_it_true(const std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
if (s == "true")
if (s == "1" || command_line::is_yes(s))
return true;
if (s == "1")
boost::algorithm::is_iequal ignore_case{};
if (boost::algorithm::equals("true", s, ignore_case))
return true;
if (s == "y" || s == "yes")
return true;
if (s == sw::tr("yes"))
if (boost::algorithm::equals(simple_wallet::tr("true"), s, ignore_case))
return true;
return false;
}
@ -916,7 +916,7 @@ bool simple_wallet::ask_wallet_create_if_needed()
LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()");
return false;
}
if(is_it_true(confirm_creation))
if(command_line::is_yes(confirm_creation))
{
success_msg_writer() << tr("Generating new wallet...");
m_generate_new = wallet_path;
@ -1977,8 +1977,7 @@ bool simple_wallet::get_address_from_str(const std::string &str, cryptonote::acc
{
return false;
}
if (confirm_dns_ok != "Y" && confirm_dns_ok != "y" && confirm_dns_ok != "Yes" && confirm_dns_ok != "yes"
&& confirm_dns_ok != tr("yes") && confirm_dns_ok != tr("no"))
if (!command_line::is_yes(confirm_dns_ok))
{
fail_msg_writer() << tr("you have cancelled the transfer request");
return false;
@ -2136,7 +2135,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)"));
if (std::cin.eof())
return true;
if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes")
if (!command_line::is_yes(accepted))
{
fail_msg_writer() << tr("transaction cancelled.");
@ -2220,7 +2219,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
std::string accepted = command_line::input_line(prompt.str());
if (std::cin.eof())
return true;
if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes")
if (!command_line::is_yes(accepted))
{
fail_msg_writer() << tr("transaction cancelled.");
@ -2399,7 +2398,7 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_)
std::string accepted = command_line::input_line(prompt_str);
if (std::cin.eof())
return true;
if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes")
if (!command_line::is_yes(accepted))
{
fail_msg_writer() << tr("transaction cancelled.");
@ -2613,7 +2612,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_)
std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)"));
if (std::cin.eof())
return true;
if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes")
if (!command_line::is_yes(accepted))
{
fail_msg_writer() << tr("transaction cancelled.");
@ -2658,7 +2657,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_)
std::string accepted = command_line::input_line(prompt_str);
if (std::cin.eof())
return true;
if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes")
if (!command_line::is_yes(accepted))
{
fail_msg_writer() << tr("transaction cancelled.");
@ -2853,8 +2852,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes,
uint64_t fee = amount - amount_to_dests;
std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min mixin %lu. %sIs this okay? (Y/Yes/N/No)")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_mixin % extra_message).str();
std::string accepted = command_line::input_line(prompt_str);
return is_it_true(accepted);
return command_line::is_yes(command_line::input_line(prompt_str));
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs)

View File

@ -35,6 +35,7 @@ set(unit_tests_sources
canonical_amounts.cpp
chacha8.cpp
checkpoints.cpp
command_line.cpp
decompose_amount_into_digits.cpp
dns_resolver.cpp
epee_boosted_tcp_server.cpp

View File

@ -0,0 +1,50 @@
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "common/command_line.h"
TEST(CommandLine, IsYes)
{
EXPECT_TRUE(command_line::is_yes("Y"));
EXPECT_TRUE(command_line::is_yes("y"));
EXPECT_TRUE(command_line::is_yes("YES"));
EXPECT_TRUE(command_line::is_yes("YEs"));
EXPECT_TRUE(command_line::is_yes("YeS"));
EXPECT_TRUE(command_line::is_yes("yES"));
EXPECT_TRUE(command_line::is_yes("Yes"));
EXPECT_TRUE(command_line::is_yes("yeS"));
EXPECT_TRUE(command_line::is_yes("yEs"));
EXPECT_TRUE(command_line::is_yes("yes"));
EXPECT_FALSE(command_line::is_yes(""));
EXPECT_FALSE(command_line::is_yes("yes-"));
EXPECT_FALSE(command_line::is_yes("NO"));
EXPECT_FALSE(command_line::is_yes("No"));
EXPECT_FALSE(command_line::is_yes("nO"));
EXPECT_FALSE(command_line::is_yes("no"));
}

File diff suppressed because it is too large Load Diff