Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message should show enum constant names, not toString() values, after value mismatch #592

Closed
startewho opened this issue Jan 7, 2019 · 5 comments

Comments

@startewho
Copy link

It seems the enum type can 't can be parsed correctly.

public enum OutType {
	WORD("word",".docx" ,1), EXCEL("excel",".xlsx" ,2), MARKDOWN("markdown",".md", 3);
    // 成员变量
    private String name;
    private String extension;
    private int index;

    // 构造方法
    private OutType(String name,String extension,int index) {
        this.name = name;
        this.extension=extension;
        this.index = index;
    }

    
    public String getExtension() {
		return this.extension;
	}
    
    public String getName() {
		return this.name;
	}
    
    public int getIndex() {
		return this.index;
	}
    
 // 覆盖方法
    @Override
    public String toString() {
        return this.name;
    }
    
}

and my command is like this:

public class CommandArgs {

	
	@Option(names = {"-V", "--version"}, versionHelp = true, description = "版本信息")
	public	boolean version;

	@Option(names = {"-h", "--help"}, usageHelp = true, description = "帮助信息")
	public boolean help;

	
	@Option(names= {"-o","--out"},defaultValue="./outdoc",description="输出文档路径")
	private String outPath="./";
	
	
	@Option(names= {"-t","--type"},description="导出格式为:${COMPLETION-CANDIDATES}")
	public OutType outType=OutType.MARKDOWN;
	
	
	/**
	 * 获取输出路径
	 * @return
	 */
	public String getOutPath() {
		
		if (outPath.lastIndexOf('.')<=0) {
			return outPath+outType.getExtension();
		}
		return outPath;
		
	}

And i use it like this:

public static void main(String args[]) throws IOException {
		
		
		CommandArgs command = CommandLine.populateCommand(new CommandArgs(), args);
		
		if (command.help) {
			ColorScheme colorScheme = new ColorScheme()
			        .commands    (Style.bold, Style.underline)    // combine multiple styles
			        .options     (Style.fg_yellow)                // yellow foreground color
			        .parameters  (Style.fg_yellow)
			        .optionParams(Style.italic);

			   CommandLine.usage(new CommandArgs(), System.out,colorScheme);
			   return;
		}
		if (command.version) {
			System.out.print("Version:0.0.1");
			return;
		}
		
		try {
			
			genOutput(command);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("生成完毕!!!");
		// 打开生成的文件
		//Runtime.getRuntime().exec("cmd.exe /c start " + out_dir);
	}

and when i use my app like this

PS D:\Project\Tools\DbDoc\target> java -jar .\dbdoc.jar -t=word

and the error is shown

Exception in thread "main" picocli.CommandLine$ParameterException: Invalid value for option '--type': expected one of [word, excel, markdown] but was 'word'
​ at picocli.CommandLine$Interpreter.tryConvert(CommandLine.java:7485)
​ at picocli.CommandLine$Interpreter.applyValueToSingleValuedField(CommandLine.java:7145)
​ at picocli.CommandLine$Interpreter.applyOption(CommandLine.java:7082)
​ at picocli.CommandLine$Interpreter.processStandaloneOption(CommandLine.java:6974)
​ at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:6883)
​ at picocli.CommandLine$Interpreter.parse(CommandLine.java:6756)
​ at picocli.CommandLine$Interpreter.parse(CommandLine.java:6638)
​ at picocli.CommandLine.parse(CommandLine.java:727)
​ at picocli.CommandLine.populateCommand(CommandLine.java:680)
​ at com.dbdoc.App.main(App.java:37)

i can't see what 's the difference between these two 'word'.

So,is there something in my code?

@remkop
Copy link
Owner

remkop commented Jan 7, 2019

I was able to reproduce the issue (thanks for providing all the details).

By default, Picocli only recognizes enum values constant names. These are all uppercase in your program, so your program will accept -t=WORD, but -t=word will be rejected because the word value does not match exactly.

You can tell Picocli to recognize enum values in a case-insensitive way, like this:

CommandArgs command = new CommandArgs();
new CommandLine(command).setCaseInsensitiveEnumValuesAllowed(true).parseArgs(args);

The error message from picocli is confusing. It should say:

expected one of [WORD, EXCEL, MARKDOWN] but was 'word'

I will improve this error message in the next release. Thanks for raising this issue!

@remkop remkop added this to the 3.9.1 milestone Jan 7, 2019
@remkop remkop changed the title enum type can't parse correct Error message should show enum constant names, not toString() values, after value mismatch Jan 7, 2019
@startewho
Copy link
Author

Thanks a lot

@remkop
Copy link
Owner

remkop commented Jan 7, 2019

I pushed a fix to master that improves the error message.
Please let me know if there are any issues.

Did the CommandLine::setCaseInsensitiveEnumValuesAllowed(true) suggestion solve your issue?

@startewho
Copy link
Author

I pushed a fix to master that improves the error message.
Please let me know if there are any issues.

Did the CommandLine::setCaseInsensitiveEnumValuesAllowed(true) suggestion solve your issue?

yes ,it 's solved

@remkop
Copy link
Owner

remkop commented Jan 8, 2019

Thanks for the confirmation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants