gitlab.com/go-turbo/command@v0.0.0-20181230142125-c7fabe092ebf/package_test.go (about) 1 // Copyright (c) 2018-2019 The Go-Turbo Project 2 // Copyright (c) 2016-2017 Mathias J. Hennig 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // 7 // 1. Redistributions of source code must retain the above copyright notice, 8 // this list of conditions and the following disclaimer. 9 // 10 // 2. Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // 14 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND ANY 15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 // DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY 18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 25 package command 26 27 // Standard packages are documented at https://golang.org/pkg/. 28 import ( 29 "flag" 30 "fmt" 31 "os" 32 ) 33 34 // ExampleWriteOptions demonstrates how to use function WriteOptions. 35 func ExampleWriteOptions() { 36 37 flags := &flag.FlagSet{} 38 flags.Bool("debug", false, "Enable debug mode.") 39 flags.Bool("v", false, "Enable verbose output.") 40 41 // Note that Flag instances (https://golang.org/pkg/flag/#Flag) with 42 // a DefValue of "true" are suffixed with "=false" in the output! 43 flags.Bool("log", true, "Disable logging.") 44 45 // https://golang.org/pkg/os/#Chdir 46 helpChdir := "Switch to a different working directory before execution." 47 flags.Var(Option(os.Chdir), "C", helpChdir) 48 49 WriteOptions(os.Stdout, flags) 50 // Output: 51 // Options: 52 // -C ... 53 // Switch to a different working directory before execution. 54 // --debug 55 // Enable debug mode. 56 // --log=false 57 // Disable logging. 58 // -v 59 // Enable verbose output. 60 // --help, -h 61 // Print this message and exit gracefully. 62 // 63 } 64 65 // ExampleWriteUsage demonstrates how to use function WriteUsage. 66 func ExampleWriteUsage() { 67 68 WriteUsage(os.Stdout, "example", "[-h] command ...", "--help") 69 // Output: 70 // Usage: example [-h] command ... 71 // example --help 72 // 73 } 74 75 // ExampleOption demonstrates how to use type Option. 76 func ExampleOption() { 77 78 handler := func(value string) error { 79 _, err := fmt.Println(value) 80 return err 81 } 82 83 flags := &flag.FlagSet{} 84 flags.Var(Option(os.Chdir), "chdir", "Switch working directory.") 85 flags.Var(Option(handler), "print", "Print the given option.") 86 87 flags.Parse([]string{"--print", "Hello World!"}) 88 // Output: 89 // Hello World! 90 }