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.

Summary

Implement ability to specify argument checks (preconditions) for command arguments in CNext.

Details

This would implement a way to have arguments validated by CNext. These checks would make it so that the data passed would have to match certain criteria for a command to be considered a valid overload. Examples include:

  • sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal
    • [NonNegative] would require that the number passed is >=0
    • [NonPositive] would require that the number passed is <= 0
    • [Positive] would require that the number passed is >0
    • [Negative] would require that the number passed is <0
    • [NonZero] would require that the number passed is !=0
    • [Min(int, [bool])] would require that the number passed has to be greater than (or greater than or equal to, if the optional argument is set to true, signifying inclusive check) the number specified
    • [Max(int, [bool])] much like above, but less than instead of greater than
    • [Even] would require the number passed to be even
    • [Odd] like above, but odd
  • string
    • [Uppercase] would require that the passed string be all uppercase
    • [Lowercase] would require that the passed string be all lowercase
    • [StartsWith(string, [bool])] would require the string passed to start with a specific string, optionally ignoring case
    • [EndsWith(string, [bool])] like above, but ends with instead of starts with
    • [Contains(string, [bool])] like above, but contains, rather than starts or ends with
    • [MaxLength(int)] requires that the passed string be not longer than specified number of characters
    • [MinLength(int)] like above, but not shorter instead of longer
    • [Regex(string, [RegexOptions])] would require the passed string to match specified regular expression
    • [Url] would require the passed string to be a valid URL

An example usage would be like this:

[Command]
public async Task SomeCommandAsync(CommandContext ctx,
    [Min(0), Max(10, true)] int arg)
{
    // the argument has to be 0 < arg <= 10
}

[Command]
public async Task AnotherCommandAsync(CommandContext ctx,
    [Regex("^\d{3}-\d{3}-\d{3}$", RegexOptions.ECMAScript)] string arg)
{
    // the argument has to consist of 3 groups of digits separated by dashes
}

This would remove the burden of validating inputs from the bot developers, since the command framework would handle all of it in a clean manner.

Notes

This could perhaps be tied into command overloads. You could make commands that have different overloads based on data passed (e.g. different overloads for negative numbers, different for non-negative ones, etc). This is currently not possible, the overloads are distinguished based on argument types alone.

Update: There’s also no way to do most of the checks in any remotely clean manner.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Naamlooscommented, Dec 6, 2018

knowing emzi, he was likely already planning to do so

0reactions
Neuheitcommented, Aug 4, 2021

Closing this due to slash commands.

Read more comments on GitHub >

github_iconTop Results From Across the Web

audit-argument-checks | Meteor API Docs
Documentation of Meteor's `audit-argument-checks` package. This package causes Meteor to require that all arguments passed to methods and publish functions ...
Read more >
Argument.Check 3.0.1
Argument.Check is a very simple library which makes it very easy to check your arguments or variables for different conditions like 'Null'.
Read more >
Argument-Checking - LANCAR
Argument -Checking. This project aims to contribute to the improvement of people's rhetorical literacy and critical thinking skills by developing procedures for ...
Read more >
Checking Arguments - NV5 Geospatial
IDL checks for a valid number of arguments but the routine itself must check the validity of types. This task consists of examining...
Read more >
How Do I Check the Structure of My Argument? - College of LSA
Your goal is to make a readable overview of your argument. In the margins of your paper, number the paper's paragraphs. Then, on...
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