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

org.apache.ivy.util.cli.CommandLineParser Maven / Gradle / Ivy

There is a newer version: 3.9
Show newest version
/*
 *  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 org.apache.ivy.util.cli;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.ivy.util.StringUtils;

public class CommandLineParser {
    private static final int MIN_DESC_WIDTH = 40;
    private static final int MAX_SPEC_WIDTH = 30;
    private Map/**/ options = new LinkedHashMap();
    private Map/*>*/ categories = new LinkedHashMap();
    
    public CommandLineParser() {
    }

    public CommandLineParser addCategory(String category) {
        categories.put(category, new ArrayList());
        return this;
    }
    
    public CommandLineParser addOption(Option option) {
        options.put(option.getName(), option);
        if (!categories.isEmpty()) {
            ((List) categories.values().toArray()[categories.values().size() - 1])
                .add(option);
        }
        return this;
    }
    
    public CommandLine parse(String[] args) throws ParseException {
        CommandLine line = new CommandLine();
        
        int index = args.length;
        ListIterator iterator = Arrays.asList(args).listIterator();
        while (iterator.hasNext()) {
            String arg = (String) iterator.next();
            if ("--".equals(arg)) {
                // skip this argument and stop looping
                index = iterator.nextIndex();
                break;
            }

            if (!arg.startsWith("-")) {
                index = iterator.previousIndex();
                break;
            }
            
            Option option = (Option) options.get(arg.substring(1));
            if (option == null) {
                throw new ParseException("Unrecognized option: " + arg);
            }
            line.addOptionValues(arg.substring(1), option.parse(iterator));
        }
        
        // left over args
        String[] leftOverArgs = new String[args.length - index];
        System.arraycopy(args, index, leftOverArgs, 0, leftOverArgs.length);
        line.setLeftOverArgs(leftOverArgs);

        return line;
    }

    public void printHelp(PrintWriter pw, int width, String command, boolean showDeprecated) {
        pw.println("usage: " + command);
        // compute the largest option spec
        int specWidth = 0;
        for (Iterator iterator = options.values().iterator(); iterator.hasNext();) {
            Option option = (Option) iterator.next();
            if (option.isDeprecated() && !showDeprecated) {
                continue;
            }
            specWidth = Math.min(MAX_SPEC_WIDTH, 
                Math.max(specWidth, option.getSpec().length()));
        }

        // print options help
        for (Iterator iterator = categories.entrySet().iterator(); iterator.hasNext();) {
            Entry entry = (Entry) iterator.next();
            String category = (String) entry.getKey();
            pw.println("==== " + category);
            List/*




© 2015 - 2025 Weber Informatics LLC | Privacy Policy