Jump to content

CLI Support Made Easy


Recommended Posts

Posted (edited)

Here's a quick way to get key/value pairs from your CLI parameters.

We can take:

-script TestScript:body.green-d'hide-body.legs.preists-robes.location.varrock.mode.attack

And convert it to:

BODY:green d'hide body
LEGS:priests robes
LOCATION:varrock
MODE:attack

You're probably wondering 'why are the keys uppercase?', well that's to signify what the bot thinks are keys. A little debugging and you'll be able to see what your bot sees.

The full-stop "." is our delimiter, and the hyphen "-" is our replacement for the space " " key.

Example code + template:

public class Test extends Script {

	Map<String, String> parameters;
	
	String mode;
	
	@Override
	public void onStart() throws InterruptedException {
		
		compileParameters();
		
		mode = parameters.get("mode");
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		// log out the parameters for debugging purposes
		
		logger.debug(parameters);
		
		// use parameters
		
		if (mode != null && !mode.isEmpty()) {
			
			if (mode.equals("attack")) {
				
				// TODO - attack something
			}
		}
		
		return 1000;
	}
	
	/**
	 * Compiles OSBot parameters into key/value pairs
	 */
	private void compileParameters() {
		String rawParameterString = getParameters();
		String[] contents;
		String key;
		String value;
		parameters = new HashMap<>();
		if (rawParameterString != null && !rawParameterString.isEmpty()) {
			logger.debug(rawParameterString);
			contents = rawParameterString.split("\\.");
			if (contents != null && contents.length > 0) {
				for (int i = 0; i < contents.length; i += 2) {
					key = contents[i];
					key = key.toUpperCase();
					value = (i + 1 < contents.length ? contents[i + 1] : "");
					value = value.replaceAll("-", " ");
					parameters.put(key, value);
				}
			}
		}
	}

}

Have fun! :)

Edited by liverare
  • Like 7
  • Heart 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...