All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.util.cli.Option Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2006 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
 * or see the FSF site: http://www.fsf.org.
 */

package com.greenpepper.util.cli;

import com.greenpepper.util.CollectionUtil;

import java.util.Arrays;
import java.util.List;

/**
 * 

Option class.

* * @author oaouattara * @version $Id: $Id */ public class Option { private String name; private String shortOpt; private String longOpt; private String description; private String arg; private Object value; private Converter converter = CommandLine.converterFor( String.class ); private Stub stub = new DoNothing(); /** *

Constructor for Option.

* * @param name a {@link java.lang.String} object. */ public Option( String name ) { this.name = name; } /** *

isValid.

* * @return a boolean. */ public boolean isValid() { return shortOpt != null || longOpt != null; } /** *

consume.

* * @param args a {@link java.util.List} object. * @throws com.greenpepper.util.cli.WrongOptionUsageException if any. */ public void consume( List args ) throws WrongOptionUsageException { if (wantsArg() && (args.isEmpty())) throw new WrongOptionUsageException( this ); value = wantsArg() ? convert( CollectionUtil.shift( args ) ) : true; } private Object convert( String value ) throws WrongOptionUsageException { try { return converter.convert( value ); } catch (Exception e) { throw new WrongOptionUsageException( this, e ); } } /** *

Getter for the field name.

* * @return a {@link java.lang.String} object. */ public String getName() { return name; } /** *

getShortForm.

* * @return a {@link java.lang.String} object. */ public String getShortForm() { return shortOpt; } /** *

setShortForm.

* * @param shortOpt a {@link java.lang.String} object. */ public void setShortForm( String shortOpt ) { this.shortOpt = shortOpt; } /** *

getLongForm.

* * @return a {@link java.lang.String} object. */ public String getLongForm() { return longOpt; } /** *

setLongForm.

* * @param longOpt a {@link java.lang.String} object. */ public void setLongForm( String longOpt ) { this.longOpt = longOpt; } /** *

Getter for the field description.

* * @return a {@link java.lang.String} object. */ public String getDescription() { return description; } /** *

Setter for the field description.

* * @param description a {@link java.lang.String} object. */ public void setDescription( String description ) { this.description = description; } /** *

Getter for the field arg.

* * @return a {@link java.lang.String} object. */ public String getArg() { return arg; } /** *

Setter for the field arg.

* * @param arg a {@link java.lang.String} object. */ public void setArg( String arg ) { this.arg = arg; } /** *

Getter for the field value.

* * @return a {@link java.lang.Object} object. */ public Object getValue() { return value; } /** *

Setter for the field value.

* * @param value a {@link java.lang.Object} object. */ public void setValue( Object value ) { this.value = value; } /** *

Getter for the field converter.

* * @return a {@link com.greenpepper.util.cli.Converter} object. */ public Converter getConverter() { return converter; } /** *

Setter for the field converter.

* * @param converter a {@link com.greenpepper.util.cli.Converter} object. */ public void setConverter( Converter converter ) { this.converter = converter; } /** *

wantsArg.

* * @return a boolean. */ public boolean wantsArg() { return arg != null; } void describeTo( StringBuilder sb ) { if (shortOpt != null) sb.append( shortOpt ); else sb.append( " " ); if (longOpt != null) { if (shortOpt != null) sb.append( ", " ); else sb.append( " " ); sb.append( longOpt ); } if (wantsArg()) sb.append( " " ).append( arg ); if (description != null) { justify( sb ); sb.append( description ); } } private void justify( StringBuilder sb ) { int padding = 30 - sb.length(); if (padding <= 0) { sb.append( "\n" ); padding = 30; } char[] filler = new char[padding]; Arrays.fill( filler, ' ' ); sb.append( filler ); } /** *

toString.

* * @return a {@link java.lang.String} object. */ public String toString() { StringBuilder sb = new StringBuilder(); describeTo( sb ); return sb.toString(); } /** *

Setter for the field stub.

* * @param stub a {@link com.greenpepper.util.cli.Option.Stub} object. */ public void setStub( Stub stub ) { this.stub = stub; } boolean wasGiven() { return value != null; } /** *

call.

*/ public void call() { stub.call( this ); } public interface Stub { void call( Option option ); } public static class DoNothing implements Stub { public void call( Option option ) { } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy