summaryrefslogtreecommitdiff
path: root/src/org/openoffice/tools/options/HelpFormatter.java
blob: d61ea0d95e331f20e78f35976bc5c10ce78b2378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.openoffice.tools.options;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;

public class HelpFormatter {

    private static final int TAB_SPACES = 4;
    private static final int COMMAND_SEP_SPACES = 2;

    public static void printHelp( String pProgram, Command pCommandLine ) {
        printHelp( System.out, 80, pProgram, pCommandLine );
    }
    
    public static void printHelp( PrintStream pOut, int pMaxLength, String pProgram, Command pCommandLine ) {
        
        org.apache.commons.cli.HelpFormatter formatter = new org.apache.commons.cli.HelpFormatter();
        formatter.setSyntaxPrefix( getFilledString( ' ', TAB_SPACES ) );
        PrintWriter pw = new PrintWriter( pOut );
        
        // Output the global usage
        pOut.println( "\nUsage:" );
        for ( SubCommand command : pCommandLine.getCommands() ) {
            String app = pProgram + " " + command.getName();
            formatter.printUsage( pw, pMaxLength, app, command.getOptions() );
        }
        pw.flush();
        
        // Output the commands list
        pOut.println( "\nSub-commands:" );
        printSubCommands( pOut, pMaxLength, pCommandLine.getCommands() ); 
        
        
        // Output the common options
        pOut.println( "\nCommon options:" );
        formatter.printOptions( pw, pMaxLength, pCommandLine.getCommonOptions(), TAB_SPACES, COMMAND_SEP_SPACES );
        pw.flush();
        
        
        // Output the commands options
        pOut.println( "\nSub-commands options:" );
        for ( SubCommand command : pCommandLine.getCommands() ) {
            int nbOptions = command.getOptions().getOptions().size();
            if ( nbOptions > 0 ) {
                pOut.println( getFilledString( ' ', TAB_SPACES ) + command.getName() );
                formatter.printOptions( pw, pMaxLength, command.getOptions(), TAB_SPACES * 2, COMMAND_SEP_SPACES );
                pw.flush();
            }
        }
    }

    private static void printSubCommands( PrintStream pOut, int pMaxLength, Collection<SubCommand> pCommands ) {
        int maxCmdLength = 0; 
        for ( SubCommand command : pCommands ) {
            if ( command.getName().length() > maxCmdLength ) {
                maxCmdLength = command.getName().length();
            }
        } 
        
        for ( SubCommand command : pCommands ) {
            String name = command.getName();
            String message = getFilledString( ' ', TAB_SPACES ) + name;
            
            int nameDiff = ( maxCmdLength - name.length() );
            message += getFilledString( ' ', nameDiff + COMMAND_SEP_SPACES );
            
            int restLen = pMaxLength - maxCmdLength - TAB_SPACES - COMMAND_SEP_SPACES;
            String aligningBlanks = getFilledString( ' ', pMaxLength - restLen ); 
            
            String[] words = command.getDescription().split( " " );
            int i = 0;
            boolean firstLine = true;
            String line = new String();
            while ( i < words.length ) {
                String word = words[i];
                if ( line.length() + 1 + word.length() < restLen ) {
                    if ( line.length() > 0 ) {
                        line += " ";
                    }
                    line += word;
                    
                    if ( i == words.length - 1 ) {
                        if ( !firstLine ) {
                            message += aligningBlanks;
                        }
                        message += line + "\n";
                    }
                    
                } else {
                    if ( !firstLine ) {
                        message += aligningBlanks;
                    }
                    message += line + "\n";
                    line = word;
                    firstLine = false;
                }
                i++;
            }
            
            pOut.print( message );
        }
    }

    private static String getFilledString(char pChar, int pNumber) {
        char[] chars = new char[pNumber];
        Arrays.fill( chars, pChar );
        return new String( chars );
    }
}