001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.optionhandler;
021
022import java.io.PrintStream;
023import java.io.PrintWriter;
024
025import org.apache.commons.cli.HelpFormatter;
026import org.apache.commons.cli.Options;
027
028import org.apache.isis.core.runtime.sysout.SystemPrinter;
029
030public class BootPrinter extends SystemPrinter {
031
032    private final PrintWriter printWriter;
033    private final String className;
034
035    public BootPrinter(final Class<?> cls, final PrintStream output) {
036        super(output);
037        this.printWriter = new PrintWriter(getOutput());
038        className = cls.getName().substring(cls.getName().lastIndexOf('.') + 1);
039    }
040
041    public BootPrinter(final Class<?> cls) {
042        this(cls, System.out);
043    }
044
045    public void printErrorAndHelp(final Options options, final String formatStr, final Object... args) {
046        getOutput().println(String.format(formatStr, args));
047        printHelp(options);
048        printWriter.flush();
049    }
050
051    public void printHelp(final Options options) {
052        final HelpFormatter help = new HelpFormatter();
053        help.printHelp(printWriter, 80, className + " [options]", null, options, 0, 0, null, false);
054        printWriter.flush();
055    }
056
057}