mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2024-11-29 08:22:36 +01:00
CLI: Properly parse cli flags for subcommands
Previously we parsed cli flags globally instead of per subcommand, which was weird.
This commit is contained in:
parent
54697be895
commit
8477ba5bfe
@ -39,7 +39,7 @@ Add this to .bashrc
|
|||||||
|
|
||||||
```
|
```
|
||||||
if [ -n "$PS1" ] ; then
|
if [ -n "$PS1" ] ; then
|
||||||
PROMPT_COMMAND='hs9001 -ret $? add "$(history 1)"'
|
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
By default, every system user gets his own database. You can override this by setting the environment variable
|
By default, every system user gets his own database. You can override this by setting the environment variable
|
||||||
|
43
main.go
43
main.go
@ -124,54 +124,63 @@ func printUsage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var ret int
|
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
|
||||||
flag.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
searchCmd := flag.NewFlagSet("search", flag.ExitOnError)
|
||||||
flag.Parse()
|
|
||||||
args := flag.Args()
|
|
||||||
argslen := len(args)
|
|
||||||
|
|
||||||
if argslen < 1 {
|
if len(os.Args) < 1 {
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := args[0]
|
cmd := os.Args[1]
|
||||||
|
globalargs := os.Args[2:]
|
||||||
|
|
||||||
conn := createConnection()
|
conn := createConnection()
|
||||||
|
|
||||||
if cmd == "add" {
|
switch cmd {
|
||||||
|
case "add":
|
||||||
|
var ret int
|
||||||
|
addCmd.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
||||||
|
addCmd.Parse(globalargs)
|
||||||
|
args := addCmd.Args()
|
||||||
|
|
||||||
if ret == 23 { // 23 is our secret do not log status code
|
if ret == 23 { // 23 is our secret do not log status code
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if argslen < 2 {
|
if len(args) < 1 {
|
||||||
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
||||||
|
|
||||||
}
|
}
|
||||||
historycmd := args[1]
|
historycmd := args[0]
|
||||||
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
|
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
|
||||||
rs := rgx.FindStringSubmatch(historycmd)
|
rs := rgx.FindStringSubmatch(historycmd)
|
||||||
if len(rs) == 2 {
|
if len(rs) == 2 {
|
||||||
add(conn, rs[1])
|
add(conn, rs[1])
|
||||||
}
|
}
|
||||||
} else if cmd == "search" {
|
case "search":
|
||||||
if argslen < 2 {
|
searchCmd.Parse(globalargs)
|
||||||
|
args := searchCmd.Args()
|
||||||
|
|
||||||
|
if len(args) < 1 {
|
||||||
fmt.Fprint(os.Stderr, "Please provide the search query\n")
|
fmt.Fprint(os.Stderr, "Please provide the search query\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
q := strings.Join(args[1:], " ")
|
|
||||||
|
q := strings.Join(args, " ")
|
||||||
search(conn, q)
|
search(conn, q)
|
||||||
os.Exit(23)
|
os.Exit(23)
|
||||||
} else if cmd == "init" {
|
case "init":
|
||||||
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
|
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
initDatabase(conn)
|
initDatabase(conn)
|
||||||
} else if cmd == "import" {
|
case "import":
|
||||||
importFromStdin(conn)
|
importFromStdin(conn)
|
||||||
} else {
|
default:
|
||||||
fmt.Fprint(os.Stderr, "Error: Unknown command supplied\n\n")
|
fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd)
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user