Skip to content

Commit

Permalink
Moved example to example_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jessevdk committed Sep 2, 2012
1 parent 245026c commit 3b27d7f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
63 changes: 63 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Example of use of the flags package.
package flags_test

import (
flags "github.com/jessevdk/go-flags"
"os"
"fmt"
"strings"
"os/exec"
)

func Example() {
var opts struct {
// Slice of bool will append 'true' each time the option
// is encountered (can be set multiple times, like -vvv)
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`

// Example of automatic marshalling to desired type (uint)
Offset uint `long:"offset" description:"Offset"`

// Example of a callback, called each time the option is found.
Call func(string) `short:"c" description:"Call phone number"`
}

// Callback which will invoke callto:<argument> to call a number.
// Note that this works just on OS X (and probably only with
// Skype) but it shows the idea.
opts.Call = func(num string) {
cmd := exec.Command("open", "callto:" + num)
cmd.Start()
cmd.Process.Release()
}

// Create a new parser
parser := flags.NewParser()

// Add the standard help group to the parser
parser.AddHelp(os.Stderr)

// Add a new group for our own options. Note that we need to pass
// opts by reference (as a pointer) to allow the parser to set
// the fields values when needed.
parser.AddGroup("Application Options", &opts)

// Finally, parse the command line arguments (uses os.Args by
// default). The resulting args are the remaining unparsed command
// line arguments. err will be set if there was a problem while
// parsing the command line options. It will take the special
// value flags.ErrHelp if the standard help was shown.
args, err := parser.Parse()

if err != nil {
if err != flags.ErrHelp {
parser.PrintError(os.Stderr, err)
}

os.Exit(1)
}

fmt.Printf("Remaining args: %s\n", strings.Join(args, " "))

// Output: Remaining args:
}
62 changes: 0 additions & 62 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,6 @@
// but provides more options and uses reflection to provide a convenient and
// succinct way of specifying command line options.
//
// Example:
// package main
//
// import (
// flags "github.com/jessevdk/go-flags"
// "os"
// "fmt"
// "strings"
// "os/exec"
// )
//
// func main() {
// var opts struct {
// // Slice of bool will append 'true' each time the option
// // is encountered (can be set multiple times, like -vvv)
// Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
//
// // Example of automatic marshalling to desired type (uint)
// Offset uint `long:"offset" description:"Offset"`
//
// // Example of a callback, called each time the option is found.
// Call func(string) `short:"c" description:"Call phone number"`
// }
//
// // Callback which will invoke callto:<argument> to call a number.
// // Note that this works just on OS X (and probably only with
// // Skype) but it shows the idea.
// opts.Call = func(num string) {
// cmd := exec.Command("open", "callto:" + num)
// cmd.Start()
// cmd.Process.Release()
// }
//
// // Create a new parser
// parser := flags.NewParser()
//
// // Add the standard help group to the parser
// parser.AddHelp(os.Stderr)
//
// // Add a new group for our own options. Note that we need to pass
// // opts by reference (as a pointer) to allow the parser to set
// // the fields values when needed.
// parser.AddGroup("Application Options", &opts)
//
// // Finally, parse the command line arguments (uses os.Args by
// // default). The resulting args are the remaining unparsed command
// // line arguments. err will be set if there was a problem while
// // parsing the command line options. It will take the special
// // value flags.ErrHelp if the standard help was shown.
// args, err := parser.Parse()
//
// if err != nil {
// if err != flags.ErrHelp {
// parser.PrintError(os.Stderr, err)
// }
//
// os.Exit(1)
// }
//
// fmt.Printf("Remaining args: %s\n", strings.Join(args, " "))
// }
//
// Supported features:
// Options with short names (-v)
// Options with long names (--verbose)
Expand Down

0 comments on commit 3b27d7f

Please sign in to comment.