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
24 struct CommandLineOption
{
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;
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
;
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 static void init(int argc
, char *argv
[]);
51 static void addOptionDefinitions(const CommandLineOption
*definitions
);
52 static void setArgumentDefinition(const char *definition
);
53 static CommandLineParser
*instance();
57 void usage(const QString
&name
, const QString
&argumentDescription
= QString());
59 /// return the options that the user passed
60 QStringList
options() const;
62 * returns true if the option was found.
63 * Consider the following definition "--expert level" The user can type as an argument
64 * "--expert 10". Calling contains("expert") will return true.
65 * @see optionArgument()
67 bool contains(const QString
& key
) const;
69 /// returns the list of items that are not options, note that the first one is the name of the command called
70 QStringList
arguments() const;
72 /// return the list of options that the user passed but we don't have a definition for.
73 QStringList
undefinedOptions() const;
76 * Return the argument passed to an option.
77 * Consider the following definition "--expert level" The user can type as an argument
78 * "--expert 10". Calling optionArgument("expert") will return a string "10"
81 QString
optionArgument(const QString
&optionName
, const QString
&defaultValue
= QString()) const;
82 QStringList
optionArguments(const QString
&optionName
) const;
84 QStringList
parseErrors() const;
87 CommandLineParser(int argc
, char **argv
);
90 static CommandLineParser
*self
;