net.morimekta.console.args.SubCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of console-util Show documentation
Show all versions of console-util Show documentation
Utilities helping with various *nix console topics. Mostly geared
toward expressive and interactive command line applications.
The newest version!
/*
* Copyright (c) 2016, Stein Eldar Johnsen
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package net.morimekta.console.args;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* Sub command base class.
*
* @param The sub-command instance type.
*/
public class SubCommand {
private final String name;
private final String usage;
private final List aliases;
private final boolean hidden;
private final Supplier instanceFactory;
private final Function parserFactory;
/**
* Make a single sub-command.
*
* @param name The name of the sub-command. Must be unuque.
* @param usage The usage string describing the sub-command.
* @param instanceFactory The instance supplier.
* @param parserFactory The argument parser supplier for an instance.
* @param aliases String aliases for the sub-command. Must be uniqye.
*/
public SubCommand(String name, String usage,
Supplier instanceFactory,
Function parserFactory,
String... aliases) {
this(name, usage, instanceFactory, parserFactory, Arrays.asList(aliases));
}
/**
* Make a single sub-command.
*
* @param name The name of the sub-command. Must be unuque.
* @param usage The usage string describing the sub-command.
* @param instanceFactory The instance supplier.
* @param parserFactory The argument parser supplier for an instance.
* @param aliases String aliases for the sub-command. Must be uniqye.
*/
public SubCommand(String name, String usage,
Supplier instanceFactory,
Function parserFactory,
Iterable aliases) {
this(name, usage, false, instanceFactory, parserFactory, aliases);
}
/**
* Make a single sub-command.
*
* @param name The name of the sub-command. Must be unuque.
* @param usage The usage string describing the sub-command.
* @param hidden True if the sub-command should not be visible by default.
* @param instanceFactory The instance supplier.
* @param parserFactory The argument parser supplier for an instance.
* @param aliases String aliases for the sub-command. Must be uniqye.
*/
public SubCommand(String name, String usage, boolean hidden,
Supplier instanceFactory,
Function parserFactory,
String... aliases) {
this(name, usage, hidden, instanceFactory, parserFactory, Arrays.asList(aliases));
}
/**
* Make a single sub-command.
*
* @param name The name of the sub-command. Must be unuque.
* @param usage The usage string describing the sub-command.
* @param hidden True if the sub-command should not be visible by default.
* @param instanceFactory The instance supplier.
* @param parserFactory The argument parser supplier for an instance.
* @param aliases String aliases for the sub-command. Must be uniqye.
*/
public SubCommand(String name, String usage, boolean hidden,
Supplier instanceFactory,
Function parserFactory,
Iterable aliases) {
this.name = name;
this.usage = usage;
this.instanceFactory = instanceFactory;
this.parserFactory = parserFactory;
this.hidden = hidden;
this.aliases = StreamSupport.stream(aliases.spliterator(), false)
.collect(Collectors.toList());
}
/**
* The sub-command name.
*
* @return The name.
*/
public String getName() {
return name;
}
/**
* The basic usage description.
*
* @return The usage description.
*/
public String getUsage() {
return usage;
}
/**
* If the sub-command is hidden by default.
*
* @return True if hidden.
*/
public boolean isHidden() {
return hidden;
}
/**
* Get the list of sub-command aliases.
*
* @return The aliases.
*/
public List getAliases() {
return aliases;
}
/**
* Instantiate the selected commands implementation.
*
* @return The new sub-command isntance.
*/
public SubCommandDef newInstance() {
return instanceFactory.get();
}
/**
* Get the sub-commands internal argument argumentParser initializes with it's
* own options.
*
* @param instance The instance to make parser for.
* @return The argument argumentParser.
*/
public ArgumentParser getArgumentParser(SubCommandDef instance) {
return parserFactory.apply(instance);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy