Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.cli;
 
 
Base class for classes with main methods. The command line is defined by annotating derived classes as described for the parser class.
 
 public abstract class Cli {
     protected final Console console;
     protected final Schema schema;
     
     @Option("-pretend")
     protected boolean pretend;
 
     @Option("v")
     public void setVerbose(boolean v) {
         .setVerbose(v);
     }
 
     public Cli() {
         this(new World());
     }
     
     public Cli(World world) {
         this(Console.create(world));
     }
     
     public Cli(Console console) {
         this(consolenew ReflectSchema(console.world));
     }
     
     public Cli(Console consoleSchema schema) {
         this. = console;
         this. = schema;
     }
 
     public abstract void printHelp();
     
     public int run(String... args) {
         Parser parser;
         Command command;
         
         parser = Parser.create(getClass());
         try {
             command = (Commandparser.run(thisargs);
             ..println("command line: " + Arrays.asList(args));
             if () {
                 ..println("pretend-only, command " + command + " is not executed");
             } else {
                 command.invoke();
             }
         } catch (ArgumentException e) {
             ..println(e.getMessage());
             ..println("Specify 'help' to get a usage message.");
             e.printStackTrace(.);
             return -1;
         } catch (RuntimeException e) {
             ..println(e.getMessage());
             e.printStackTrace(.);
             return -1;
         } catch (Exception e) {
             ..println(e.getMessage());
             e.printStackTrace(.);
             return -1;
         }
         return 0;
     }  
     
     @Child("help")
     public Command help() {
         return new Command() {
             public void invoke() throws Exception {
                 printHelp();
             }
         };
     }
    
    @Child("version")
    public Command version() {
        return new Command() {
            public void invoke() throws Exception {
                Package pkg;
                
                pkg = getClass().getPackage();
                if (pkg == null) {
                    ..println("unknown version");
                } else {
                    ..println(pkg.getName());
                    ..println("  specification title: " + pkg.getSpecificationTitle());
                    ..println("  specification version: " + pkg.getSpecificationVersion());
                    ..println("  specification vendor: " + pkg.getSpecificationVendor());
                    ..println("  implementation title: " + pkg.getImplementationTitle());
                    ..println("  implementation version: " + pkg.getImplementationVersion());
                    ..println("  implementation vendor: " + pkg.getImplementationVendor());
                }
                ..println("Platform encoding: " + System.getProperty("file.encoding"));
                ..println("Default Locale: " + Locale.getDefault());
                ..println("Scanner Locale: " + ..locale());
            }
        };
    }
New to GrepCode? Check out our FAQ X