ArgNameFormat.java

/*
 * Copyright 2023 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;

import net.morimekta.strings.NamingUtil;
import net.morimekta.strings.StringUtil;

/**
 * Name formatting for arguments. This can modify how arguments are named
 * based on field or method names.
 */
public enum ArgNameFormat {
    /**
     * Name arguments like <code>--lisp-naming</code>.
     */
    LISP("-", NamingUtil.Format.LISP, false),
    /**
     * Name arguments like <code>--snake_casing</code>.
     */
    SNAKE("_", NamingUtil.Format.SNAKE, false),
    /**
     * Name arguments like <code>--camelCasing</code>.
     */
    CAMEL("", NamingUtil.Format.CAMEL, true);

    private final String            sep;
    private final NamingUtil.Format nameFormat;
    private final boolean           capitalizeName;

    ArgNameFormat(String sep, NamingUtil.Format nameFormat, boolean capitalizeName) {
        this.sep = sep;
        this.nameFormat = nameFormat;
        this.capitalizeName = capitalizeName;
    }

    /**
     * @param name The argument name (not including '--' ) to format.
     * @return Argument name formatted using specified rule.
     */
    public String format(String name) {
        return NamingUtil.format(name, nameFormat);
    }

    /**
     * @param prefix Argument prefix.
     * @param name The argument name (not including '--' ) to format.
     * @return Argument name formatted using specified rule.
     */
    public String join(String prefix, String name) {
        if (prefix.isEmpty()) {
            return name;
        }
        if (capitalizeName) {
            return prefix + sep + StringUtil.capitalize(name);
        } else {
            return prefix + sep + name;
        }
    }
}