-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move command line flags into it's own module
- Loading branch information
Chris Cummer
committed
Jun 16, 2018
1 parent
bbef2bf
commit 4a00c11
Showing
4 changed files
with
62 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
wtf: | ||
colors: | ||
background: gray | ||
#background: gray | ||
border: | ||
focusable: darkslateblue | ||
focused: orange | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
goFlags "github.com/jessevdk/go-flags" | ||
"github.com/senorprogrammer/wtf/wtf" | ||
) | ||
|
||
type Flags struct { | ||
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"` | ||
Module string `short:"m" long:"module" optional:"yes" description:"Display info about a specific module, i.e.: 'wtf -m=todo'"` | ||
Version bool `short:"v" long:"version" description:"Show Version Info"` | ||
} | ||
|
||
func NewFlags() *Flags { | ||
flags := Flags{} | ||
return &flags | ||
} | ||
|
||
/* -------------------- Exported Functions -------------------- */ | ||
|
||
func (flags *Flags) HasConfig() bool { | ||
return len(flags.Config) > 0 | ||
} | ||
|
||
func (flags *Flags) HasModule() bool { | ||
return len(flags.Module) > 0 | ||
} | ||
|
||
func (flags *Flags) Parse(version string) { | ||
parser := goFlags.NewParser(flags, goFlags.Default) | ||
if _, err := parser.Parse(); err != nil { | ||
if flagsErr, ok := err.(*goFlags.Error); ok && flagsErr.Type == goFlags.ErrHelp { | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
if !flags.HasConfig() { | ||
homeDir, err := wtf.Home() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
flags.Config = filepath.Join(homeDir, ".wtf", "config.yml") | ||
} | ||
|
||
if flags.Version { | ||
fmt.Printf("Version: %s\n", version) | ||
os.Exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.