Make test more complete
[vng.git] / CommandLineParser.h
blob8c4c95f1132c785e7cafa5a113e4fa1754f80ff6
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef COMMANDLINEPARSER_H
20 #define COMMANDLINEPARSER_H
22 #include <QString>
24 struct CommandLineOption {
25 /**
26 * The specification of an option includes the name of the option the user must pass and optional arguments it has.
27 * Example specifications are;
28 * <ol>
29 * <li>"-a, --all" </li>
30 * <li>"--version" </li>
31 * <li>"--type name" </li>
32 * <li>"-f, --format name [suffix] [foo]" </li> </ol>
33 * Number 1 allows the user to either type -a or --all (or /A on Windows) to activate this option.
34 * Number 2 allows the user to type --version to activate this option.
35 * Number 3 requires the user to type a single argument after the option.
36 * Number 4 Allows the user to either use -f or --format, which is followed by one required argument
37 * and optionally 2 more arguments.
39 const char *specification;
40 /**
41 * A textual description of the option that will be printed when the user asks for help.
43 const char *description;
46 #define CommandLineLastOption { 0, 0 }
48 class CommandLineParser
50 public:
51 static void init(int argc, char *argv[]);
52 static void addOptionDefinitions(const CommandLineOption *definitions);
53 static void setArgumentDefinition(const char *definition);
54 static CommandLineParser *instance();
56 ~CommandLineParser();
58 void usage(const QString &name, const QString &argumentDescription = QString());
60 /// return the options that the user passed
61 QStringList options() const;
62 /**
63 * returns true if the option was found.
64 * Consider the following definition "--expert level" The user can type as an argument
65 * "--expert 10". Calling contains("expert") will return true.
66 * @see optionArgument()
68 bool contains(const QString & key) const;
70 /// returns the list of items that are not options, note that the first one is the name of the command called
71 QStringList arguments() const;
73 /// return the list of options that the user passed but we don't have a definition for.
74 QStringList undefinedOptions() const;
76 /**
77 * Return the argument passed to an option.
78 * Consider the following definition "--expert level" The user can type as an argument
79 * "--expert 10". Calling optionArgument("expert") will return a string "10"
80 * @see contains()
82 QString optionArgument(const QString &optionName, const QString &defaultValue = QString()) const;
83 QStringList optionArguments(const QString &optionName) const;
85 QStringList parseErrors() const;
87 private:
88 CommandLineParser(int argc, char **argv);
89 class Private;
90 Private * const d;
91 static CommandLineParser *self;
94 #endif