File: qgetopt.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (134 lines) | stat: -rw-r--r-- 5,831 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************
* MeshLab                                                           o o     *
* An extendible mesh processor                                    o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005, 2009                                          \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef GETOPT_H
#define GETOPT_H

#include <QString>
#include <QStringList>
#include <QMap>
#include <QVariant>

/* Example usage:

  QString file1, file2;
  QString o_gamma = "10";
  QString o_scale = "0.7";

  GetOpt opt(argc, argv);
  opt.addArgument("img1", "first image", &file1);
  opt.addArgument("img2", "second image", &file2);
  opt.addOption('g', "gamma", "weigth to derivatives of images (default: 10)", &o_gamma);
  opt.addOption('s', "scale", "scale [0.5-0.99] for multiscale approach (default: 0.7)", &o_scale);

  opt.parse();          */

class GetOpt {
 protected:
  struct Option {
    enum Type { SWITCH, OPTION, ARGUMENT, OPTIONAL };
    Type type;
    char o;
    QString name;
    QString description;
    QVariant *value;
    QString *string_value;
	float *float_value;
    double *double_value;
    int *int_value;
    bool *boolean_value;

	Option(): value(NULL), string_value(NULL), float_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
    Option(Type _type, char _o, QString _name, QString _descr):
        type(_type), o(_o), name(_name), description(_descr),
		value(NULL), string_value(NULL), float_value(NULL), double_value(NULL), int_value(NULL), boolean_value(NULL) {}
  };

  bool unlimitedArgs;
  QList<Option> options;

 public:
  QString appname;          //application name
  QString help;             //help text
  QStringList args;         //original argument vector
  QStringList arguments;    //arbitrary long list of arguments if unlimitedArgs is true

  GetOpt(): unlimitedArgs(false) {}
  GetOpt(int argc, char *argv[] );
  GetOpt(const QStringList &a);

  //add an option without a value
  void addSwitch(char s, const QString &longname, const QString &description, bool *b );

  //add a valued option (v will be left untouched if the option is not given)
  void addOption(char s, const QString &longname, const QString &description, QVariant *v);
  void addOption(char s, const QString &longname, const QString &description, QString *v);
  void addOption(char s, const QString &longname, const QString &description, float *v);
  void addOption(char s, const QString &longname, const QString &description, double *v);
  void addOption(char s, const QString &longname, const QString &description, int *v);
  void addOption(char s, const QString &longname, const QString &description, bool *v);


  //add an argument
  void addArgument(const QString &name, const QString &description, QVariant *v);
  void addArgument(const QString &name, const QString &description, QString *v);
  void addArgument(const QString &name, const QString &description, float *v);
  void addArgument(const QString &name, const QString &description, double *v);
  void addArgument(const QString &name, const QString &description, int *v);
  void addArgument(const QString &name, const QString &description, bool *v);
  void addArgument(const QString &name, const QString &description, Option option);

  //add an optional agrument
  void addOptionalArgument(const QString &name, const QString &description, QVariant *v);

  //allow an unlimited number of optional arguments
  void allowUnlimitedArguments(bool allow) { unlimitedArgs = allow; }

  //set help if someone uses -h or --help option
  void setHelp(const QString &_help) { help = _help; }

  //parses the command line and fill variables or print an error message and exits
  void parse();

  //return usage string
  QString usage();

  //return argv[0]
  QString &applicationName();

protected:
  //parses and return true on success
  bool parse(QString &error);
  //return options or switch
  bool findOption(char c, Option &option);
  //return any named argument
  bool findArg(const QString &name, Option &option);
  //split desc into n pieces of the right length TODO: check for newlines also
  QString formatDesc(QString desc, int len);
  //manage conversion from string to option value
  bool assignOption(Option &option, QString arg, QString &error);

};

#endif