Teach all ssh commands to stop option parsing on --

Using -- as a way to break the option parser and start doing
argument parsing is very common.  Teach all of our commands
to work that way.

Change-Id: Iaeb1be2ee443fce7f09a9eff6dd7270e1ba91509
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2010-01-02 18:45:16 -08:00
parent 4039675ad5
commit 9ead06f525
2 changed files with 46 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.sshd.SshScope.Context;
import com.google.gerrit.util.cli.CmdLineParser;
import com.google.gerrit.util.cli.EndOfOptionsHandler;
import com.google.inject.Inject;
import com.google.inject.Provider;
@ -60,6 +61,9 @@ public abstract class BaseCommand implements Command {
@Option(name = "--help", usage = "display this help text", aliases = {"-h"})
private boolean help;
@Option(name = "--", usage = "end of options", handler = EndOfOptionsHandler.class)
private boolean endOfOptions;
protected InputStream in;
protected OutputStream out;
protected OutputStream err;

View File

@ -0,0 +1,42 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.util.cli;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
/** Typically used with {@code @Option(name="--")} to signal end of options. */
public class EndOfOptionsHandler extends OptionHandler<Boolean> {
public EndOfOptionsHandler(CmdLineParser parser, OptionDef option,
Setter<? super Boolean> setter) {
super(parser, option, setter);
}
@Override
public String getDefaultMetaVariable() {
return null;
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
owner.stopOptionParsing();
setter.addValue(true);
return 0;
}
}