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

dev.jbang.Main Maven / Gradle / Ivy

There is a newer version: 0.121.0
Show newest version
package dev.jbang;

import java.util.ArrayList;
import java.util.List;

import dev.jbang.cli.JBang;

import picocli.CommandLine;

public class Main {
	public static void main(String... args) {
		CommandLine cli = JBang.getCommandLine();
		args = handleDefaultRun(cli.getCommandSpec(), args);
		int exitcode = cli.execute(args);
		System.exit(exitcode);
	}

	private static String[] handleDefaultRun(CommandLine.Model.CommandSpec spec, String[] args) {
		List leadingOpts = new ArrayList<>();
		List remainingArgs = new ArrayList<>();
		boolean foundParam = false;
		for (String arg : args) {
			if (!arg.startsWith("-") || arg.equals("-") || arg.equals("--")) {
				foundParam = true;
			}
			if (foundParam) {
				remainingArgs.add(arg);
			} else {
				leadingOpts.add(arg);
			}
		}
		// Check if we have a parameter and it's not the same as any of the subcommand
		// names
		if (!remainingArgs.isEmpty() && !spec.subcommands().containsKey(remainingArgs.get(0))
				|| hasRunOpts(leadingOpts)) {
			List result = new ArrayList<>();
			result.add("run");
			result.addAll(leadingOpts);
			result.addAll(remainingArgs);
			args = result.toArray(args);
		}
		return args;
	}

	private static boolean hasRunOpts(List opts) {
		boolean res = opts.contains("-i") || opts.contains("--interactive")
				|| opts.contains("-c") || opts.contains("--code") || opts.contains("--build-dir");
		res = res || opts	.stream()
							.anyMatch(o -> o.startsWith("-i=") || o.startsWith("--interactive=")
									|| o.startsWith("-c=") || o.startsWith("--code=") || o.startsWith("--build-dir="));
		return res;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy