📄 args.h
字号:
//
// args.h
//
// $Id: args.h,v 1.1.1.1 2001/02/28 00:28:35 cstolte Exp $
//
// This file provides yet another set of command-line argument procesing
// routines for C++ programs. By taking advantage of some C++ language
// features, however, we're able to make them much more intuitive and
// useful than most argument processing code.
// Sample usage:
//
// int main(int argc, char **argv) {
// char *in_filename = 0;
// char *out_filename = 0;
// double scale = 1.;
// double pos[3] = { 0, 0, 0 };
// bool verbose = false;
// ArgParser ap;
//
// ap('o', "out-filename", &out_filename, true);
// ap('s', "scale", &scale);
// ap('p', "position", pos, false, 3);
// ap('v', "verbose", &verbose);
// ap(&in_filename, "input-filename", true);
// ap.process("progname", argc, argv);
// [...]
// }
// This program takes a required output filename, prefixed by either
// "-o" or "--out-filename", an optional scale, prefixed by "-s" or
// "--scale", an optional position defined by three doubles, a switch,
// "-v" or "--verbose" which toggles the verbose variable each time it's
// found in the argument list, and a required input filename, which
// isn't prefixed by anything. (The 'true' flag is used for required
// arguments above, and the number of following arguments to snarf is
// specified by the "3" for the position argument.)
// So for this example, valid command lines would include:
//
// (%) progname in.file -p 1. 0.2 .5 -o out.file -v
// (%) progname -s 5 -p 1. 1. 1. -v -v in.file -v -o out.file
//
// but not:
//
// (%) progname -o out.file
//
// If required arguments are missing when process() is called, or
// if the print_help() method is called, then a usage string will
// be printed.
//
#ifndef SGL_ARGS_H
#define SGL_ARGS_H
#include <sgl/defs.h>
enum ArgRequired { Optional, Required };
class ArgParser {
public:
ArgParser();
~ArgParser();
void operator()(char **fl, const char *float_name, ArgRequired a);
void operator()(char c, const char *str, int *i, ArgRequired a,
u_int num = 1);
void operator()(char c, const char *str, double *d, ArgRequired a,
u_int num = 1);
void operator()(char c, const char *str, u_int *ui, ArgRequired a,
u_int num = 1);
void operator()(char c, const char *str, char **s, ArgRequired a,
u_int num = 1);
void operator()(char c, const char *str, bool *b);
void process(const char *prog_name, int argc, char **argv);
void print_help(const char *prog_name) const;
private:
struct APItem {
union {
int *i;
double *d;
u_int *ui;
char **s;
bool *b;
};
enum Type { I, D, UI, S, B };
Type type;
char c_switch;
char *str_switch;
u_int num;
bool required, initialized;
APItem *next;
};
APItem *item_list;
char **floating_arg;
char *floating_name;
bool floating_required;
bool floating_set;
void print_args_for_item(std::ostream &os, APItem *item) const;
void print_values_of_all_switches() const;
void add_item_to_list(APItem *item);
};
#endif /* SGL_ARGS_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -