Electroneum
command_line.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <iostream>
35 #include <type_traits>
36 
37 #include <boost/program_options/parsers.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 #include "include_base_utils.h"
41 
42 namespace command_line
43 {
44 
45  std::string input_line(const std::string& prompt);
46 
48  bool is_yes(const std::string& str);
49 
50  template<typename T, bool required = false>
52 
53  template<typename T>
55  {
56  typedef T value_type;
57 
58  const char* name;
59  const char* description;
62  };
63 
64  template<typename T>
65  struct arg_descriptor<std::vector<T>, false>
66  {
67  typedef std::vector<T> value_type;
68 
69  const char* name;
70  const char* description;
71  };
72 
73  template<typename T>
75  {
76  static_assert(!std::is_same<T, bool>::value, "Boolean switch can't be required");
77 
78  typedef T value_type;
79 
80  const char* name;
81  const char* description;
82  };
83 
84  template<typename T>
85  boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, true>& /*arg*/)
86  {
87  return boost::program_options::value<T>()->required();
88  }
89 
90  template<typename T>
91  boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg)
92  {
93  auto semantic = boost::program_options::value<T>();
94  if (!arg.not_use_default)
95  semantic->default_value(arg.default_value);
96  return semantic;
97  }
98 
99  template<typename T>
100  boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg, const T& def)
101  {
102  auto semantic = boost::program_options::value<T>();
103  if (!arg.not_use_default)
104  semantic->default_value(def);
105  return semantic;
106  }
107 
108  template<typename T>
109  boost::program_options::typed_value<std::vector<T>, char>* make_semantic(const arg_descriptor<std::vector<T>, false>& /*arg*/)
110  {
111  auto semantic = boost::program_options::value< std::vector<T> >();
112  semantic->default_value(std::vector<T>(), "");
113  return semantic;
114  }
115 
116  template<typename T, bool required>
117  void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required>& arg, bool unique = true)
118  {
119  if (0 != description.find_nothrow(arg.name, false))
120  {
121  CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
122  return;
123  }
124 
125  description.add_options()(arg.name, make_semantic(arg), arg.description);
126  }
127 
128  template<typename T>
129  void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, false>& arg, const T& def, bool unique = true)
130  {
131  if (0 != description.find_nothrow(arg.name, false))
132  {
133  CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
134  return;
135  }
136 
137  description.add_options()(arg.name, make_semantic(arg, def), arg.description);
138  }
139 
140  template<>
141  inline void add_arg(boost::program_options::options_description& description, const arg_descriptor<bool, false>& arg, bool unique)
142  {
143  if (0 != description.find_nothrow(arg.name, false))
144  {
145  CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
146  return;
147  }
148 
149  description.add_options()(arg.name, boost::program_options::bool_switch(), arg.description);
150  }
151 
152  template<typename charT>
153  boost::program_options::basic_parsed_options<charT> parse_command_line(int argc, const charT* const argv[],
154  const boost::program_options::options_description& desc, bool allow_unregistered = false)
155  {
156  auto parser = boost::program_options::command_line_parser(argc, argv);
157  parser.options(desc);
158  if (allow_unregistered)
159  {
160  parser.allow_unregistered();
161  }
162  return parser.run();
163  }
164 
165  template<typename F>
166  bool handle_error_helper(const boost::program_options::options_description& desc, F parser)
167  {
168  try
169  {
170  return parser();
171  }
172  catch (const std::exception& e)
173  {
174  std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
175  std::cerr << desc << std::endl;
176  return false;
177  }
178  catch (...)
179  {
180  std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
181  std::cerr << desc << std::endl;
182  return false;
183  }
184  }
185 
186  template<typename T, bool required>
187  bool has_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
188  {
189  auto value = vm[arg.name];
190  return !value.empty();
191  }
192 
193 
194  template<typename T, bool required>
195  T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
196  {
197  return vm[arg.name].template as<T>();
198  }
199 
200  template<>
201  inline bool has_arg<bool, false>(const boost::program_options::variables_map& vm, const arg_descriptor<bool, false>& arg)
202  {
203  return get_arg<bool, false>(vm, arg);
204  }
205 
206 
207  extern const arg_descriptor<bool> arg_help;
208  extern const arg_descriptor<bool> arg_version;
209  extern const arg_descriptor<std::string> arg_data_dir;
210  extern const arg_descriptor<std::string> arg_testnet_data_dir;
211  extern const arg_descriptor<bool> arg_test_drop_download;
212  extern const arg_descriptor<uint64_t> arg_test_drop_download_height;
213  extern const arg_descriptor<int> arg_test_dbg_lock_sleep;
214  extern const arg_descriptor<bool, false> arg_testnet_on;
215  extern const arg_descriptor<bool> arg_dns_checkpoints;
216  extern const arg_descriptor<uint64_t> arg_fast_block_sync;
217  extern const arg_descriptor<uint64_t> arg_prep_blocks_threads;
218  extern const arg_descriptor<uint64_t> arg_show_time_stats;
219  extern const arg_descriptor<size_t> arg_block_sync_size;
220  extern const arg_descriptor<std::string> arg_check_updates;
221  extern const arg_descriptor<bool> arg_disable_fluffy_blocks;
222  extern const arg_descriptor<std::string> arg_validator_key;
223 }
const arg_descriptor< bool, false > arg_testnet_on
Definition: command_line.cpp:89
T value_type
Definition: command_line.h:56
const uint32_t T[512]
Definition: groestl_tables.h:34
const arg_descriptor< std::string > arg_validator_key
Definition: command_line.cpp:129
#define F(w, k)
Definition: sha512-blocks.c:61
const char * description
Definition: command_line.h:59
bool not_use_default
Definition: command_line.h:61
bool is_yes(const std::string &str)
Definition: command_line.cpp:68
bool has_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required > &arg)
Definition: command_line.h:187
const char * description
Definition: command_line.h:81
const arg_descriptor< bool > arg_disable_fluffy_blocks
Definition: command_line.cpp:124
const char * description
Definition: command_line.h:70
Definition: block_queue.cpp:41
const command_line::arg_descriptor< std::string > arg_check_updates
Definition: command_line.cpp:119
const arg_descriptor< bool > arg_help
Definition: command_line.cpp:82
const char * name
Definition: command_line.h:58
const char * name
Definition: command_line.h:69
const command_line::arg_descriptor< uint64_t > arg_prep_blocks_threads
Definition: command_line.cpp:104
const char * name
Definition: command_line.h:80
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:166
boost::program_options::typed_value< T, char > * make_semantic(const arg_descriptor< T, true > &)
Definition: command_line.h:85
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required > &arg, bool unique=true)
Definition: command_line.h:117
bool has_arg< bool, false >(const boost::program_options::variables_map &vm, const arg_descriptor< bool, false > &arg)
Definition: command_line.h:201
const arg_descriptor< int > arg_test_dbg_lock_sleep
Definition: command_line.cpp:88
#define false
Definition: stdbool.h:38
const arg_descriptor< std::string > arg_data_dir
Definition: command_line.cpp:84
const arg_descriptor< bool > arg_test_drop_download
Definition: command_line.cpp:86
Definition: command_line.cpp:44
const arg_descriptor< uint64_t > arg_test_drop_download_height
Definition: command_line.cpp:87
std::string input_line(const std::string &prompt)
Definition: command_line.cpp:54
internal::NamedArgWithType< char, T > arg(StringRef name, const T &arg)
Definition: format.h:3450
const command_line::arg_descriptor< uint64_t > arg_fast_block_sync
Definition: command_line.cpp:99
const command_line::arg_descriptor< uint64_t > arg_show_time_stats
Definition: command_line.cpp:109
const arg_descriptor< bool > arg_dns_checkpoints
Definition: command_line.cpp:94
boost::program_options::basic_parsed_options< charT > parse_command_line(int argc, const charT *const argv[], const boost::program_options::options_description &desc, bool allow_unregistered=false)
Definition: command_line.h:153
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required > &arg)
Definition: command_line.h:195
const arg_descriptor< std::string > arg_testnet_data_dir
Definition: command_line.cpp:85
T value_type
Definition: command_line.h:76
Definition: command_line.h:74
Definition: command_line.h:51
const arg_descriptor< bool > arg_version
Definition: command_line.cpp:83
std::vector< T > value_type
Definition: command_line.h:67
#define true
Definition: stdbool.h:37
T default_value
Definition: command_line.h:60
Definition: command_line.h:54
const command_line::arg_descriptor< size_t > arg_block_sync_size
Definition: command_line.cpp:114