Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
add Setup function
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Sep 1, 2018
1 parent b749952 commit 3e63926
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions setup.go
@@ -0,0 +1,31 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flag

import (
"flag"
"fmt"
"os"
)

// Setup parses flag and setups usage function.
// `positionalArgs` is a string representing positional args, eg: "arg1 arg2 arg3"
// `commands` if provided is a positional argument command description.
func Setup(version, positionalArgs string, commands ...string) {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Version: ", version)
if len(commands) != 0 {
fmt.Fprintf(os.Stderr,
"USAGE: %s <commands> <parameters> %s\n\n", os.Args[0], positionalArgs)
fmt.Fprint(os.Stderr, "COMMANDS:", commands[0], "\n\n")
} else {
fmt.Fprintf(os.Stderr,
"USAGE: %s <parameters> %s\n\n", os.Args[0], positionalArgs)
}
fmt.Fprintln(os.Stderr, "PARAMETERS:")
flag.PrintDefaults()
}
flag.Parse()
}

0 comments on commit 3e63926

Please sign in to comment.