question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional arguments

See original GitHub issue

Please describe your suggestion Optional arguments

Please supply examples of the desired inputs and outputs Normal usage: User enters this:

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .withOptionalArgument(new IntegerArgument("optional"), 10)
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    int arg2 = (int) args[1]; // This is 10 if not existant
  })
  .register();

Basically gets converted into this:

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .withArgument(new IntegerArgument("optional"))
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    int arg2 = (int) args[1];
  })
  .register();

and

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    int arg2 = (int) args[1]; // This is 10, args.length = 2
  })
  .register();

Using annotations: Either something like this:

@Subcommand("mycommand")
@Arg(name = "int", type = IntegerArgument.class)
@OptionalArg(name = "int", type = IntegerArgument.class, optional = "10")
public void onCommand(int arg1, int arg2) {
  //...
}

or something like this:

@Subcommand("mycommand")
@Arg(name = "int", type = IntegerArgument.class)
@Arg(name = "optional", type = IntegerArgument.class)
public void onCommand(int arg1, @Default("10") int arg2) {
  //...
}

Cons of this via annotations is that the value must be a string, primitive, enum, annotation or class, which is really restrictive.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JorelAlicommented, Jul 11, 2022

It would be very good to get this implemented (or at least have made significant progress on this) for a 8.5.0 release due late July or early August.

0reactions
SNWCreationscommented, May 2, 2022

@SNWCreations

This code:

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .withOptionalArgument(new IntegerArgument("optional"), 10)
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    int arg2 = (int) args[1]; // This is 10 if not existant
  })
  .register();

Is the same as this code:

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    execute(sender, arg1, 10);
  })
  .register();

new CommandAPICommand("blah")
  .withArgument(new IntegerArgument("int"))
  .withArgument(new IntegerArgument("optional"))
  .executes((sender, args) -> {
    int arg1 = (int) args[0];
    int arg2 = (int) args[1];
    execute(sender, arg1, arg2);
  })
  .register();
  
public void execute(CommandSender sender, int arg1, int arg2) {
  // ...
}

Thanks! I got it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Python Optional Arguments When Defining Functions
In this section, you'll learn how to define a function that takes an optional argument. Functions with optional arguments offer more flexibility in...
Read more >
python - How do I define a function with optional arguments?
Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c . You would have...
Read more >
Optional Arguments (Guile Reference Manual) - GNU.org
6.7.4 Optional Arguments. Scheme procedures, as defined in R5RS, can either handle a fixed number of actual arguments, or a fixed number of...
Read more >
Python Optional Arguments: A How-To Guide - Career Karma
A Python optional argument is an argument with a default value. You can specify a default value for an argument using the assignment...
Read more >
C++ Default Parameters - W3Schools
A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found