Java Array Element Get getArgsOption(String[] args, String option)

Here you can find the source of getArgsOption(String[] args, String option)

Description

To get values for an given option from command line arguments.

License

Apache License

Parameter

Parameter Description
args a parameter
option a parameter

Return

an string array containing all values found for given option

Declaration

public static String[] getArgsOption(String[] args, String option) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2017 Capital One Services, LLC and Bitwise, Inc.
 * 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//from w  w  w  . ja v  a  2s  .co m
 *******************************************************************************/

import java.util.ArrayList;

public class Main {
    /**
     * To get values for an given option from command line arguments. Please
     * check tests for usage.
     * 
     * @param args
     * @param option
     * @return an string array containing all values found for given option
     */
    public static String[] getArgsOption(String[] args, String option) {

        if (args == null || args.length <= 0) {
            return null;
        }

        String optionChar = "-";
        String optionString = optionChar + option;

        ArrayList<String> values = new ArrayList<String>();

        int currentArgsIndex = -1;
        int totalArgs = args.length;
        for (String argument : args) {
            currentArgsIndex = currentArgsIndex + 1;

            // first check if there is something next, otherwise we will get
            // stringoutofindex
            if (currentArgsIndex >= totalArgs - 1) {

                // we have run out of arguments because there is nothing
                // next to be set as value. so just break.
                break;
            }

            if (argument.equalsIgnoreCase(optionString)) {
                // if argument matches the current option

                // Need to check if next option is a option itself or a
                // value
                if (args[currentArgsIndex + 1].startsWith(optionChar)) {
                    // if next argument is a option it means there is no
                    // value for current option and we need to move to next
                    // option
                    continue;

                } else {
                    // we are good to consider next value
                    values.add(args[currentArgsIndex + 1]);
                }

            }

            // below is end of for loop
        }

        if (values.size() <= 0) {
            // option not found so just return null
            return null;
        } else {
            return values.toArray(new String[values.size()]);
        }
    }
}

Related

  1. get(T[] array, int index)
  2. getAllMatches(String[] target, String[] pattern)
  3. getArgPairsSeparatedByChar(String[] args, String separator)
  4. getArgs(final String[] args, final String[] defaultValue, final String... flags)
  5. getArgs(String[] args, String... singleArgs)
  6. getArrayNoNull(Object[] array)
  7. getArrayPreview(final Object[] array, final int previewSize)
  8. getArrays(byte[] inputArray, int arraySize, boolean zeroPad)
  9. getArraySubset(T[] array, int start, int end)