001package org.kohsuke.args4j; 002 003import java.io.File; 004import java.io.FileOutputStream; 005import java.io.IOException; 006 007import org.kohsuke.args4j.CmdLineException; 008import org.kohsuke.args4j.CmdLineParser; 009import org.kohsuke.args4j.OptionDef; 010import org.kohsuke.args4j.spi.OptionHandler; 011import org.kohsuke.args4j.spi.Parameters; 012import org.kohsuke.args4j.spi.Setter; 013 014/** 015 * An {@link OptionHandler} that can provide a {@link FileOutputStream} from 016 * a file name. 017 * 018 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 019 * 020 */ 021public class FileOutputStreamOptionHandler extends OptionHandler<FileOutputStream> { 022 /** 023 * Default constructor. 024 * @param parser the parser 025 * @param option the option definition 026 * @param setter the setter 027 */ 028 public FileOutputStreamOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super FileOutputStream> setter) { 029 super(parser, option, setter); 030 } 031 032 @Override 033 public String getDefaultMetaVariable() { 034 return "outputFile"; 035 } 036 037 @Override 038 public int parseArguments(Parameters params) throws CmdLineException { 039 File file = new File(params.getParameter(0)); 040 try { 041 setter.addValue(new FileOutputStream(file)); 042 } catch (IOException e) { 043 throw new CmdLineException(owner, "Error opening stream to output file " + file, e); 044 } 045 046 return 1; 047 } 048} 049