The client is offline due to RuneScape update. You may see instance errors but this is due to the update and not actual instance errors. This should be resolved soon. Join our Discord for more information.

How to implement script arguments into a script with JCommander


  • Script Writer

    How to implement script arguments into a script with JCommander

    Script arguments are a powerful way for users to pass parameters to a script without needing a GUI and before the script has started. This guide will show you how to implement script arguments with JCommander so users of the script can pass parameters to the script.

    If you're a user wanting to learn how to pass script arguments to a script you can find a guide on that here.

    JCommander is a very small Java framework that makes it trivial to parse command line parameters. This will be a basic implementation of script arguments with JCommander; check their API docs to see what more it can do.


    Step 1

    In this example, we'll pretend these are the arguments for a woodcutting script.
    Create a class called Args. This is where you'll create and annotate your fields.

    public class Args {
        // The annotation allows you to specify information about the argument such as the name to parse it by or the description.
        @Parameter(names = "-treeType", description = "The tree type to chop.")
        public String treeType;
    
        // If you want to define a boolean parameter that’s true by default, you can declare it as having an arity of 1. 
        // Users will then have to specify the value they want explicitly.
        @Parameter(names = "-dropLogs", description = "True if the logs should be dropped; false otherwise.", arity = 1)
        public boolean dropLogs = false;
    
        @Parameter(names = "-logsToChop", description = "The amount of logs to chop before stopping.")
         // You can set the value of the field beforehand. If they do not specify `logsTopChop` as a parameter it will be set to 1000.
        public int logsTopChop = 1000;
    }
    

    Step 2

    Create and set up the main class to the script then create an instance of the Args class and pass it to JCommander along with the user arguments to parse.
    You can call the getArgs() method from the RSPeer API to get the user passed arguments.

    @ScriptMeta(developer = "Sphiinx", category = ScriptCategory.TOOL, name = "Example Script Args")
    public class ExampleScriptArgs extends Script {
    
        // Create a new instance of the `Args` class.
        private final Args args = new Args();
    
        @Override
        public void onStart() {
            final String userArgs = getArgs(); // Create a String field to store the user arguments.
            JCommander.newBuilder()
                    .addObject(args) // Pass the instance of the args class to JCommander.
                    .build()
                    .parse(userArgs.split(" ")); // Pass the String of user arguments to JCommander and split them by spaces so they can be parsed.
        }
    }
    

    Step 3

    After the script has started and JCommander has parsed the user arguments you can access the fields through the instance of args you created.

    @ScriptMeta(developer = "Sphiinx", category = ScriptCategory.TOOL, name = "Example Script Args")
    public class ExampleScriptArgs extends Script {
    
        // Create a new instance of the `Args` class.
        private final Args args = new Args();
    
        @Override
        public void onStart() {
            final String userArgs = getArgs(); // Create a String field to store the user arguments.
            JCommander.newBuilder()
                    .addObject(args) // Pass the instance of the args class to JCommander.
                    .build()
                    .parse(userArgs.split(" ")); // Pass the String of user arguments to JCommander and split them by spaces so they can be parsed.
        }
    
        @Override
        public int loop() {
            // Use the instance of args to access the fields.
            Log.fine("Tree Type: " + args.treeType);
            Log.fine("Drop Logs: " + args.dropLogs);
            Log.fine("Logs To Chop: " + args.logsTopChop);
            return 150;
        }
    }
    

    Step 4

    Example results:
    Arguments: -treeType Willow -dropLogs false -logsToChop 1500
    40619171-9ef8-4e69-8eb8-778659cfa992-image.png

    Arguments: -treeType Willow -dropLogs false
    e4e10da8-a876-4791-a01f-f836322be250-image.png


 

52
Online

16.6k
Users

1.4k
Topics

19.2k
Posts