Conversation
This is a POC that it's possible to relax the `constraint.Integer` constraint into simply `constraint.Ordered`. This means in particular that string enum can be accepted, which I found to be a common case. It seems to work, but I may not have covered all the cases properly. Signed-off-by: Michael Muré <batolettre@gmail.com>
e4cc308 to
d64c71d
Compare
|
Thank you very much for catching this and contributing this improvement! I will later cross-check just in case, but this is definitely good to have. How to you use it with strings? I'm wondering if defining our own constraint type from basically |
|
Really like you would expect: type Sample string
const (
SampleInuseSpace Sample = "inuse_space"
SampleInuseObjects = "inuse_objects"
SampleAllocSpace = "alloc_space"
SampleAllocObjects = "alloc_objects"
)
// ------
var allocSampleIds = map[engine.Sample][]string{
engine.SampleInuseSpace: {"inuse_space"},
engine.SampleInuseObjects: {"inuse_objects"},
engine.SampleAllocSpace: {"alloc_space"},
engine.SampleAllocObjects: {"alloc_objects"},
}
var allocSampleHelp = map[engine.Sample]string{
engine.SampleInuseSpace: "Memory in use (space)",
engine.SampleInuseObjects: "Memory in use (objects)",
engine.SampleAllocSpace: "Memory allocated (space)",
engine.SampleAllocObjects: "Memory allocated (objects)",
}
sampleEnum := enumflag.New(&options.sample, "sample", allocSampleIds, enumflag.EnumCaseInsensitive)
flags.VarP(sampleEnum, "sample", "s", "Memory sample to analyse")
err := sampleEnum.RegisterCompletion(cmd, "sample", allocSampleHelp)Btw, it would be really nice to have a function to generate the description of possible values (something like As for the constraint, I don't think it makes sense to define a custom one. As I understand, your only imperative is |
|
Sorry for the silence; I've now checked with what the stdlib |
|
Ha yeah, if it does work with |
|
Thank you very much for your contribution! |
|
Thank you for maintaining it :-) |
|
released as v2.1.0; I've finally went for |
This is a POC that it's possible to relax the
constraint.Integerconstraint into simplyconstraint.Ordered. This means in particular that string enum can be accepted, which I found to be a common case.It seems to work, but I may not have covered all the cases properly.