From d4c2b35b2ef5b67c3ec6f904cea0dff806d51e2c Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Fri, 3 Mar 2017 19:12:17 +0700 Subject: Adding separate tag option As outlined in #49, there is a need to mimic the behavior of other applications by interweaving positional and non-positional parameters. This change adds the 'separate' option that will force a arg of type []string to only read the next supplied value. For example, when dealing with the following arg type: var MyArgs struct { Pos []string `arg:"positional"` Separate []string `arg:"-s,separate"` } This commit will parse the following command line: ./app pos1 pos2 -s=separate1 -s=separate2 pos3 -s=separate3 pos4 Such that MyArgs.Pos will be [pos1 pos2 pos3 pos4] and MyArgs.Separate will be [separate1 separate2 separate3]. Unit tests for the above have also been written and are included in this commit, as well as the addition of a section to README.md and an example func in example_test.go. Fixes #49 --- example_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'example_test.go') diff --git a/example_test.go b/example_test.go index 6fb5197..c34effa 100644 --- a/example_test.go +++ b/example_test.go @@ -71,6 +71,21 @@ func Example_multipleValues() { fmt.Printf("Fetching the following IDs from %s: %q", args.Database, args.IDs) } +// This eample demonstrates multiple value arguments that can be mixed with +// other arguments. +func Example_multipleMixed() { + os.Args = []string{"./example", "-c", "cmd1", "db1", "-f", "file1", "db2", "-c", "cmd2", "-f", "file2", "-f", "file3", "db3", "-c", "cmd3"} + var args struct { + Commands []string `arg:"-c,separate"` + Files []string `arg:"-f,separate"` + Databases []string `arg:"positional"` + } + MustParse(&args) + fmt.Println("Commands:", args.Commands) + fmt.Println("Files", args.Files) + fmt.Println("Databases", args.Databases) +} + // This example shows the usage string generated by go-arg func Example_usageString() { // These are the args you would pass in on the command line -- cgit v1.2.3