]> andersk Git - svn-all-fast-export.git/blame - src/options.cpp
fix the option parsing
[svn-all-fast-export.git] / src / options.cpp
CommitLineData
1228bd7c
TM
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
26Options* Options::globalOptions = 0;
27
28Options::Options()
29{
30 globalOptions = this;
31}
32
33Options::~Options()
34{
35}
36
37void Options::showHelp()
38{
39 printf("Usage: svn-all-fast-export configfile path-to-svn\n");
40}
41
42void Options::parseArguments(const QStringList &argumentList)
43{
44 QSet<QString> validOptions;
45 validOptions << "help";
46
b0fb9f08
TM
47 QSet<QString> validOptionsWithComplement;
48 validOptionsWithComplement << "resume-from" << "identity-map";
1228bd7c
TM
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
b0fb9f08 67 arguments.prepend(arg);
1228bd7c
TM
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
b0fb9f08 79 if (validOptionsWithComplement.contains(arg)) {
1228bd7c 80 if (arguments.isEmpty()) {
b0fb9f08 81 fprintf(stderr, "Option -%s requires an argument\n", qPrintable(arg));
1228bd7c
TM
82 exit(2);
83 }
84
b0fb9f08
TM
85 if (options.contains(arg)) {
86 fprintf(stderr, "Option -%s given more than once\n", qPrintable(arg));
1228bd7c
TM
87 exit(2);
88 }
89
90 if (!complement.isEmpty())
b0fb9f08 91 options[arg] = complement;
1228bd7c 92 else if (!arguments.isEmpty())
b0fb9f08 93 options[arg] = arguments.takeFirst();
1228bd7c 94 else {
b0fb9f08 95 fprintf(stderr, "Option -%s requires an argument\n", qPrintable(arg));
1228bd7c
TM
96 exit(2);
97 }
98 continue;
99 } else if (validOptions.contains(arg)) {
100 if (switches.contains(arg)) {
b0fb9f08 101 fprintf(stderr, "Option -%s given more than once\n", qPrintable(arg));
1228bd7c
TM
102 exit(2);
103 }
104
105 switches[arg] = !(complement == "no");
106 } else {
107 if (complement == "no")
b0fb9f08 108 fprintf(stderr, "Invalid option: -no-%s\n", qPrintable(arg));
1228bd7c 109 else
b0fb9f08 110 fprintf(stderr, "Invalid option: -%s\n", qPrintable(arg));
1228bd7c
TM
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.055721 seconds and 5 git commands to generate.