OptionUtils.java

/*
 * Copyright 2022 Terminal Utils Authors
 *
 * 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.terminal.args.impl;

import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

/**
 * Validation utilities for option and argument names.
 */
public final class OptionUtils {
    public static String requireValidArgumentName(String name) {
        requireNonNull(name, "name == null");
        if (name.isEmpty()) {
            throw new IllegalArgumentException("Empty argument name.");
        }
        if (!ARGUMENT_NAME_PATTERN.matcher(name).matches()) {
            throw new IllegalArgumentException("Invalid argument name \"" + name + "\", must be an identifier string, e.g.: \"name\".");
        }
        return name;
    }

    public static String requireValidLongName(String name) {
        requireNonNull(name, "name == null");
        if (name.isEmpty()) {
            throw new IllegalArgumentException("Empty long name.");
        }
        if (!LONG_NAME_PATTERN.matcher(name).matches()) {
            throw new IllegalArgumentException("Invalid name \"" + name + "\", must be an identifier string prefixed with '--', e.g.: \"--name\".");
        }
        return name;
    }

    public static String requireValidShortNames(String shortNames) {
        requireNonNull(shortNames, "shortNames == null");
        if (shortNames.isEmpty()) {
            throw new IllegalArgumentException("Empty short name character set.");
        }
        if (!SHORT_NAME_PATTERN.matcher(shortNames).matches()) {
            throw new IllegalArgumentException("Invalid short name set \"" + shortNames + "\", can only be letters, numbers, '?' or '!'.");
        }
        return shortNames;
    }

    public static String requireValidUsage(String usage) {
        requireNonNull(usage, "usage == null");
        if (usage.isEmpty()) {
            throw new IllegalArgumentException("Empty usage string.");
        }
        return usage;
    }

    private static final Pattern LONG_NAME_PATTERN     = Pattern.compile("--[a-zA-Z0-9]+(?:[-_][a-zA-Z0-9]+)*");
    private static final Pattern ARGUMENT_NAME_PATTERN = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*(?:[-_][a-zA-Z0-9]+)*");
    private static final Pattern SHORT_NAME_PATTERN    = Pattern.compile("[a-zA-Z0-9?!]+");

    private OptionUtils() {}
}