001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.fusesource.hawtbuf.proto.compiler;
019    
020    import java.util.ArrayList;
021    
022    /**
023     * Support utility that can be used to set the properties on any object
024     * using command line arguments.
025     * 
026     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
027     */
028    public class CommandLineSupport {
029            
030            /**
031             * Sets the properties of an object given the command line args.
032             * 
033             * if args contains: --ack-mode=AUTO --url=tcp://localhost:61616 --persistent 
034             * 
035             * then it will try to call the following setters on the target object.
036             * 
037             * target.setAckMode("AUTO");
038             * target.setURL(new URI("tcp://localhost:61616") );
039             * target.setPersistent(true);
040             * 
041             * Notice the the proper conversion for the argument is determined by examining the 
042             * setter argument type.  
043             * 
044             * @param target the object that will have it's properties set
045             * @param args the command line options
046             * @return any arguments that are not valid options for the target
047             */
048            static public String[] setOptions(Object target, String []args) {
049                    ArrayList rc = new ArrayList();
050                    
051                    for (int i = 0; i < args.length; i++) {
052                            if( args[i] == null )
053                                    continue;
054                            
055                            if( args[i].startsWith("--") ) {
056                                    
057                                    // --options without a specified value are considered boolean flags that are enabled.
058                                    String value="true";
059                                    String name = args[i].substring(2);
060                                    
061                                    // if --option=value case
062                                    int p = name.indexOf("=");
063                                    if( p > 0 ) {
064                                            value = name.substring(p+1);
065                                            name = name.substring(0,p);
066                                    }
067                                    
068                                    // name not set, then it's an unrecognized option
069                                    if( name.length()==0 ) {
070                                            rc.add(args[i]);
071                                            continue;
072                                    }
073                                    
074                                    String propName = convertOptionToPropertyName(name);
075                                    if( !IntrospectionSupport.setProperty(target, propName, value) ) {                                      
076                                            rc.add(args[i]);
077                                            continue;
078                                    }
079                            } else {
080                                rc.add(args[i]);
081                            }
082                            
083                    }
084                    
085                    String r[] = new String[rc.size()];
086                    rc.toArray(r);
087                    return r;
088            }
089    
090            /**
091             * converts strings like: test-enabled to testEnabled
092             * @param name
093             * @return
094             */
095            private static String convertOptionToPropertyName(String name) {
096                    String rc="";
097                    
098                    // Look for '-' and strip and then convert the subsequent char to uppercase
099                    int p = name.indexOf("-");
100                    while( p > 0 ) {
101                            // strip
102                            rc += name.substring(0, p);
103                            name = name.substring(p+1);
104                            
105                            // can I convert the next char to upper?
106                            if( name.length() >0 ) {
107                                    rc += name.substring(0,1).toUpperCase();
108                                    name = name.substring(1);
109                            }
110                            
111                            p = name.indexOf("-");
112                    }
113                    return rc+name;
114            }
115    }