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.

Enforce at least one argument in commands

See original GitHub issue

Variadic arguments are heavily used in the package. However, I think the way they are written now is quite error-prone. For example del https://github.com/denolib/deno-redis/blob/4ad600278d9c6aeccbbf624eb2593d895ebb9e1c/command.ts#L50 and most other commands require at least one argument. However, with the way the function is written above it’s easy to call del() without any arguments, and an error is thrown. It can be instead written like this, which is semantically similar but safer.

del(key: string, ...keys: string[]): Promise<Integer>;

Thoughts welcome!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tumilecommented, Aug 2, 2020

Good point. Not being able to use spread is quite painful. The error being thrown was the reason I opened this issue, I thought we could leverage type checking to protect users from themselves. But seeing that it’s actually expected behavior in other packages, maybe we should follow the crowd here.

1reaction
uki00acommented, Aug 1, 2020

PS) On second thought, I think the current behavior is not wrong 🤔

Reason 1: There may be a situation where users want to use an array

const keys = collectKeys();
await redis.del(...keys);

Reason 2: Some other clients behave the same way as this module

For example, go-redis, ioredis, and redis-cli gives the same error when no key passed:

go-redis

main.go

package main

import (
    "context"
    "fmt"
    "github.com/go-redis/redis/v8"  
)

var ctx = context.Background()

func main() {
    rdb := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    })
    keysToDelete := []string{}
    n, err := rdb.Del(ctx, keysToDelete...).Result()
    if err != nil {
      fmt.Printf("%v\n", err)
    } else {
      fmt.Printf("%d\n", n)
    }
}
$ go run main.go
ERR wrong number of arguments for 'del' command

ioredis

index.js

const Redis = require("ioredis");
const redis = new Redis();
(async () => {
  try {
    await redis.del([]);
  } catch (err) {
    console.error(err);
  }
})();
$ node index.js
ReplyError: ERR wrong number of arguments for 'del' command
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python argparse: Make at least one argument required
The program is meaningless without at least one parameter. How can I configure argparse to force at least one parameter to be chosen?...
Read more >
Adding arguments and options to your Bash scripts - Red Hat
First, add a variable and initialize it. Add the two lines shown in bold in the segment of the program shown below. This...
Read more >
Python Command Line Arguments
Python command line arguments are the key to converting your programs into useful and enticing tools that are ready to be used in...
Read more >
A Simple Guide To Command Line Arguments With ArgParse
Each one of these commands requires a unique set of arguments, and subparsers allow you to distinguish between them. This last example describes ......
Read more >
argparse — Parser for command-line options, arguments and ...
The add_argument() method must know whether an optional argument, like -f or --foo , or a positional argument, like a list of filenames,...
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