1 /*
2 * Copyright (C) 2015 Zend Technologies Ltd. and others and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10 package org.eclipse.jgit.pgm.opt;
11
12 import org.eclipse.jgit.pgm.internal.CLIText;
13 import org.kohsuke.args4j.CmdLineException;
14 import org.kohsuke.args4j.CmdLineParser;
15 import org.kohsuke.args4j.OptionDef;
16 import org.kohsuke.args4j.spi.Parameters;
17 import org.kohsuke.args4j.spi.Setter;
18 import org.kohsuke.args4j.spi.StringOptionHandler;
19
20 /**
21 * Special handler for the <code>--untracked-files</code> option of the
22 * <code>status</code> command.
23 *
24 * The following rules apply:
25 * <ul>
26 * <li>If no mode is given, i.e. just <code>--untracked-files</code> is passed,
27 * then it is the same as <code>--untracked-files=all</code></li>
28 * <li>If the <code>-u</code> alias is passed then it is the same as
29 * <code>--untracked-files</code></li>
30 * <li>If the <code>-uno</code> alias is passed then it is the same as
31 * <code>--untracked-files=no</code></li>
32 * <li>If the <code>-uall</code> alias is passed then it is the same as
33 * <code>--untracked-files=all</code></li>
34 * </ul>
35 *
36 * @since 4.0
37 */
38 public class UntrackedFilesHandler extends StringOptionHandler {
39
40 /**
41 * <p>Constructor for UntrackedFilesHandler.</p>
42 *
43 * @param parser
44 * The parser to which this handler belongs to.
45 * @param option
46 * The annotation.
47 * @param setter
48 * Object to be used for setting value.
49 */
50 public UntrackedFilesHandler(CmdLineParser parser, OptionDef option,
51 Setter<? super String> setter) {
52 super(parser, option, setter);
53 }
54
55 /** {@inheritDoc} */
56 @Override
57 public int parseArguments(Parameters params) throws CmdLineException {
58 String alias = params.getParameter(-1);
59 if ("-u".equals(alias)) { //$NON-NLS-1$
60 setter.addValue("all"); //$NON-NLS-1$
61 return 0;
62 } else if ("-uno".equals(alias)) { //$NON-NLS-1$
63 setter.addValue("no"); //$NON-NLS-1$
64 return 0;
65 } else if ("-uall".equals(alias)) { //$NON-NLS-1$
66 setter.addValue("all"); //$NON-NLS-1$
67 return 0;
68 } else if (params.size() == 0) {
69 setter.addValue("all"); //$NON-NLS-1$
70 return 0;
71 } else if (params.size() == 1) {
72 String mode = params.getParameter(0);
73 if ("no".equals(mode) || "all".equals(mode)) { //$NON-NLS-1$ //$NON-NLS-2$
74 setter.addValue(mode);
75 } else {
76 throw new CmdLineException(owner,
77 CLIText.format(CLIText.get().invalidUntrackedFilesMode),
78 mode);
79 }
80 return 1;
81 } else {
82 return super.parseArguments(params);
83 }
84 }
85
86 }