UntrackedFilesHandler.java

  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. import org.eclipse.jgit.pgm.internal.CLIText;
  12. import org.kohsuke.args4j.CmdLineException;
  13. import org.kohsuke.args4j.CmdLineParser;
  14. import org.kohsuke.args4j.OptionDef;
  15. import org.kohsuke.args4j.spi.Parameters;
  16. import org.kohsuke.args4j.spi.Setter;
  17. import org.kohsuke.args4j.spi.StringOptionHandler;

  18. /**
  19.  * Special handler for the <code>--untracked-files</code> option of the
  20.  * <code>status</code> command.
  21.  *
  22.  * The following rules apply:
  23.  * <ul>
  24.  * <li>If no mode is given, i.e. just <code>--untracked-files</code> is passed,
  25.  * then it is the same as <code>--untracked-files=all</code></li>
  26.  * <li>If the <code>-u</code> alias is passed then it is the same as
  27.  * <code>--untracked-files</code></li>
  28.  * <li>If the <code>-uno</code> alias is passed then it is the same as
  29.  * <code>--untracked-files=no</code></li>
  30.  * <li>If the <code>-uall</code> alias is passed then it is the same as
  31.  * <code>--untracked-files=all</code></li>
  32.  * </ul>
  33.  *
  34.  * @since 4.0
  35.  */
  36. public class UntrackedFilesHandler extends StringOptionHandler {

  37.     /**
  38.      * <p>Constructor for UntrackedFilesHandler.</p>
  39.      *
  40.      * @param parser
  41.      *            The parser to which this handler belongs to.
  42.      * @param option
  43.      *            The annotation.
  44.      * @param setter
  45.      *            Object to be used for setting value.
  46.      */
  47.     public UntrackedFilesHandler(CmdLineParser parser, OptionDef option,
  48.             Setter<? super String> setter) {
  49.         super(parser, option, setter);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public int parseArguments(Parameters params) throws CmdLineException {
  54.         String alias = params.getParameter(-1);
  55.         if ("-u".equals(alias)) { //$NON-NLS-1$
  56.             setter.addValue("all"); //$NON-NLS-1$
  57.             return 0;
  58.         } else if ("-uno".equals(alias)) { //$NON-NLS-1$
  59.             setter.addValue("no"); //$NON-NLS-1$
  60.             return 0;
  61.         } else if ("-uall".equals(alias)) { //$NON-NLS-1$
  62.             setter.addValue("all"); //$NON-NLS-1$
  63.             return 0;
  64.         } else if (params.size() == 0) {
  65.             setter.addValue("all"); //$NON-NLS-1$
  66.             return 0;
  67.         } else if (params.size() == 1) {
  68.             String mode = params.getParameter(0);
  69.             if ("no".equals(mode) || "all".equals(mode)) { //$NON-NLS-1$ //$NON-NLS-2$
  70.                 setter.addValue(mode);
  71.             } else {
  72.                 throw new CmdLineException(owner,
  73.                         CLIText.format(CLIText.get().invalidUntrackedFilesMode),
  74.                         mode);
  75.             }
  76.             return 1;
  77.         } else {
  78.             return super.parseArguments(params);
  79.         }
  80.     }

  81. }