sdcf4j icon indicating copy to clipboard operation
sdcf4j copied to clipboard

Documentation lyin'

Open xbony2 opened this issue 6 years ago • 4 comments

Command.usage() says

/**
* Gets the usage of the command.
* If no usage was provided it will use the first alias.
*
* @return The usage of the command.
*/
String usage() default "";

However when this command is called...

package xbony2.esaebsad2.commands

import de.btobastian.sdcf4j.Command
import de.btobastian.sdcf4j.CommandExecutor
import xbony2.esaebsad2.ESAEBSAD2;

class HelpCommand implements CommandExecutor {
	@Command(aliases = ["!help"], description = "The help command gives a list of commands.")
	onCommand(){
		def builder = new StringBuilder()
			
		builder.append "```"
		
		ESAEBSAD2.handler.getCommands().each { command ->
			builder.append "\n"
			def annotation = command.getCommandAnnotation()
			def emoji = getPermissionEmoji(annotation)
			println (annotation.usage() ?: "null")
			//builder.append "${annotation.usage()} | ${emoji} ${annotation.description()}"
			builder.append "${annotation.usage() ?: annotation.aliases()[0]} ${emoji}\n"
			builder.append "\t${annotation.description()}"
		}
		
		builder.append "\n```"
		builder.append "Permissions: 🙂 for everyone, 🚷 for Editors, ☢ for Moderators"
		builder.toString()
	}
	
	private static String getPermissionEmoji(Command command){
		switch(command.requiredPermissions()){
			case "moderator":
				return "☢"
			case "editor":
				return "🚷"
			default:
				return "🙂"
		}
	}
}

the println part gives "null" for each command. I apologize that this is in Groovy but the logic is very similar to Java.

So basically, the usage() method, instead of "using the first alias" "[i]f no usage was provided," just returns null.

xbony2 avatar Dec 17 '19 02:12 xbony2

Just as a side-note for non-Groovy-savvy people, the elvis operator in Groovy (or actually any boolean context) does not only do a null check, but also an emptiness check, so an empty string is considered false.

Vampire avatar Dec 17 '19 08:12 Vampire

Oh hmm. I can check if it's empty or null. But it's definitely not the first alias.

xbony2 avatar Dec 18 '19 01:12 xbony2

I think you misunderstand the documentation. You're working with the annotation. The annotation is unchanged and, in your example, usage() will still be empty since you didn't provide it. The documentation refers to how the value is used internally / how a default value will be chosen if the usage is left empty. It does not document the return value of a method. That's just how annotation values are defined, they look / are treated like methods for some reason. But it will only ever return what you have put in.

Saladoc avatar Dec 18 '19 02:12 Saladoc

It says "Gets the usage of the command. If no usage was provided it will use the first alias." If it is as you describe than the documentation seems pretty misleading then if the second sentence is an instruction for you to do rather than what it returns.

xbony2 avatar Dec 18 '19 03:12 xbony2