1 87 package org.codehaus.loom.frontends; 88 89 import java.util.Properties ; 90 import org.apache.commons.cli.CommandLine; 91 import org.apache.commons.cli.CommandLineParser; 92 import org.apache.commons.cli.HelpFormatter; 93 import org.apache.commons.cli.Options; 94 import org.apache.commons.cli.ParseException; 95 import org.apache.commons.cli.PosixParser; 96 import org.codehaus.spice.salt.i18n.ResourceManager; 97 import org.codehaus.spice.salt.i18n.Resources; 98 99 102 class CLISetup 103 { 104 private static final Resources REZ = ResourceManager.getPackageResources( 105 CLISetup.class ); 106 107 private static final String DEBUG_LOG_OPT = "d"; 108 private static final String HELP_OPT = "h"; 109 private static final String LOG_FILE_OPT = "l"; 110 private static final String PERSISTENT_OPT = "p"; 111 private static final String CONFIGFILE_OPT = "f"; 112 private static final String STDOUT_OPT = "s"; 113 private static final String TEMPORARY_OPT = "t"; 114 115 private final Properties m_parameters = new Properties (); 117 118 private final String m_command; 120 121 public CLISetup( final String command ) 122 { 123 m_command = command; 124 } 125 126 129 private Options createCLOptions() 130 { 131 final Options options = new Options(); 132 options.addOption( STDOUT_OPT, 133 "std-out", 134 false, 135 REZ.getString( "cli.opt.configfile.stdout" ) ); 136 options.addOption( HELP_OPT, 137 "help", 138 false, 139 REZ.getString( "cli.opt.help.desc" ) ); 140 options.addOption( CONFIGFILE_OPT, 141 "configfile", 142 true, 143 REZ.getString( "cli.opt.configfile.desc" ) ); 144 options.addOption( LOG_FILE_OPT, 145 "log-file", 146 true, 147 REZ.getString( "cli.opt.log-file.desc" ) ); 148 options.addOption( DEBUG_LOG_OPT, 149 "debug-init", 150 false, 151 REZ.getString( "cli.opt.debug-init.desc" ) ); 152 options.addOption( PERSISTENT_OPT, 153 "persistent", 154 false, 155 REZ.getString( "cli.opt.persistent.desc" ) ); 156 options.addOption( TEMPORARY_OPT, 157 "temporary", 158 false, 159 REZ.getString( "cli.opt.temporary.desc" ) ); 160 return options; 161 } 162 163 public Properties getParameters() 164 { 165 return m_parameters; 166 } 167 168 public boolean parseCommandLineOptions( final String [] args ) 169 { 170 final CommandLineParser parser = new PosixParser(); 172 173 final Options options = createCLOptions(); 175 try 176 { 177 final CommandLine line = parser.parse( options, args ); 179 180 if( line.hasOption( HELP_OPT ) ) 181 { 182 final HelpFormatter formatter = new HelpFormatter(); 183 formatter.printHelp( m_command, options ); 184 return false; 185 } 186 if( line.getArgList().size() > 0 ) 187 { 188 final String message = REZ.getString( "cli.error.unknown.arg" ); 189 System.err.println( message ); 190 return false; 191 } 192 if( line.hasOption( DEBUG_LOG_OPT ) ) 193 { 194 m_parameters.setProperty( "log-priority", "DEBUG" ); 195 } 196 if( line.hasOption( TEMPORARY_OPT ) ) 197 { 198 m_parameters.setProperty( CLIMain.TEMPORARY, "true" ); 199 } 200 if( line.hasOption( PERSISTENT_OPT ) ) 201 { 202 m_parameters.setProperty( CLIMain.TEMPORARY, "false" ); 203 } 204 if( line.hasOption( CONFIGFILE_OPT ) ) 205 { 206 final String file = line.getOptionValue( CONFIGFILE_OPT ); 207 m_parameters.setProperty( CLIMain.CONFIGFILE, file ); 208 } 209 if( line.hasOption( LOG_FILE_OPT ) ) 210 { 211 final String file = line.getOptionValue( LOG_FILE_OPT ); 212 m_parameters.setProperty( "log-destination", file ); 213 } 214 if( line.hasOption( STDOUT_OPT ) ) 215 { 216 m_parameters.setProperty( "log-stdout", "true" ); 217 } 218 return true; 219 } 220 catch( final ParseException pe ) 221 { 222 final String message = REZ.format( "cli.error.parser", 223 pe.getMessage() ); 224 System.err.println( message ); 225 return false; 226 } 227 } 228 } 229 | Popular Tags |