Giter Club home page Giter Club logo

mflag's Introduction

mFlag

Support for map and slice flags (containing 'string's and 'int's).

Map Usage

map_example.go:

graphOfFriends := lFlag.NewMapStringString() 

flag.Var(&graphOfFriends, "m", "a map containing a directed graph of who considers who a friend")
flag.Parse()

graphOfFriends["some1else"] = "some2else"

fmt.Println(graphOfFriends) // graphOfFriends.String()

shell:

go run map_example.go -m person1:person2 -m person2:person3 

{"person1":"person2","person2":"person3","some1else":"some2else"}

Slice Usage

slice_example.go:

list := lFlag.NewSliceInt()

flag.Var(&list, "l", "a list of numbers")
flag.Parse()

l = append(l, 99)

fmt.Println(list) // list.String()

shell:

go run slice_example.go -l 0 -l 1 -l 2

[0,1,2,99]

'New' Functions

Each 'New' function, such as NewMapStringString or NewSliceInt, returns a specific type; map[string]string and []int in this case. A generic approach of using any map[Type]Type or []Type isn't used since the map and slice datatypes cannot be extended directly. Therefore, to extend maps, one approach would be to extend specific types, such as map[string]string, or extend a map[interface{}]interface{}. The difficulty with the latter is that another argument for the type of map would have to be supplied on the command line, this flag would then be parsed before parsing the map/slice flags, and then the map[interface{}]interface{} or []interface{} reference supplied to flag.Var would then need to be cast by the user to the type input on the command line.

Therefore, the former approach of defining a 'New' function for each possible type is taken. The type combinations are as follows:

  • map[string]string - NewMapStringString()
  • map[string]int - NewMapStringInt()
  • map[int]string - NewMapIntString()
  • map[int]int - NewMapIntInt()
  • []string - NewSliceString()
  • []int - NewSliceInt()

Encoded Semicolons

Escaped semicolons will be ignored and the leading backslash removed when used in a map type! (but not with slices)

For example: -m aPunctuationMark:\\: --> {"aPunctuationMark":":"}

mflag's People

Contributors

ctmcdo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.