]> andersk Git - svn-all-fast-export.git/blob - src/options.cpp
Add a better option-parser
[svn-all-fast-export.git] / src / options.cpp
1 /*
2  *  Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "options.h"
19
20 #include <QSet>
21 #include <QStringList>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 Options* Options::globalOptions = 0;
27
28 Options::Options()
29 {
30     globalOptions = this;
31 }
32
33 Options::~Options()
34 {
35 }
36
37 void Options::showHelp()
38 {
39     printf("Usage: svn-all-fast-export configfile path-to-svn\n");
40 }
41
42 void Options::parseArguments(const QStringList &argumentList)
43 {
44     QSet<QString> validOptions;
45     validOptions << "help";
46
47     QHash<QString, QString> optionsWithComplement;
48     optionsWithComplement.insert("resume-from", QString());
49     optionsWithComplement.insert("identity-map", QString());
50
51     QStringList arguments = argumentList;
52     arguments.takeFirst();           // the first one is the executable name; drop it
53     while (!arguments.isEmpty()) {
54         QString arg = arguments.takeFirst();
55         QString complement;
56
57         if (arg == "--")
58             break;
59
60         if (arg.startsWith("--"))
61             arg = arg.mid(1);   // drop double dashes to single
62
63         if (arg.startsWith("-no-")) {
64             complement = "no";
65             arg = arg.mid(4);
66         } else if (!arg.startsWith("-")) {
67             // non-option arg
68             break;
69         } else { // starts with "-"
70             arg = arg.mid(1);
71         }
72
73         if (arg.contains('=') && complement.isEmpty()) {
74             int pos = arg.indexOf('=');
75             complement = arg.mid(pos + 1);
76             arg.truncate(pos);
77         }
78
79         if (optionsWithComplement.contains(arg)) {
80             if (arguments.isEmpty()) {
81                 fprintf(stderr, "Option -%s requires an argument", qPrintable(arg));
82                 exit(2);
83             }
84
85             QString &setting = optionsWithComplement[arg];
86             if (!setting.isNull()) {
87                 fprintf(stderr, "Option -%s given more than once", qPrintable(arg));
88                 exit(2);
89             }
90
91             if (!complement.isEmpty())
92                 setting = complement;
93             else if (!arguments.isEmpty())
94                 setting = arguments.takeFirst();
95             else {
96                 fprintf(stderr, "Option -%s requires an argument", qPrintable(arg));
97                 exit(2);
98             }
99             continue;
100         } else if (validOptions.contains(arg)) {
101             if (switches.contains(arg)) {
102                 fprintf(stderr, "Option -%s given more than once", qPrintable(arg));
103                 exit(2);
104             }
105
106             switches[arg] = !(complement == "no");
107         } else {
108             if (complement == "no")
109                 fprintf(stderr, "Invalid option: -no-%s", qPrintable(arg));
110             else
111                 fprintf(stderr, "Invalid option: -%s", qPrintable(arg));
112             exit(2);
113         }
114     }
115
116     if (switches.value("help")) {
117         showHelp();
118         exit(0);
119     } else if (arguments.count() < 2) {
120         showHelp();
121         exit(2);
122     }
123
124     ruleFile = arguments.takeFirst();
125     pathToRepository = arguments.takeFirst();
126 }
This page took 0.038614 seconds and 5 git commands to generate.