• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.15
/exist-core/src/main/java/org/exist/client/CommandlineOptions.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 *
24
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.client;
50

51
import java.io.File;
52
import java.net.URISyntaxException;
53
import java.nio.file.Path;
54
import java.util.*;
55

56
import org.exist.util.OSUtil;
57
import org.exist.xmldb.XmldbURI;
58
import org.exist.xquery.util.URIUtils;
59
import se.softhouse.jargo.*;
60

61
import static org.exist.util.ArgumentUtil.*;
62
import static se.softhouse.jargo.Arguments.*;
63

64
/**
65
 * Command Line Options for the {@link InteractiveClient}
66
 *
67
 * @author <a href="mailto:adam.retter@googlemail.com">Adam Retter</a>
68
 * @author wessels
69
 */
70
public class CommandlineOptions {
71

72
    /* general arguments */
73
    private static final Argument<?> helpArg = helpArgument("-h", "--help");
1✔
74
    private static final Argument<Boolean> quietArg = optionArgument("-q", "--quiet")
1✔
75
            .description("be quiet. Just print errors.")
1✔
76
            .defaultValue(false)
1✔
77
            .build();
1✔
78
    private static final Argument<Boolean> verboseArg = optionArgument("-v", "--verbose")
1✔
79
            .description("be verbose. Display progress information on put.")
1✔
80
            .defaultValue(false)
1✔
81
            .build();
1✔
82
    private static final Argument<File> outputFileArg = fileArgument("-O", "--output")
1✔
83
            .description("write output of command into given file (use with -x, -g).")
1✔
84
            .build();
1✔
85
    private static final Argument<Map<String, String>> optionArg = stringArgument("-o", "--option")
1✔
86
            .description("specify extra options: property=value. For available properties see client.properties.")
1✔
87
            .asKeyValuesWithKeyParser(StringParsers.stringParser())
1✔
88
            .build();
1✔
89

90

91
    /* database connection arguments */
92
    private static final Argument<String> userArg = stringArgument("-u", "--user")
1✔
93
            .description("set username.")
1✔
94
            .defaultValue(null)
1✔
95
            .build();
1✔
96
    private static final Argument<String> passwordArg = stringArgument("-P", "--password")
1✔
97
            .description("specify password.")
1✔
98
            .build();
1✔
99
    private static final Argument<Boolean> useSslArg = optionArgument("-S", "--use-ssl")
1✔
100
            .description("Use SSL by default for remote connections")
1✔
101
            .defaultValue(false)
1✔
102
            .build();
1✔
103
    private static final Argument<Boolean> embeddedArg = optionArgument("-l", "--local")
1✔
104
            .description("launch a local database instance. Otherwise client will connect to URI specified in client.properties.")
1✔
105
            .defaultValue(false)
1✔
106
            .build();
1✔
107
    private static final Argument<File> embeddedConfigArg = fileArgument("-C", "--config")
1✔
108
            .description("specify alternate configuration file. Implies -l.")
1✔
109
            .build();
1✔
110
    private static final Argument<Boolean> noEmbeddedModeArg = optionArgument("-N", "--no-embedded-mode")
1✔
111
            .description("do not make embedded mode available")
1✔
112
            .defaultValue(false)
1✔
113
            .build();
1✔
114

115

116
    /* gui arguments */
117
    private static final Argument<Boolean> noGuiArg = optionArgument("-s", "--no-gui")
1✔
118
            .description("don't start client with GUI. Just use the shell.")
1✔
119
            .defaultValue(false)
1✔
120
            .build();
1✔
121
    private static final Argument<Boolean> guiQueryDialogArg = optionArgument("-Q", "--query")
1✔
122
            .description("directly open the query gui")
1✔
123
            .defaultValue(false)
1✔
124
            .build();
1✔
125

126

127
    /* mk/rm/set collection arguments */
128
    private static final Argument<String> mkColArg = stringArgument("-m", "--mkcol")
1✔
129
            .description("create a collection (and any missing parent collection). Implies -c.")
1✔
130
            .build();
1✔
131
    private static final Argument<String> rmColArg = stringArgument("-R", "--rmcol")
1✔
132
            .description("remove entire collection")
1✔
133
            .build();
1✔
134
    private static final Argument<String> setColArg = stringArgument("-c", "--collection")
1✔
135
            .description("set target collection.")
1✔
136
            .build();
1✔
137

138

139
    /* put/get/rm document arguments */
140
    private static final Argument<List<File>> parseDocsArg = fileArgument("-p", "--parse")
1✔
141
            .description("store files or directories given as extra args on command line.")
1✔
142
            .variableArity()
1✔
143
            .build();
1✔
144
    private static final Argument<String> getDocArg = stringArgument("-g", "--get")
1✔
145
            .description("retrieve a document.")
1✔
146
            .build();
1✔
147
    private static final Argument<String> rmDocArg = stringArgument("-r", "--remove")
1✔
148
            .description("remove a document.")
1✔
149
            .build();
1✔
150

151

152
    /* query arguments */
153
    public static final String XPATH_STDIN = "<<STDIN";
154
    private static final Argument<String> xpathArg = stringArgument("-x", "--xpath")
1✔
155
            .description("execute XPath query given as argument. Without argument reads query from stdin.")
1✔
156
            .build();
1✔
157
    private static final Argument<List<File>> loadQueriesArg = fileArgument("-F", "--file")
1✔
158
            .description("load queries from file and execute in random order.")
1✔
159
            .variableArity()
1✔
160
            .build();
1✔
161
    private static final Argument<Integer> howManyResultsArg = integerArgument("-n", "--howmany")
1✔
162
            .description("max. number of query results to be displayed.")
1✔
163
            .build();
1✔
164
    private static final Argument<File> traceQueriesArg = fileArgument("-T", "--trace")
1✔
165
            .description("log queries to the file specified by the argument (for debugging).")
1✔
166
            .build();
1✔
167

168

169
    /* xupdate arguments */
170
    private static final Argument<String> setDocArg = stringArgument("-f", "--resource")
1✔
171
            .description("specify a resource contained in the current collection. Use in conjunction with --xupdate to specify the resource to update.")
1✔
172
            .build();
1✔
173

174
    private static final Argument<File> xupdateArg = fileArgument("-X", "--xupdate")
1✔
175
            .description("process XUpdate commands. Commands are read from the file specified in the argument.")
1✔
176
            .build();
1✔
177

178
    /* reindex arguments */
179
    private static final Argument<Boolean> reindexArg = optionArgument("-i", "--reindex")
1✔
180
            .description("reindex the collection specified in the collection argument --collection")
1✔
181
            .defaultValue(false)
1✔
182
            .build();
1✔
183
    private static final Argument<Boolean> reindexRecurseDirsArg = optionArgument("-d", "--recurse-dirs")
1✔
184
            .description("recurse into subdirectories during index?")
1✔
185
            .defaultValue(false)
1✔
186
            .build();
1✔
187

188
    private static Optional<XmldbURI> optUri(final ParsedArguments parsedArguments, final Argument<String> argument) throws URISyntaxException {
189
        final Optional<String> uriStr = getOpt(parsedArguments, argument);
1✔
190
        if (uriStr.isPresent()) {
1!
191
            return Optional.of(URIUtils.encodeXmldbUriFor(uriStr.get()));
×
192
        } else {
193
            return Optional.empty();
1✔
194
        }
195
    }
196

197
    public static CommandlineOptions parse(final String[] args) throws ArgumentException, URISyntaxException {
198
        final ParsedArguments arguments = CommandLineParser
1✔
199
                .withArguments(userArg, passwordArg, useSslArg, embeddedArg, embeddedConfigArg, noEmbeddedModeArg)
1✔
200
                .andArguments(noGuiArg, guiQueryDialogArg)
1✔
201
                .andArguments(mkColArg, rmColArg, setColArg)
1✔
202
                .andArguments(parseDocsArg, getDocArg, rmDocArg)
1✔
203
                .andArguments(xpathArg, loadQueriesArg, howManyResultsArg, traceQueriesArg)
1✔
204
                .andArguments(setDocArg, xupdateArg)
1✔
205
                .andArguments(reindexArg, reindexRecurseDirsArg)
1✔
206
                .andArguments(helpArg, quietArg, verboseArg, outputFileArg, optionArg)
1✔
207
                .programName("client" + (OSUtil.IS_WINDOWS ? ".bat" : ".sh"))
1!
208
                .parse(args);
1✔
209

210
        final boolean quiet = getBool(arguments, quietArg);
1✔
211
        final boolean verbose = getBool(arguments, verboseArg);
1✔
212
        final Optional<Path> outputFile = getPathOpt(arguments, outputFileArg);
1✔
213
        final Map<String, String> options = arguments.get(optionArg);
1✔
214

215
        final Optional<String> username = getOpt(arguments, userArg);
1✔
216
        final Optional<String> password = getOpt(arguments, passwordArg);
1✔
217
        final boolean useSSL = getBool(arguments, useSslArg);
1✔
218
        final boolean embedded = getBool(arguments, embeddedArg);
1✔
219
        final Optional<Path> embeddedConfig = getPathOpt(arguments, embeddedConfigArg);
1✔
220
        final boolean noEmbeddedMode = getBool(arguments, noEmbeddedModeArg);
1✔
221

222
        final boolean startGUI = !getBool(arguments, noGuiArg);
1!
223
        final boolean openQueryGUI = getBool(arguments, guiQueryDialogArg);
1✔
224

225
        final Optional<XmldbURI> mkCol = optUri(arguments, mkColArg);
1✔
226
        final Optional<XmldbURI> rmCol = optUri(arguments, rmColArg);
1✔
227
        final Optional<XmldbURI> setCol = optUri(arguments, setColArg);
1✔
228

229
        final List<Path> parseDocs = getPathsOpt(arguments, parseDocsArg);
1✔
230
        final Optional<XmldbURI> getDoc = optUri(arguments, getDocArg);
1✔
231
        final Optional<String> rmDoc = getOpt(arguments, rmDocArg);
1✔
232

233
        final Optional<String> maybeXpath = getOpt(arguments, xpathArg);
1✔
234
        final Optional<String> xpath;
235
        if(maybeXpath.isPresent()) {
1!
236
            if(maybeXpath.get().isEmpty()) {
×
237
                xpath = Optional.of(XPATH_STDIN);
×
238
            } else {
×
239
                xpath = maybeXpath;
×
240
            }
241
        } else {
×
242
            xpath = Optional.empty();
1✔
243
        }
244
        final List<Path> queryFiles = getPathsOpt(arguments, loadQueriesArg);
1✔
245
        final Optional<Integer> howManyResults = getOpt(arguments, howManyResultsArg);
1✔
246
        final Optional<Path> traceQueriesFile = getPathOpt(arguments, traceQueriesArg);
1✔
247

248
        final Optional<String> setDoc = getOpt(arguments, setDocArg);
1✔
249
        final Optional<Path> xupdateFile = getPathOpt(arguments, xupdateArg);
1✔
250

251
        final boolean reindex = getBool(arguments, reindexArg);
1✔
252
        final boolean reindexRecurse = getBool(arguments, reindexRecurseDirsArg);
1✔
253

254
        return new CommandlineOptions(
1✔
255
                quiet,
1✔
256
                verbose,
1✔
257
                outputFile,
1✔
258
                options,
1✔
259
                username,
1✔
260
                password,
1✔
261
                useSSL,
1✔
262
                embedded,
1✔
263
                embeddedConfig,
1✔
264
                noEmbeddedMode,
1✔
265
                startGUI,
1✔
266
                openQueryGUI,
1✔
267
                mkCol,
1✔
268
                rmCol,
1✔
269
                setCol,
1✔
270
                parseDocs,
1✔
271
                getDoc,
1✔
272
                rmDoc,
1✔
273
                xpath,
1✔
274
                queryFiles,
1✔
275
                howManyResults,
1✔
276
                traceQueriesFile,
1✔
277
                setDoc,
1✔
278
                xupdateFile,
1✔
279
                reindex,
1✔
280
                reindexRecurse
1✔
281
        );
282
    }
283

284
    public CommandlineOptions(boolean quiet, boolean verbose, Optional<Path> outputFile, Map<String, String> options, Optional<String> username, Optional<String> password, boolean useSSL, boolean embedded, Optional<Path> embeddedConfig, boolean noEmbeddedMode, boolean startGUI, boolean openQueryGUI, Optional<XmldbURI> mkCol, Optional<XmldbURI> rmCol, Optional<XmldbURI> setCol, List<Path> parseDocs, Optional<XmldbURI> getDoc, Optional<String> rmDoc, Optional<String> xpath, List<Path> queryFiles, Optional<Integer> howManyResults, Optional<Path> traceQueriesFile, Optional<String> setDoc, Optional<Path> xupdateFile, boolean reindex, boolean reindexRecurse) {
1✔
285
        this.quiet = quiet;
1✔
286
        this.verbose = verbose;
1✔
287
        this.outputFile = outputFile;
1✔
288
        this.options = options;
1✔
289
        this.username = username;
1✔
290
        this.password = password;
1✔
291
        this.useSSL = useSSL;
1✔
292
        this.embedded = embedded;
1✔
293
        this.embeddedConfig = embeddedConfig;
1✔
294
        this.noEmbeddedMode = noEmbeddedMode;
1✔
295
        this.startGUI = startGUI;
1✔
296
        this.openQueryGUI = openQueryGUI;
1✔
297
        this.mkCol = mkCol;
1✔
298
        this.rmCol = rmCol;
1✔
299
        this.setCol = setCol;
1✔
300
        this.parseDocs = parseDocs;
1✔
301
        this.getDoc = getDoc;
1✔
302
        this.rmDoc = rmDoc;
1✔
303
        this.xpath = xpath;
1✔
304
        this.queryFiles = queryFiles;
1✔
305
        this.howManyResults = howManyResults;
1✔
306
        this.traceQueriesFile = traceQueriesFile;
1✔
307
        this.setDoc = setDoc;
1✔
308
        this.xupdateFile = xupdateFile;
1✔
309
        this.reindex = reindex;
1✔
310
        this.reindexRecurse = reindexRecurse;
1✔
311
    }
1✔
312

313
    final boolean quiet;
314
    final boolean verbose;
315
    final Optional<Path> outputFile;
316
    final Map<String, String> options;
317

318
    final Optional<String> username;
319
    final Optional<String> password;
320
    final boolean useSSL;
321
    final boolean embedded;
322
    final Optional<Path> embeddedConfig;
323
    final boolean noEmbeddedMode;
324

325
    final boolean startGUI;
326
    final boolean openQueryGUI;
327

328
    final Optional<XmldbURI> mkCol;
329
    final Optional<XmldbURI> rmCol;
330
    final Optional<XmldbURI> setCol;
331

332
    final List<Path> parseDocs;
333
    final Optional<XmldbURI> getDoc;
334
    final Optional<String> rmDoc;
335

336
    final Optional<String> xpath;
337
    final List<Path> queryFiles;
338
    final Optional<Integer> howManyResults;
339
    final Optional<Path> traceQueriesFile;
340

341
    final Optional<String> setDoc;
342
    final Optional<Path> xupdateFile;
343

344
    final boolean reindex;
345
    final boolean reindexRecurse;
346
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc