| 1 | # mauflag |
| 2 | [](https://travis-ci.org/tulir293/mauflag) |
| 3 | [](http://www.gnu.org/licenses/gpl-3.0.html) |
| 4 | |
| 5 | An extendable argument parser for Golang. Mostly follows the GNU [Program Argument Syntax Conventions](https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html). |
| 6 | |
| 7 | Use `go get maunium.net/go/mauflag` to get this package |
| 8 | |
| 9 | ## Basic usage |
| 10 | A flag can have as many long or short keys as you like. Short keys are prefixed with one dash (`-`) whilst long keys are prefixed with two (`--`) |
| 11 | |
| 12 | To create a flag, you must first call the `Make()` method in a flagset. Calling flagset methods as package functions uses the default flagset which takes input arguments from `os.Args` |
| 13 | |
| 14 | After creating a flag, you can call the functions it has as you like. After configuring the flag, you can call one of the type functions to set the flag type and get a pointer to the value. |
| 15 | |
| 16 | ### Examples |
| 17 | Here's a basic string flag that can be set using `-s` or `--my-string-flag` |
| 18 | ```go |
| 19 | var myStringFlag = flag.Make().ShortKey("s").LongKey("my-string-flag").Default("a string").String() |
| 20 | ``` |
| 21 | |
| 22 | The following input arguments would set the value of the flag to `foo` |
| 23 | * `-s`, `foo` |
| 24 | * `-s=foo` |
| 25 | * `-sfoo` |
| 26 | * `--my-string-flag`, `foo` |
| 27 | * `--my-string-flag=foo` |
| 28 | |
| 29 | #### Chaining |
| 30 | Short boolean flags can be chained. The last flag of a chain doesn't necessarily have to be a boolean flag. |
| 31 | For example, if we had the following flags defined |
| 32 | ```go |
| 33 | var boolA = flag.Make().ShortKey("a").Bool() |
| 34 | var boolB = flag.Make().ShortKey("b").Bool() |
| 35 | var boolC = flag.Make().ShortKey("c").Bool() |
| 36 | var stringD = flag.Make().ShortKey("d").String() |
| 37 | ``` |
| 38 | We could set the values of all the boolean flags to true with one input argument: `-abc`. We could also add the `d` flag to the end like so: `-abcd`, `foo`. |
| 39 | Any of the other ways to set short flags also work (`-abcd=foo`, `-abcdfoo`). |
| 40 | |
| 41 | However, if you put `d` as the first flag (`-dabc`) the value of the `d` flag will be `abc` and none of the boolean flags would change. |
| 42 | |
| 43 | ### Godoc |
| 44 | More docs, including all values supported by default, can be found from [godoc.org/maunium.net/go/mauflag](https://godoc.org/maunium.net/go/mauflag) |
| 45 | |
| 46 | ## Custom values |
| 47 | All value containers must implement the `Value` interface. It contains two functions: |
| 48 | * `Set(string)` which is called whenever the parser finds a value associated with the flag. |
| 49 | * `Name()` which should return a human-readable name for the type of the value. If the value container contains multiple actual values, `Name()` should return the name of a single object (e.g. `integer` if the value container contains multiple integers) |
| 50 | |
| 51 | After creating a value container you can use it by calling the `Custom` method of a `Flag` and giving an instance of the value container. The parser will then call the `Set` method each time it encounters a value associated with the flag. |