summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/cli/CLITools.java
blob: 1b732072fe2b30bc20f986e91374b910e401a2e9 (plain) (blame)
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
112
113
114
115
116
117
package cz.crcs.ectester.common.cli;

import cz.crcs.ectester.common.ec.EC_Category;
import cz.crcs.ectester.common.ec.EC_Data;
import cz.crcs.ectester.data.EC_Store;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class CLITools {

    /**
     * Print help.
     */
    public static void help(String prog, String header, Options options, String footer, boolean usage) {
        HelpFormatter help = new HelpFormatter();
        help.setOptionComparator(null);
        help.printHelp(prog, header, options, footer, usage);
    }

    private static void help(HelpFormatter help, PrintWriter pw, CommandLineParser cli, int depth) {
        if (cli instanceof TreeParser) {
            TreeParser tp = (TreeParser) cli;
            tp.getParsers().forEach((key, value) -> {
                help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + String.valueOf(depth) + "s" + key + ":", " "));
                help.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, value.getOptions(), HelpFormatter.DEFAULT_LEFT_PAD + depth, HelpFormatter.DEFAULT_DESC_PAD);
                pw.println();
                CLITools.help(help, pw, value.getParser(), depth + 1);
            });
        }
    }

    private static void usage(HelpFormatter help, PrintWriter pw, CommandLineParser cli, Options opts) {
        StringWriter sw = new StringWriter();
        PrintWriter upw = new PrintWriter(sw);
        help.printUsage(upw, HelpFormatter.DEFAULT_WIDTH, "", opts);
        upw.print(" ");
        if (cli instanceof TreeParser) {
            TreeParser tp = (TreeParser) cli;
            if (!tp.isRequired()) {
                upw.print("[ ");
            }
            tp.getParsers().forEach((key, value) -> {
                upw.print("( " + key + " ");
                usage(help, upw, value.getParser(), value.getOptions());
                upw.print(")");
            });
            if (!tp.isRequired()) {
                upw.print(" ]");
            }
        }
        pw.println(sw.toString().substring(8).replace("\n", ""));
    }

    /**
     * Print tree help.
     */
    public static void help(String prog, String header, Options baseOpts, TreeParser baseParser, String footer, boolean printUsage) {
        HelpFormatter help = new HelpFormatter();
        help.setOptionComparator(null);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        if (printUsage) {
            StringWriter uw = new StringWriter();
            PrintWriter upw = new PrintWriter(uw);
            usage(help, upw, baseParser, baseOpts);
            pw.print(prog + " usage: ");
            help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, uw.toString());
            upw.close();
        }
        help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, header);
        help.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, baseOpts, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD);
        pw.println();
        help(help, pw, baseParser, 1);
        help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, footer);
        System.out.println(sw.toString());
    }

    /**
     * Print version info.
     */
    public static void version(String description, String license) {
        System.out.println(description);
        System.out.println(license);
    }

    /**
     * List categories and named curves.
     */
    public static void listNamed(EC_Store dataStore, String named) {
        Map<String, EC_Category> categories = dataStore.getCategories();
        if (named == null) {
            // print all categories, briefly
            for (EC_Category cat : categories.values()) {
                System.out.println(cat);
            }
        } else if (categories.containsKey(named)) {
            // print given category
            System.out.println(categories.get(named));
        } else {
            // print given object
            EC_Data object = dataStore.getObject(EC_Data.class, named);
            if (object != null) {
                System.out.println(object);
            } else {
                System.err.println("Named object " + named + " not found!");
            }
        }
    }
}