Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
[BZ 1073096] - Adding the check for any unrecognized arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkremser committed Mar 18, 2014
1 parent bb6f4c8 commit 705dca8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ public int exec(String[] args) {
Options options = getOptions();
int rValue = RHQControl.EXIT_CODE_OK;
try {
CommandLineParser parser = new PosixParser();
CommandLine cmdLine = parser.parse(options, args);
CommandLineParser parser = new RHQPosixParser(true);
CommandLine cmdLine = parser.parse(options, args, true);
if (!cmdLine.getArgList().isEmpty()) {
// there were some unrecognized args
printUsage();
return RHQControl.EXIT_CODE_INVALID_ARGUMENT;
}
rValue = exec(cmdLine);

if (rhqctlConfig != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* RHQ Management Platform
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.rhq.server.control;

import java.util.ListIterator;

import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
* @author Jirka Kremser
*
* This parser class throws an exception if it finds some unknown option.
* Option in the commons-cli terminology is the command line token starting with '-' or '--')
*
*/
public class RHQPosixParser extends PosixParser {
private boolean ignoreUnrecognizedOption;

public RHQPosixParser() {
this(false);
}

public RHQPosixParser(final boolean ignoreUnrecognizedOption) {
this.ignoreUnrecognizedOption = ignoreUnrecognizedOption;
}

@Override
protected void processOption(final String arg, final ListIterator iter) throws ParseException {
boolean hasOption = getOptions().hasOption(arg);

if (hasOption || ignoreUnrecognizedOption) {
throw new ParseException("Unknown option: " + arg);
}
super.processOption(arg, iter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;

import org.jboss.as.controller.client.ModelControllerClient;
import org.rhq.common.jbossas.client.controller.DeploymentJBossASClient;
import org.rhq.common.jbossas.client.controller.MCCHelper;
Expand All @@ -52,6 +56,7 @@
import org.rhq.server.control.RHQControl;
import org.rhq.server.control.RHQControlException;
import org.rhq.server.control.util.ExecutorAssist;
import org.rhq.server.control.RHQPosixParser;

/**
* Common code for commands that perform installs. Basically shared code for Install and Upgrade commands.
Expand Down Expand Up @@ -822,7 +827,7 @@ protected void addUndoTaskToStopComponent(final String componentArgument) {
addUndoTask(new ControlCommand.UndoTask("Stopping component: " + componentArgument) {
public void performUndoWork() throws Exception {
Stop stopCommand = new Stop();
CommandLineParser parser = new PosixParser();
CommandLineParser parser = new RHQPosixParser(true);
CommandLine cmdLine = parser.parse(stopCommand.getOptions(), new String[] { componentArgument });
stopCommand.exec(cmdLine);
}
Expand Down

0 comments on commit 705dca8

Please sign in to comment.