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.

Add commandline option constraints

See original GitHub issue


When poking around, I noticed that certain options that should only allow positive values (such as --genesis-time and --validator-count in teku genesis mock) will process negative values.

For negative genesis times, the checkArgument call will catch the problem and throw an exception.

$ teku genesis mock --genesis-time -1
java.lang.IllegalArgumentException: value (-1) must be >= 0
  <call stack>

But for most other options, it will continue as normal. For example, a negative validator count will not show any warnings. In this situation, validatorCount is effectively zero because IntStream.range(0, -1) is a range of zero. So it’s not really a problem here.

Below are some other options that could benefit from constraints. I’m able to specify negative values for all of them.


Possible Solution

Add validation constraints to some picocli options. Here is an example:

This would look like:

@Min(value = 0, message = "Genesis time must be a positive value")
    names = {"-t", "--genesis-time"},
    paramLabel = "<GENESIS_TIME>",
    description = "The genesis time")
private long genesisTime = System.currentTimeMillis() / 1000;

Note, another project dependency would be required.

implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

I would be happy to do this and submit a pull request if it’s something we want to fix.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

jtragliacommented, Jan 28, 2022

@ajsutton I can shave some yaks if you want. I don’t think :infrastructure:exceptions should have that dependency either. Also, I started a PR just to get the ball rolling. It doesn’t allow those options I mention in the issue to have negative values anymore. I also added validation checks to some other options too. I’m not sure if I should add more checks elsewhere or not. To my surprise, throwing IllegalArgumentException doesn’t print a stack trace and doesn’t look that bad, but I’m still planning on changing all of to InvalidConfigurationException exceptions.

ajsuttoncommented, Jan 28, 2022

To my surprise, throwing IllegalArgumentException doesn’t print a stack trace and doesn’t look that bad, but I’m still planning on changing all of to InvalidConfigurationException exceptions.

Yeah we must have special handling for IllegalArgumentException when parsing the options but definitely nicer to use the more specific exception type.

Just left a comment on the PR as well. It’s looking good - thanks for taking this on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Option constraint to accept only positive integers? #940 - GitHub
Am I correct to assume that currently System.CommandLine does not support constraints on integer options? Is there a convention how to implement ...
Read more >
Chapter 7. Resource Constraints Red Hat Enterprise Linux 7
When configuring a location constraint on a node, you can use the resource-discovery option of the pcs constraint location command to indicate a...
Read more >
SystemVerilog Command Line Arguments - ChipVerify
These arguments passed in from the command line are accessible in SV code via the following system functions called as plusargs. Syntax. $test$plusargs...
Read more >
Creating and modifying constraints - IBM
To define unique constraints through the command line, use the ADD CONSTRAINT option of the ALTER TABLE statement. For example, the following statement...
Read more >
picocli - a mighty tiny command line interface
Users sometimes run into system limitations on the length of a command line when creating a command line with lots of options or...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found