All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinystruct.system.cli.CommandLine Maven / Gradle / Ivy

Go to download

A simple framework for Java development. Simple is hard, Complex is easy. Better thinking, better design.

There is a newer version: 1.3.4
Show newest version
package org.tinystruct.system.cli;

import org.tinystruct.Application;
import org.tinystruct.system.util.StringUtilities;

import java.io.File;
import java.util.*;

public class CommandLine implements Comparable {
    private final String command;
    private String description;
    private final Application app;
    private List options = new ArrayList<>();
    private Set> arguments = new HashSet<>(16);
    private String example;

    public static final int ARGUMENT_MAX_WIDTH = 77;

    public CommandLine(Application app, String command, String description) {
        this.app = app;
        this.command = command;
        this.description = description;
    }

    public CommandLine(Application app, String command, String description, Set> arguments, List options) {
        this(app, command, description);

        this.arguments = arguments;
        this.options = options;
    }

    public Application getApplication() {
        return this.app;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getExample() {
        return example;
    }

    public void setExample(String value) {
        this.example = value;
    }

    public List getOptions() {
        return options;
    }

    public CommandLine setOptions(List options) {
        this.options = options;

        return this;
    }

    public Set> getArguments() {
        return arguments;
    }

    public CommandLine addArgument(CommandArgument argument) {
        this.arguments.add(argument);

        return this;
    }

    public CommandLine setArguments(Set> arguments) {
        this.arguments = arguments;
        return this;
    }

    @Override
    public String toString() {
        StringBuilder placeholders = new StringBuilder();
        for (CommandArgument argument : arguments) {
            placeholders.append("/{").append(argument.getKey()).append("}");
        }

        StringBuilder help = new StringBuilder("Usage: bin" + File.separator + "dispatcher " + command + placeholders + " [OPTIONS]\n");
        help.append(this.description).append("\n");

        if (arguments.size() > 0) {
            OptionalInt longSizeCommand = this.arguments.stream().mapToInt(o -> o.getKey().length()).max();
            int max = longSizeCommand.orElse(0);

            help.append("Arguments: \n");
            for (CommandArgument argument : arguments) {
                help.append("\t")
                        .append(StringUtilities.rightPadding(argument.getKey(), max, ' '))
                        .append("\t")
                        .append(argument.getDescription().isEmpty() ? "Not specified" : argument.getDescription())
                        .append("\n");
            }
        }

        if (options.size() > 0) {
            OptionalInt longSizeCommand = this.options.stream().mapToInt(o -> o.getKey().length()).max();
            int max = longSizeCommand.orElse(0);

            help.append("Options: \n");
            for (CommandArgument argument : options) {
                help.append("\t").append(StringUtilities.rightPadding(argument.getKey(), max, ' ')).append("\t").append(argument.getDescription()).append("\n");
            }
        }

        if (this.example != null) {
            help.append("Example(s): \n").append(this.example).append("\n");
        }

        return help.toString();
    }

    public String getCommand() {
        return this.command;
    }

    public String help() {
        return this.toString();
    }

    @Override
    public int compareTo(CommandLine o) {
        return this.getCommand().compareTo(o.getCommand());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy