Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/cyclopsgroup/jcli/annotation/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@
* @return True if option has to be specified explicitly
*/
boolean required() default false;

/**
* @return Array of conflicting options
*/
String[] conflicts() default { };
}
10 changes: 9 additions & 1 deletion src/main/java/org/cyclopsgroup/jcli/impl/AnnotationOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AnnotationOption

private final Option option;

AnnotationOption( Option option, boolean flag, boolean multiValue )
AnnotationOption( Option option, boolean flag, boolean multiValue)
{
this.option = option;
this.flag = flag;
Expand Down Expand Up @@ -109,5 +109,13 @@ public boolean isRequired()
{
return option.required();
}

/**
* @inheritDoc
*/
@Override
public String[] getConflicts(){
return option.conflicts();
}

}
20 changes: 20 additions & 0 deletions src/main/java/org/cyclopsgroup/jcli/impl/DefaultBeanProcessor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.cyclopsgroup.jcli.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cyclopsgroup.jcli.spi.CommandLine;
import org.cyclopsgroup.jcli.spi.CommandLineParser;
import org.cyclopsgroup.jcli.spi.Option;

class DefaultBeanProcessor
{
Expand Down Expand Up @@ -49,6 +51,24 @@ static <T> void process( AnnotationParsingContext<T> context, List<String> argum
MultiValueReference<T> ref = (MultiValueReference<T>) context.lookupReference( entry.getKey(), false );
ref.setValues( bean, entry.getValue() );
}
//END of data load into bean
for(CommandLine.OptionValue ov : cli.getOptionValues())
{
Option cmOption;
for(Option contextOption: context.options())
{
Reference<T> ref = context.lookupReference( contextOption.getName(), false );
if(ref instanceof SingleValueReference<?> && ( (SingleValueReference<T>) ref ).getValue( bean ) == null)
{
continue;
}

if((cmOption = context.optionWithShortName(ov.name)) != null &&
Arrays.asList(cmOption.getConflicts()).contains(contextOption.getName()))
throw new AssertionError("grande errore cmOption: " + cmOption.getName() + " : " + contextOption.getName());
}
}
//
Reference<T> ref = context.lookupReference( DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME, false );
if ( ref == null )
{
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/cyclopsgroup/jcli/impl/OptionHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @author <a href="mailto:jiaqi@cyclopsgroup.org">Jiaqi Guo</a>
*/
@FixLengthType( length = 256 )
@FixLengthType( length = 485 )
public class OptionHelp
{
private final Option option;
Expand All @@ -23,14 +23,18 @@ public class OptionHelp
/**
* @return Description of option
*/
@FixLengthField( start = 26, length = 228 )
@FixLengthField( start = 26, length = 256 )
public String getDescription()
{
String desc = option.getDescription();
if ( !option.isFlag() && StringUtils.isNotBlank( option.getDefaultValue() ) )
{
desc += "(Default value is " + option.getDefaultValue() + ")";
}
if(option.getConflicts().length > 0)
{
desc += ", conflicts [\"" + StringUtils.join(option.getConflicts(), "\",\"" ) + "\"]";
}
return desc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ void setValue( T bean, String value )
{
ref.writeValue( converter.fromCharacters( value ), bean );
}

public Object getValue( T bean)
{
return ref.isReadable() ? ref.readValue( bean ) : null;
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/cyclopsgroup/jcli/spi/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public interface Option
* @return True if option is required
*/
boolean isRequired();

/**
* @return Array of conflicting options
*/
String[] getConflicts();
}
26 changes: 26 additions & 0 deletions src/test/java/org/cyclopsgroup/jcli/GnuParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@
*/
public class GnuParserTest
{

/**
* Verify use cases with all possible types of arguments
*/
@Test
public void testConflict()
{
ArgumentProcessor<Simple> p = ArgumentProcessor.newInstance( Simple.class, new GnuParser() );
Simple b = new Simple();
boolean thrown = false;
try{
p.process( new String[] { "-c", "123", "a", "-f", "abc"}, b );
}catch(AssertionError e){
thrown = true;
}
assertTrue("Conflict not detected", thrown);
thrown = false;
p = ArgumentProcessor.newInstance( Simple.class, new GnuParser() );
try{
p.process( new String[] { "-c", "123", "a", "--field1", "abc"}, b );
}catch(AssertionError e){
thrown = true;
}
assertTrue("Conflict not detected with long name", thrown);
}

/**
* Verify use cases with all possible types of arguments
*/
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/cyclopsgroup/jcli/Simple.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ else if ( optionName.equals( "f" ) )
private String stringFIeld2;

private List<String> values;

private String conflictFieldValue;

/**
* @return An integer option
*/
public final String getFieldWithConflict()
{
return conflictFieldValue;
}

/**
* @return An integer option
*/
Expand Down Expand Up @@ -106,6 +116,15 @@ public final boolean isBooleanField()
{
return booleanField;
}

/**
* @param conflict test
*/
@Option( name = "c", longName = "conflict", description = "Test conflicts", conflicts = {"f"})
public final void setFieldWithConflict( String conflictFieldValue )
{
this.conflictFieldValue = conflictFieldValue;
}

/**
* @param booleanField A flag option
Expand Down