diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 121fd4da9..acf985624 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -55,6 +55,9 @@ var ( // ErrKeyNotFound is returned when a key is not found. ErrKeyNotFound = errors.New("key not found") + + // ErrNotEnoughArgs is returned with a cmd is being executed with fewer arguments. + ErrNotEnoughArgs = errors.New("not enough arguments") ) func main() { @@ -1000,6 +1003,9 @@ func (cmd *getCommand) Run(args ...string) error { // Require database path, bucket and key. relevantArgs := fs.Args() + if len(relevantArgs) < 3 { + return fmt.Errorf("expected arguments are 'database path', 'bucket', and 'key': %w", ErrNotEnoughArgs) + } path, buckets := relevantArgs[0], relevantArgs[1:len(relevantArgs)-1] key, err := parseBytes(relevantArgs[len(relevantArgs)-1], parseFormat) if err != nil { diff --git a/cmd/bbolt/main_test.go b/cmd/bbolt/main_test.go index e137db3e9..4461e30b5 100644 --- a/cmd/bbolt/main_test.go +++ b/cmd/bbolt/main_test.go @@ -649,6 +649,23 @@ func TestCompactCommand_Run(t *testing.T) { } } +func TestCommands_Run_NoArgs(t *testing.T) { + testCases := []struct { + cmd string + expErr error + }{ + { + cmd: "get", + expErr: main.ErrNotEnoughArgs, + }, + } + m := NewMain() + for _, tc := range testCases { + err := m.Run(tc.cmd) + require.ErrorIs(t, err, main.ErrNotEnoughArgs) + } +} + func fillBucket(b *bolt.Bucket, prefix []byte) error { n := 10 + rand.Intn(50) for i := 0; i < n; i++ {