@ -43,6 +43,7 @@
package org.eclipse.jgit.pgm.opt ;
import java.io.IOException ;
import java.io.Writer ;
import java.lang.reflect.Field ;
import java.util.ArrayList ;
@ -53,6 +54,7 @@ import java.util.ResourceBundle;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.Repository ;
import org.eclipse.jgit.pgm.Die ;
import org.eclipse.jgit.pgm.TextBuiltin ;
import org.eclipse.jgit.pgm.internal.CLIText ;
import org.eclipse.jgit.revwalk.RevCommit ;
@ -95,6 +97,8 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
private boolean seenHelp ;
private TextBuiltin cmd ;
/ * *
* Creates a new command line owner that parses arguments / options and set
* them into the given object .
@ -126,8 +130,12 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
* /
public CmdLineParser ( final Object bean , Repository repo ) {
super ( bean ) ;
if ( repo = = null & & bean instanceof TextBuiltin )
repo = ( ( TextBuiltin ) bean ) . getRepository ( ) ;
if ( bean instanceof TextBuiltin ) {
cmd = ( TextBuiltin ) bean ;
}
if ( repo = = null & & cmd ! = null ) {
repo = cmd . getRepository ( ) ;
}
this . db = repo ;
}
@ -167,6 +175,11 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
try {
super . parseArgument ( tmp . toArray ( new String [ tmp . size ( ) ] ) ) ;
} catch ( Die e ) {
if ( ! seenHelp ) {
throw e ;
}
printToErrorWriter ( CLIText . fatalError ( e . getMessage ( ) ) ) ;
} finally {
// reset "required" options to defaults for correct command printout
if ( backup ! = null & & ! backup . isEmpty ( ) ) {
@ -176,6 +189,18 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
}
}
private void printToErrorWriter ( String error ) {
if ( cmd = = null ) {
System . err . println ( error ) ;
} else {
try {
cmd . getErrorWriter ( ) . println ( error ) ;
} catch ( IOException e1 ) {
System . err . println ( error ) ;
}
}
}
private List < OptionHandler > unsetRequiredOptions ( ) {
List < OptionHandler > options = getOptions ( ) ;
List < OptionHandler > backup = new ArrayList < > ( options ) ;