Fix for help when $HOME is /

estesp noticed that when $HOME is / the ~ substitutions messes up
becuase it tries to replace all paths that start with "/" with "~".
This fixes it so that it will only replace it when $HOME isn't "/".

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-02-06 14:17:34 -08:00
parent de0ed79618
commit 9ac0da4578
2 changed files with 21 additions and 5 deletions

View file

@ -5,14 +5,20 @@ import (
"runtime" "runtime"
) )
// Key returns the env var name for the user's home dir based on
// the platform being run on
func Key() string {
if runtime.GOOS == "windows" {
return "USERPROFILE"
}
return "HOME"
}
// Get returns the home directory of the current user with the help of // Get returns the home directory of the current user with the help of
// environment variables depending on the target operating system. // environment variables depending on the target operating system.
// Returned path should be used with "path/filepath" to form new paths. // Returned path should be used with "path/filepath" to form new paths.
func Get() string { func Get() string {
if runtime.GOOS == "windows" { return os.Getenv(Key())
return os.Getenv("USERPROFILE")
}
return os.Getenv("HOME")
} }
// GetShortcutString returns the string that is shortcut to user's home directory // GetShortcutString returns the string that is shortcut to user's home directory

View file

@ -86,6 +86,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -505,7 +506,16 @@ func Set(name, value string) error {
// otherwise, the default values of all defined flags in the set. // otherwise, the default values of all defined flags in the set.
func (f *FlagSet) PrintDefaults() { func (f *FlagSet) PrintDefaults() {
writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0) writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
home := homedir.Get() var home string
if runtime.GOOS != "windows" {
// Only do this on non-windows systems
home = homedir.Get()
// Don't substitute when HOME is /
if home == "/" {
home = ""
}
}
f.VisitAll(func(flag *Flag) { f.VisitAll(func(flag *Flag) {
format := " -%s=%s" format := " -%s=%s"
names := []string{} names := []string{}