climax icon indicating copy to clipboard operation
climax copied to clipboard

Iterators?

Open l0k18 opened this issue 6 years ago • 0 comments

I am using this library in my application at https://github.com/parallelcointeam/pod - because it seems to be the most friendly and accessible CLI flags library that exists for Go. However, my application needs some 40-50 separate parameters in most instances of the use of climax and I used a function wrapper to condense the declarations, but the ctx.Is and ctx.Get functions chew up a whole if block per parameter.

I am looking at it, and for the reason that the function that reads off the values from the command line to overlay the configuration struct for the subcommands, I looked closer at the source and noted that the variable and non-variable types have exported names and thus can be iterated.

Of course it is not (that) difficult to write this, but I'm just posting this issue to suggest that it could have an iterator built into it if one defined a map type that contains the pointers to the variables, in the case of variables, and for the nonvariable case, could produce an array containing the handlers associated with the nonvariable flags to enable order sensitivity to the triggers so in the case of a subcommand that terminates, that it can do this from one map literal declaration and the iterator command, and automatically executes it.

I am immediately in the process of refactoring my code's use of this by using an iterator on these maps, I would think that probably the function could be quickly modified to become generic, using a map[string]interface{}, enable the automatic determination of intended variable type, assignment and resolution of the interface to direct explicit type. I am using JSON as the configuration file format also, so I can constrain the types to match JSON in as much as objects go to maps and arrays to slices, then bool, int, float and string.

If there is interest in having a PR for adding this iterator, let me know, and I probably can create an additional source file to go with the library that implements this.

l0k18 avatar Feb 24 '19 08:02 l0k18