cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Let the commands store flagComp functions internally (and avoid global state)

Open maxlandon opened this issue 10 months ago • 30 comments

Problem

Currently, flag completion functions are a global map, preventing garbage collection to work properly, especially if the commands using these funcs are garbage-collected themselves. This might happen if commands are declared, bound and used through yielder functions.

This is also produce more coherent API usage in different cases:

  • Declare flag completions close to where you declare the flags themselves,
  • When a code path happens to have access to a given command, it knows how to bind flags to it (in a more dynamic manner, for that respect).

In addition, and while in theory nothing prevents a given command cmd1 to be called with RegisterFlagsCompletion(cmd2Flag), and still store and work correctly with another command flag.

Changes

Add the old flagCompletionFunctions map and its mutex as unexported fields of the Command.

maxlandon avatar Aug 08 '23 02:08 maxlandon