]> andersk Git - svn-all-fast-export.git/blob - src/options.cpp
fix the option parsing
[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     QSet<QString> validOptionsWithComplement;
48     validOptionsWithComplement << "resume-from" << "identity-map";
49
50     QStringList arguments = argumentList;
51     arguments.takeFirst();           // the first one is the executable name; drop it
52     while (!arguments.isEmpty()) {
53         QString arg = arguments.takeFirst();
54         QString complement;
55
56         if (arg == "--")
57             break;
58
59         if (arg.startsWith("--"))
60             arg = arg.mid(1);   // drop double dashes to single
61
62         if (arg.startsWith("-no-")) {
63             complement = "no";
64             arg = arg.mid(4);
65         } else if (!arg.startsWith("-")) {
66             // non-option arg
67             arguments.prepend(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 (validOptionsWithComplement.contains(arg)) {
80             if (arguments.isEmpty()) {
81                 fprintf(stderr, "Option -%s requires an argument\n", qPrintable(arg));
82                 exit(2);
83             }
84
85             if (options.contains(arg)) {
86                 fprintf(stderr, "Option -%s given more than once\n", qPrintable(arg));
87                 exit(2);
88             }
89
90             if (!complement.isEmpty())
91                 options[arg] = complement;
92             else if (!arguments.isEmpty())
93                 options[arg] = arguments.takeFirst();
94             else {
95                 fprintf(stderr, "Option -%s requires an argument\n", qPrintable(arg));
96                 exit(2);
97             }
98             continue;
99         } else if (validOptions.contains(arg)) {
100             if (switches.contains(arg)) {
101                 fprintf(stderr, "Option -%s given more than once\n", qPrintable(arg));
102                 exit(2);
103             }
104
105             switches[arg] = !(complement == "no");
106         } else {
107             if (complement == "no")
108                 fprintf(stderr, "Invalid option: -no-%s\n", qPrintable(arg));
109             else
110                 fprintf(stderr, "Invalid option: -%s\n", qPrintable(arg));
111             exit(2);
112         }
113     }
114
115     if (switches.value("help")) {
116         showHelp();
117         exit(0);
118     } else if (arguments.count() < 2) {
119         showHelp();
120         exit(2);
121     }
122
123     ruleFile = arguments.takeFirst();
124     pathToRepository = arguments.takeFirst();
125 }
This page took 0.129154 seconds and 5 git commands to generate.