gctl/lib/utility/getopt_help.h

243 lines
8.3 KiB
C
Raw Permalink Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_GETOPT_HELP_H
#define _GCTL_GETOPT_HELP_H
#include "stream.h"
#ifdef _WINDOWS
#include "windows.h"
#include "getopt_win.h"
#else
#include "sys/ioctl.h"
#include "getopt.h"
#endif
namespace gctl
{
struct option_info
{
/**
* The following three variables we added here are mainly used to generate the
* 'help' page. The variable 'info' represents a short message that explains what
* the option does. 'format' express the argument's format that the option takes.
* Additionally, The variable 'manda' indicates whether the option is mandatory
* or optional.
*/
const char *info;
const char *format;
bool manda;
};
/**
* @brief display formatted help information in terminal
*
* This function will display formated help information in the terminal using
* the standard error output. We choose the error output to avoid unexpected
* redirection and make sure the information will appear in the terminal.
*
* @param[in] proname The program's name.
* @param[in] brief The brief information show right after the program's name.
* @param[in] exp_longopts The pointer of expanded longopts
* @param stdout
*/
void getopt_long_help(const option *longopts, const option_info *expd_longopts, const char* proname,
const char* brief, std::ostream& sout=std::clog, std::string extra_usage = "");
/**
* @brief Display the help information of one option indicated by its id.
*
* @param[in] val The option's value
* @param exp_longopts The expanded longopts
* @param stdout
*/
void getopt_long_option_info(int val, const option *longopts, const option_info *expd_longopts, std::ostream& sout=std::clog);
/**
* @brief
*
* GNU getopt_long()
*
*
* 1.
* 2. has_arg = no_argument
* 3. has_arg = required_argument =
* 4. has_arg = optional_argument =
*/
class flags_parser
{
public:
flags_parser();
virtual ~flags_parser();
/**
* @brief
*
* @param[in] s_name
* @param[in] f_name
* @param[in] has_arg
* @param flag get_arg()NULL则get_arg()s_name的值拷贝给flag指向的变量0
* @param[in] info
* @param[in] format
* @param[in] manda
*/
void add_opt(int s_name, const char* f_name, int has_arg, int *flag, const char* info,
const char* format, bool manda);
/**
* @brief
*
*
*
* @param[in] argc
* @param argv
*/
void configure(int argc, char **argv);
/**
* @brief
*
* @return
*/
bool pass_mandatory();
/**
* @brief
*
* @param[in] s_name
*
* @return 10-1
*/
int set_opt(const int s_name);
/**
* @brief
*
* @param[in] f_name
*
* @return 10-1
*/
int set_opt(const char* f_name);
/**
* @brief
*
* @param[in] s_name
*
* @return NULLNO_ARG
*/
std::string get_arg(const int s_name);
/**
* @brief
*
* @param[in] s_name
*
* @return NULLNO_ARG
*/
std::string get_arg(const char* f_name);
/**
* @brief
*
* @param[in] tar_val
* @param[in] ret_str
*/
void get_argv(std::initializer_list<char> tar_val, std::initializer_list<std::string*> ret_str);
/**
* @brief
*/
void show_help_page(std::ostream& sout = std::clog, std::string extra_usage = "");
/**
* @brief
*
* @param[in] s_name
*/
void show_option_info(int s_name, std::ostream& sout=std::clog);
/**
* @brief
*
* @param[in] s_name
*/
void show_option_info(const char* f_name, std::ostream& sout=std::clog);
/**
* @brief
*
* @param[in] proname
*/
void set_proname(const char* proname);
/**
* @brief
*
* @param[in] proinfo
*/
void set_proinfo(const char* proinfo);
/**
* @brief NULL
*
* @param[in] in_str
*/
void set_nofound_return(std::string in_str);
/**
* @brief NO_ARG
*
* @param[in] in_str
*/
void set_noarg_return(std::string in_str);
/**
* @brief
*
* @param stdout
*/
void show_arg_format(std::ostream& sout=std::clog);
protected:
std::vector<option> rec_opts_;
std::vector<option_info> rec_info_;
std::string arg_format_;
std::string pro_name_;
std::string pro_info_;
std::string nofound_return_;
std::string noarg_return_;
int argc_;
char **argv_;
bool configured_;
bool failed_mandatory_;
};
}
#endif //_GCTL_GETOPT_HELP_H