View Javadoc
1   package de.spiritscorp.datasync;
2   
3   /*-
4    * 		Data Sync
5    *
6    * 		Copyright ©   2022    The Spirit
7    * 		@email                        thespirit@spiritscorp.network
8    *
9    * 		This program is free software; you can redistribute it and/or modify
10   * 		it under the terms of the GNU General Public License as published by
11   * 		the Free Software Foundation; either version 3 of the License, or
12   * 		(at your option) any later version.
13   *
14   * 		This program is distributed in the hope that it will be useful,
15   * 		but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * 		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17   * 		See the GNU General Public License for more details.
18   *
19   * 		You should have received a copy of the GNU General Public License
20   * 		along with this program. If not, see <http://www.gnu.org/licenses/>.
21   */
22  
23  import java.util.Locale;
24  
25  /**
26   * Defines the supported command-line interface (CLI) switches and arguments
27   * utilized to configure the application's runtime environment during the bootstrapping phase.
28   *
29   * @author Tom Spirit
30   * @version 1.0.0
31   */
32  public enum CLIFlags {
33  
34  	/** Command-line argument to specify a custom directory path hosting configuration files, profiles, and sync maps. */
35  	CONFIG_DIR( "--config-dir", "-c" ),
36  	/** Command-line flag to enforce an extended initialization pause during system boot sequences to prevent resource contention. */
37  	BOOT_DELAY( "--boot-delay", "-b" ),
38  	/** Command-line flag to activate verbose diagnostic logging streams to the standard console output. */
39  	DEBUG( "--debug", "-d" ),
40  	/** Command-line flag to mirror or redirect diagnostic debug streams into a local file system log repository. */
41  	DEBUG_TO_FILE( "--debug-to-file", "-f" ),
42  	/** Fallback flag used when no matching command-line argument is detected to prevent NullPointerExceptions. */
43  	NONE( "", "" );
44  
45  	/** The verbose, multi-character command-line argument identifier. */
46  	private final String longFlag;
47  	/** The single-character mnemonic shortcut alias for rapid terminal execution. */
48  	private final String shortFlag;
49  
50  	/**
51  	 * Constructs a new command-line flag definition mapped to its corresponding terminal input literals.
52  	 *
53  	 * @param longFlag  The verbose argument identifier starting with a double dash
54  	 * @param shortFlag The single-character mnemonic alias starting with a single dash
55  	 */
56  	CLIFlags( final String longFlag, final String shortFlag ) {
57  		this.longFlag = longFlag;
58  		this.shortFlag = shortFlag;
59  	}
60  
61  	/**
62  	 * Resolves a raw command-line argument string to its corresponding strongly-typed {@link CLIFlags} representation.
63  	 * <p>
64  	 * This method serves as the central factory for command-line argument mapping and employs a defensive,
65  	 * null-safe lookup strategy. It supports exact match evaluation for standardized flags as well as
66  	 * prefix-based evaluation to handle parameterized switches (such as directory paths attached directly
67  	 * to the configuration argument).
68  	 * </p>
69  	 * <p>
70  	 * If the input string is {@code null}, blank, or fails to match any registered application switch,
71  	 * this method returns {@link #NONE} to ensure safe downstream processing without the risk of
72  	 * encountering a {@link NullPointerException}.
73  	 * </p>
74  	 *
75  	 * @param arg the raw terminal argument to evaluate (can be {@code null} or blank)
76  	 * @return the resolved {@link CLIFlags} enum constant, or {@link #NONE} as a robust fallback if unrecognized
77  	 */
78  	public static CLIFlags fromArgument( final String arg ) {
79  		if( arg == null || arg.isBlank() ) return NONE;
80  		for( final CLIFlags flag : values() ) {
81  			if( flag == NONE ) continue;
82  			final String lowArg = arg.toLowerCase( Locale.ROOT );
83  			if( flag.isExactMatch( lowArg ) || flag.isPrefixMatch( lowArg ) ) { return flag; }
84  		}
85  		return NONE;
86  	}
87  
88  	/**
89  	 * Retrieves the verbose argument identifier prefix.
90  	 *
91  	 * @return The long flag string
92  	 */
93  	public String getLongFlag() { return longFlag; }
94  
95  	/**
96  	 * Retrieves the single-character mnemonic alias prefix.
97  	 *
98  	 * @return The short flag string
99  	 */
100 	public String getShortFlag() { return shortFlag; }
101 
102 	/**
103 	 * Checks for a standard, exact command-line switch match.
104 	 */
105 	private boolean isExactMatch( final String arg ) {
106 		return longFlag.equals( arg ) || shortFlag.equals( arg );
107 	}
108 
109 	/**
110 	 * Checks for prefix-based argument structures (specifically restricted to the configuration directory path).
111 	 */
112 	private boolean isPrefixMatch( final String arg ) {
113 		return this == CONFIG_DIR && ( arg.startsWith( longFlag ) || arg.startsWith( shortFlag ) );
114 	}
115 }