Skip to content

Commit

Permalink
Add the ability to retrieve the set's program and parameters.
Browse files Browse the repository at this point in the history
Modify UsageLine to not include the program name (fetch it with set.Program()).oImprove comments.
  • Loading branch information
pborman committed Jul 29, 2018
1 parent 754a1a6 commit 566dc99
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
20 changes: 12 additions & 8 deletions v2/getopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,21 @@ var HelpColumn = 20

// PrintUsage prints the usage line and set of options of set S to w.
func (s *Set) PrintUsage(w io.Writer) {
var params string
parts := make([]string, 2, 4)
parts[0] = "Usage:"
parts[1] = s.program
if usage := s.UsageLine(); usage != "" {
parts = append(parts, usage)
}
if s.parameters != "" {
params = " " + s.parameters
parts = append(parts, s.parameters)
}
fmt.Fprintf(w, "Usage: %s%s\n", s.UsageLine(), params)
fmt.Fprintln(w, strings.Join(parts, " "))
s.PrintOptions(w)

}

// UsageLine returns the usage line for the set s. The set's parameters,
// if any, are not included.
// UsageLine returns the usage line for the set s. The set's program name and
// parameters, if any, are not included.
func (s *Set) UsageLine() string {
sort.Sort(s.options)
flags := ""
Expand Down Expand Up @@ -286,9 +290,9 @@ func (s *Set) UsageLine() string {
}
flags = strings.Join(opts, "] [")
if flags != "" {
flags = " [" + flags + "]"
flags = "[" + flags + "]"
}
return s.program + flags
return flags
}

// PrintOptions prints the list of options in s to w.
Expand Down
9 changes: 5 additions & 4 deletions v2/options/options.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Package options is under development and may be moved in the future.
// 26 July 2018
//
// Package options provides a structured interface to the getopt package. It is
// particularly helpful for parsing an option set more than once and possibly
// concurrently. This package was designed to make option specification simpler
// and more concise.
// Package options provides a structured interface for getopt style flag
// parsing. It is particularly helpful for parsing an option set more than once
// and possibly concurrently. This package was designed to make option
// specification simpler and more concise. It is a wrapper around the getopt
// package github.com/pborman/getopt/v2 package.
//
// Option Decorations
//
Expand Down
3 changes: 2 additions & 1 deletion v2/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var myOptions = theOptions{
// This is the help we expect from theOptions. If you change theOptions then
// you must change this string. Note that getopt.HelpColumn must be set to 25.
var theHelp = `
Usage: [-v] [-c COUNT] [--lazy value] [-n NUMBER] [--name NAME] [--timeout value] [parameters ...]
Usage: program [-v] [-c COUNT] [--lazy value] [-n NUMBER] [--name NAME] [--timeout value] [parameters ...]
-c, --count=COUNT number of widgets
--lazy=value unspecified
-n NUMBER set n to NUMBER
Expand Down Expand Up @@ -56,6 +56,7 @@ func TestHelp(t *testing.T) {
t.Errorf("RegisterNew returned type %T, want %T", dopts, opts)
}
var buf bytes.Buffer
s.SetProgram("program")
s.PrintUsage(&buf)
if help := buf.String(); help != theHelp {
t.Errorf("Got help:\n%s\nWant:\n%s", help, theHelp)
Expand Down
7 changes: 7 additions & 0 deletions v2/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (s *Set) SetParameters(parameters string) {
s.parameters = parameters
}

// Parameters returns the parameters set by SetParameters on s.
func (s *Set) Parameters() string { return s.parameters }


// SetProgram sets the program name to program. Normally it is determined
// from the zeroth command line argument (see os.Args).
func SetProgram(program string) {
Expand All @@ -156,6 +160,9 @@ func (s *Set) SetProgram(program string) {
s.program = program
}

// Program returns the program name associated with Set s.
func (s *Set) Program() string { return s.program }

// SetUsage sets the function used by Parse to display the commands usage
// on error. It defaults to calling PrintUsage(os.Stderr).
func SetUsage(usage func()) {
Expand Down

0 comments on commit 566dc99

Please sign in to comment.