Geogram  Version 1.9.0
A programming library of geometric algorithms
command_line.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_COMMAND_LINE
41 #define GEOGRAM_BASIC_COMMAND_LINE
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/string.h>
46 #include <geogram/basic/assert.h>
47 
53 namespace GEO {
54 
60  namespace CmdLine {
61 
69  void GEOGRAM_API initialize();
70 
78  void GEOGRAM_API terminate();
79 
80 
91  void GEOGRAM_API set_config_file_name(
92  const std::string& filename,
93  bool auto_create_args = false
94  );
95 
104  bool GEOGRAM_API config_file_loaded();
105 
112  std::string GEOGRAM_API get_config_file_name();
113 
121  void GEOGRAM_API load_config(
122  const std::string& filename, const std::string& program_name
123  );
124 
128  enum ArgType {
132  ARG_INT = 1,
138  ARG_BOOL = 8,
140  ARG_PERCENT = 16
141  };
142 
146  enum ArgFlags {
150  ARG_ADVANCED = 1
151  };
152 
173  void GEOGRAM_API declare_arg_group(
174  const std::string& name,
175  const std::string& description,
177  );
178 
198  void GEOGRAM_API declare_arg(
199  const std::string& name,
200  ArgType type,
201  const std::string& default_value,
202  const std::string& description,
204  );
205 
212  ArgType GEOGRAM_API get_arg_type(const std::string& name);
213 
220  bool GEOGRAM_API arg_is_declared(const std::string& name);
221 
229  inline void declare_arg(
230  const std::string& name,
231  const std::string& default_value,
232  const std::string& description,
234  ) {
235  declare_arg(
236  name, ARG_STRING, default_value,
237  description, flags
238  );
239  }
240 
248  inline void declare_arg(
249  const std::string& name,
250  const char* default_value,
251  const std::string& description,
253  ) {
254  declare_arg(
255  name, ARG_STRING, default_value,
256  description, flags
257  );
258  }
259 
267  inline void declare_arg(
268  const std::string& name,
269  int default_value,
270  const std::string& description,
272  ) {
273  declare_arg(
274  name, ARG_INT, String::to_string(default_value),
275  description, flags
276  );
277  }
278 
286  inline void declare_arg(
287  const std::string& name,
288  double default_value,
289  const std::string& description,
291  ) {
292  declare_arg(
293  name, ARG_DOUBLE, String::to_string(default_value),
294  description, flags
295  );
296  }
297 
305  inline void declare_arg(
306  const std::string& name,
307  bool default_value,
308  const std::string& description,
310  ) {
311  declare_arg(
312  name, ARG_BOOL, default_value ? "true" : "false",
313  description, flags
314  );
315  }
316 
326  inline void declare_arg_percent(
327  const std::string& name,
328  double default_value,
329  const std::string& description = "...",
331  ) {
332  declare_arg(
333  name, ARG_PERCENT, String::to_string(default_value) + "%",
334  description, flags
335  );
336  }
337 
376  bool GEOGRAM_API parse(
377  int argc, char** argv, std::vector<std::string>& unparsed_args,
378  const std::string& additional_arg_specs = ""
379  );
380 
390  bool GEOGRAM_API parse(
391  int argc, char** argv
392  );
393 
399  int GEOGRAM_API argc();
400 
401 
402  typedef char** charptrptr; // Need to do that else the compiler thinks
403  // that GEOGRAM_API qualifies the ptr instead
404  // of the function.
405 
412  charptrptr GEOGRAM_API argv();
413 
426  void GEOGRAM_API show_usage(
427  const std::string& additional_args = "",
428  bool advanced = false
429  );
430 
438  std::string GEOGRAM_API get_arg(const std::string& name);
439 
450  int GEOGRAM_API get_arg_int(const std::string& name);
451 
462  unsigned int GEOGRAM_API get_arg_uint(const std::string& name);
463 
474  double GEOGRAM_API get_arg_double(const std::string& name);
475 
494  double GEOGRAM_API get_arg_percent(
495  const std::string& name, double reference
496  );
497 
508  bool GEOGRAM_API get_arg_bool(const std::string& name);
509 
522  bool GEOGRAM_API set_arg(
523  const std::string& name, const std::string& value
524  );
525 
538  inline bool set_arg(const std::string& name, const char* value) {
539  return set_arg(name, std::string(value));
540  }
541 
552  void GEOGRAM_API set_arg(const std::string& name, Numeric::int32 value);
553 
554  /*
555  * \brief Sets an argument value from an integer
556  * \details This replaces the value of argument \p name by the given
557  * integer \p value. If the declared type of the argument is not
558  * compatible with an integer then the function aborts (compatible
559  * argument types are: int, double or string). If the argument does
560  * not exist, it is added as a new argument of undefined type.
561  * \param[in] name the argument name
562  * \param[in] value the new value as an integer
563  */
564  void GEOGRAM_API set_arg(
565  const std::string& name, Numeric::uint32 value
566  );
567 
568  /*
569  * \brief Sets an argument value from an integer
570  * \details This replaces the value of argument \p name by the given
571  * integer \p value. If the declared type of the argument is not
572  * compatible with an integer then the function aborts (compatible
573  * argument types are: int, double or string). If the argument does
574  * not exist, it is added as a new argument of undefined type.
575  * \param[in] name the argument name
576  * \param[in] value the new value as an integer
577  */
578  void GEOGRAM_API set_arg(const std::string& name, Numeric::int64 value);
579 
580  /*
581  * \brief Sets an argument value from an integer
582  * \details This replaces the value of argument \p name by the given
583  * integer \p value. If the declared type of the argument is not
584  * compatible with an integer then the function aborts (compatible
585  * argument types are: int, double or string). If the argument does
586  * not exist, it is added as a new argument of undefined type.
587  * \param[in] name the argument name
588  * \param[in] value the new value as an integer
589  */
590  void GEOGRAM_API set_arg(
591  const std::string& name, Numeric::uint64 value
592  );
593 
604  void GEOGRAM_API set_arg(const std::string& name, double value);
605 
616  void GEOGRAM_API set_arg(const std::string& name, bool value);
617 
630  void GEOGRAM_API set_arg_percent(const std::string& name, double value);
631 
632  /********************************************************************/
633 
640  void GEOGRAM_API get_args(std::vector<std::string>& args);
641 
646  index_t GEOGRAM_API ui_terminal_width();
647 
675  void GEOGRAM_API ui_separator(
676  const std::string& title,
677  const std::string& short_title = ""
678  );
679 
693  void GEOGRAM_API ui_separator();
694 
707  void GEOGRAM_API ui_close_separator();
708 
727  void GEOGRAM_API ui_message(
728  const std::string& message,
729  index_t wrap_margin
730  );
731 
740  void GEOGRAM_API ui_message(
741  const std::string& message
742  );
743 
749  void GEOGRAM_API ui_clear_line();
750 
772  void GEOGRAM_API ui_progress(
773  const std::string& task_name, index_t val,
774  index_t percent, bool clear = true
775  );
776 
788  void GEOGRAM_API ui_progress_time(
789  const std::string& task_name,
790  double elapsed, bool clear = true
791  );
792 
805  void GEOGRAM_API ui_progress_canceled(
806  const std::string& task_name,
807  double elapsed, index_t percent, bool clear = true
808  );
809 
837  std::string GEOGRAM_API ui_feature(
838  const std::string& feature, bool show = true
839  );
840  }
841 }
842 
843 
844 #ifdef GEO_OS_ANDROID
845 struct android_app;
846 
847 namespace GEO {
848  namespace CmdLine {
853  void GEOGRAM_API set_android_app(android_app* app);
854 
859  android_app* GEOGRAM_API get_android_app();
860  }
861 }
862 
863 #endif
864 
865 
866 #endif
867 
Assertion checking mechanism.
Common include file, providing basic definitions. Should be included before anything else by all head...
void ui_separator(const std::string &title, const std::string &short_title="")
Outputs a separator with a title on the console.
std::string ui_feature(const std::string &feature, bool show=true)
Formats a Logger feature name.
void load_config(const std::string &filename, const std::string &program_name)
Loads command line argument values from a file.
void ui_progress_canceled(const std::string &task_name, double elapsed, index_t percent, bool clear=true)
Displays the time elapsed for a canceled task.
bool config_file_loaded()
Tests whether the configuration file was loaded.
void show_usage(const std::string &additional_args="", bool advanced=false)
Displays program help.
void declare_arg(const std::string &name, ArgType type, const std::string &default_value, const std::string &description, ArgFlags flags=ARG_FLAGS_DEFAULT)
Declares an argument.
bool get_arg_bool(const std::string &name)
Gets an argument value as a boolean.
void declare_arg_percent(const std::string &name, double default_value, const std::string &description="...", ArgFlags flags=ARG_FLAGS_DEFAULT)
Declares an argument of type percentage.
Definition: command_line.h:326
int argc()
Gets the number of arguments of the command line.
std::string get_config_file_name()
Gets the name of the configuration file.
charptrptr argv()
Gets the command line arguments.
bool parse(int argc, char **argv, std::vector< std::string > &unparsed_args, const std::string &additional_arg_specs="")
Parses the command line arguments.
bool set_arg(const std::string &name, const std::string &value)
Sets an argument value from a string.
void ui_close_separator()
Closes an opened separator.
ArgFlags
Command line group or argument flags.
Definition: command_line.h:146
double get_arg_percent(const std::string &name, double reference)
Gets an argument value as a percentage.
void initialize()
Initializes the command line framework.
void ui_clear_line()
Clears the last line.
index_t ui_terminal_width()
Gets the width of the console.
void ui_progress_time(const std::string &task_name, double elapsed, bool clear=true)
Displays the time elapsed for a completed task.
std::string get_arg(const std::string &name)
Gets an argument value.
bool arg_is_declared(const std::string &name)
Checks if an argument exists.
int get_arg_int(const std::string &name)
Gets an argument value as an integer.
double get_arg_double(const std::string &name)
Gets an argument value as a floating point.
void get_args(std::vector< std::string > &args)
Gets the value of all arguments.
ArgType get_arg_type(const std::string &name)
Gets the type of an argument.
ArgType
Command line argument types.
Definition: command_line.h:128
void declare_arg_group(const std::string &name, const std::string &description, ArgFlags flags=ARG_FLAGS_DEFAULT)
Declares an argument group.
void ui_progress(const std::string &task_name, index_t val, index_t percent, bool clear=true)
Displays a progress bar.
unsigned int get_arg_uint(const std::string &name)
Gets an argument value as an unsigned integer.
void ui_message(const std::string &message, index_t wrap_margin)
Outputs a message on the console.
void set_config_file_name(const std::string &filename, bool auto_create_args=false)
Defines the name of the configuration file.
void terminate()
Cleans up the command line framework.
void set_arg_percent(const std::string &name, double value)
Sets an argument value from a percentage.
void clear(void *addr, size_t size)
Clears a memory block.
Definition: memory.h:116
uint64_t uint64
Definition: numeric.h:144
int32_t int32
Definition: numeric.h:129
uint32_t uint32
Definition: numeric.h:141
int64_t int64
Definition: numeric.h:132
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Types and functions for numbers manipulation.
Functions for string manipulation.