The problem of all code I've seen so far is that they are all demonstrate half the solution, i.e., the conversion goes one way. I know it's an easy fix, but none the less, for me it's still half solution, because I need to get and *validate *the enum as *user input* from command line, but use simple enum type *internally*.
package main import ( "fmt" "github.com/mkideal/cli" "github.com/suntong/enum" ) var ( gender enum.Enum male = gender.Iota("male") female = gender.Iota("female") theGender int = -1 ) type argT struct { cli.Helper Age int `cli:"age" usage:"your age"` Gender string `cli:"g,gender" usage:"your gender (male/female)" dft:"male"` } // Validate implements cli.Validator interface func (argv *argT) Validate(ctx *cli.Context) error { if argv.Age < 0 || argv.Age > 300 { return fmt.Errorf("age %d out of range", argv.Age) } ok := false if theGender, ok = gender.Get(argv.Gender); !ok { return fmt.Errorf("invalid gender %s", ctx.Color().Yellow(argv.Gender)) } return nil } func main() { cli.Run(new(argT), func(ctx *cli.Context) error { ctx.JSONln(ctx.Argv()) fmt.Printf("%d:%s\n", theGender, gender.String(theGender)) return nil }) } $ go run 010-validatorEnum.go {"Help":false,"Age":0,"Gender":"male"} 0:male $ go run 010-validatorEnum.go -g male {"Help":false,"Age":0,"Gender":"male"} 0:male $ go run 010-validatorEnum.go -g female {"Help":false,"Age":0,"Gender":"female"} 1:female $ go run 010-validatorEnum.go -g unknown ERR! invalid gender unknown Ref: https://github.com/suntong/lang/blob/master/lang/Go/src/sys/CLI/010-validatorEnum.go On Wednesday, April 26, 2017 at 3:58:28 PM UTC-4, freeformz wrote: > > FWIW, I wouldn't use an "enum" (those don't exist in go) and would > probably do something like this: https://play.golang.org/p/J6_hssueao > > > On Wed, Apr 26, 2017 at 11:41 AM Tong Sun <sunto...@gmail.com > <javascript:>> wrote: > >> >> >> On Tuesday, April 25, 2017 at 4:25:14 PM UTC-4, Tong Sun wrote: >>> >>> >>> On Tue, Apr 25, 2017 at 3:42 PM, Egon wrote: >>> >>> I think the extra "enum" package would reduce readability. The code you >>>> are putting into package is ~10 lines of code... so the extra package >>>> doesn't reduce much typing, but it also loses enum typing... Depending on >>>> the enum, you may want to have different properties as well... >>>> I chose the name "ciota" because of "custom-iota", in practice you >>>> would want something more descriptive such as "NewWeekday". >>> >>> >>> So the solution is easy, don't create a general purpose package for it :D >>>> >>> >>> OK. >>> >>> If going that route, and I have several enum series to define, I need >>> to define a series of almost identical functions, right? Hmm..., that'd >>> be really messy. >>> >>> Ok, I'll think of a better way on my own, which I doubt I could, :-) >>> >> >> >> Thought I need to use reflection, but after several round of wrestling, I >> managed to do without reflection at all: >> >> >> package main >> >> >> import ( >> "fmt" >> >> >> enum "github.com/suntong/enum" >> ) >> >> >> var ( >> example enum.Enum >> Alpha = example.Iota("Alpha") >> Beta = example.Iota("Beta") >> >> >> weekday enum.Enum >> Sunday = weekday.Iota("Sunday") >> Monday = weekday.Iota("Monday") >> ) >> >> >> func main() { >> fmt.Printf("%s\n", example.String(Alpha)) >> fmt.Printf("%s\n", example.String(Beta)) >> >> fmt.Println("=======") >> fmt.Printf("%d\t%d\n", Alpha, Alpha+1) >> >> fmt.Printf("%s\t%s\n", example.String(Beta-1), example.String(Alpha+1)) >> fmt.Println("=======") >> if a, ok := example.Get("Alpha"); ok { >> fmt.Printf("%d: %s\n", a, example.String(a)) >> } >> if b, ok := example.Get("Beta"); ok { >> fmt.Printf("%d: %+v\n", b, example.String(b)) >> } >> >> >> fmt.Printf("%d:%s\n", Sunday, weekday.String(Sunday)) >> fmt.Printf("%d:%s\n", Monday, weekday.String(Monday)) >> } >> >> >> The output: >> >> Alpha >> Beta >> ======= >> 0 1 >> Alpha Beta >> ======= >> 0: Alpha >> 1: Beta >> 0:Sunday >> 1:Monday >> >> Ref: >> >> https://github.com/suntong/lang/blob/master/lang/Go/src/ds/EnumsStr1p.go >> >> and >> >> https://github.com/suntong/enum >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.