/* * Copyright 2015-2016 OpenCB * * Licensed 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 org.opencb.opencga.storage.app.cli; import com.beust.jcommander.DynamicParameter; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created on 26/01/16 * * @author Jacobo Coll <jacobo167@gmail.com> */ public class GeneralCliOptions { protected final JCommander jcommander; protected final GeneralOptions generalOptions; protected final CommonOptions commonOptions; public GeneralCliOptions() { generalOptions = new GeneralOptions(); jcommander = new JCommander(generalOptions); commonOptions = new CommonOptions(); } public void parse(String[] args) throws ParameterException { jcommander.parse(args); } public String getCommand() { return (jcommander.getParsedCommand() != null) ? jcommander.getParsedCommand() : ""; } public String getSubCommand() { String parsedCommand = jcommander.getParsedCommand(); if (jcommander.getCommands().containsKey(parsedCommand)) { String subCommand = jcommander.getCommands().get(parsedCommand).getParsedCommand(); return subCommand != null ? subCommand: ""; } else { return null; } } public boolean isHelp() { String parsedCommand = jcommander.getParsedCommand(); if (parsedCommand != null) { JCommander jCommander = jcommander.getCommands().get(parsedCommand); List<Object> objects = jCommander.getObjects(); if (!objects.isEmpty() && objects.get(0) instanceof CommonOptions) { return ((CommonOptions) objects.get(0)).help; } } return commonOptions.help; } /** * This class contains all those parameters available for all 'commands' */ public class CommandOptions { @Parameter(names = {"-h", "--help"}, description = "This parameter prints this help", help = true) public boolean help; public JCommander getSubCommand() { return jcommander.getCommands().get(getCommand()).getCommands().get(getSubCommand()); } public String getParsedSubCommand() { String parsedCommand = jcommander.getParsedCommand(); if (jcommander.getCommands().containsKey(parsedCommand)) { String subCommand = jcommander.getCommands().get(parsedCommand).getParsedCommand(); return subCommand != null ? subCommand: ""; } else { return ""; } } } public class GeneralOptions { @Parameter(names = {"-h", "--help"}, help = true) public boolean help; @Parameter(names = {"--version"}) public boolean version; } /** * This class contains all those common parameters available for all 'subcommands' */ public class CommonOptions { @Parameter(names = {"-h", "--help"}, description = "Print this help", help = true) public boolean help; @Parameter(names = {"-L", "--log-level"}, description = "One of the following: 'error', 'warn', 'info', 'debug', 'trace'") public String logLevel = "info"; @Parameter(names = {"--log-file"}, description = "One of the following: 'error', 'warn', 'info', 'debug', 'trace'") public String logFile; @Parameter(names = {"-v", "--verbose"}, description = "Increase the verbosity of logs") public boolean verbose = false; @Parameter(names = {"-C", "--conf"}, description = "Configuration file path.") public String configFile; @Parameter(names = {"--storage-engine"}, arity = 1, description = "One of the listed in storage-configuration.yml") public String storageEngine; @DynamicParameter(names = "-D", description = "Storage engine specific parameters go here comma separated, ie. -Dmongodb" + ".compression=snappy", hidden = false) public Map<String, String> params = new HashMap<>(); //Dynamic parameters must be initialized } public class IndexCommandOptions { @Parameter(names = {"-d", "--database"}, description = "DataBase name to load the data", required = false, arity = 1) public String dbName; @Parameter(names = {"-i", "--input"}, description = "File to index in the selected backend", required = true, variableArity = true) public String input; @Parameter(names = {"-o", "--outdir"}, description = "Directory where output files will be saved (optional)", arity = 1, required = false) public String outdir; } public class QueryCommandOptions { @Parameter(names = {"-o", "--output"}, description = "Output file. [STDOUT]", required = false, arity = 1) public String output; @Parameter(names = {"-d", "--database"}, description = "DataBase name", required = false, arity = 1) public String dbName; @Parameter(names = {"-i", "--include"}, description = "", required = false, arity = 1) public String include; @Parameter(names = {"-e", "--exclude"}, description = "", required = false, arity = 1) public String exclude; @Parameter(names = {"--skip"}, description = "Skip some number of elements.", required = false, arity = 1) public int skip; @Parameter(names = {"--limit"}, description = "Limit the number of returned elements.", required = false, arity = 1) public int limit; @Parameter(names = {"--count"}, description = "Count results. Do not return elements.", required = false, arity = 0) public boolean count; } }