Fix command line arg order

This commit is contained in:
Marc Di Luzio 2020-06-09 18:32:44 +01:00
parent 4558e8a9b0
commit 339163e06d

View file

@ -16,17 +16,16 @@ import (
var USAGE = "" var USAGE = ""
// Command usage // Command usage
// TODO: Allow COMMAND to be used first
func Usage() { func Usage() {
fmt.Printf("Usage: %s [OPTIONS]... COMMAND\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage: %s COMMAND [OPTIONS]...\n", os.Args[0])
fmt.Println("\nCommands:") fmt.Fprintln(os.Stderr, "\nCommands:")
fmt.Println("\tstatus \tprints the server status") fmt.Fprintln(os.Stderr, "\tstatus \tprints the server status")
fmt.Println("\tregister\tregisters an account and stores it (use with -name)") fmt.Fprintln(os.Stderr, "\tregister\tregisters an account and stores it (use with -name)")
fmt.Println("\tspawn \tspawns a rover for the current account") fmt.Fprintln(os.Stderr, "\tspawn \tspawns a rover for the current account")
fmt.Println("\tmove \tissues move command to rover") fmt.Fprintln(os.Stderr, "\tmove \tissues move command to rover")
fmt.Println("\tradar \tgathers radar data for the current rover") fmt.Fprintln(os.Stderr, "\tradar \tgathers radar data for the current rover")
fmt.Println("\trover \tgets data for current rover") fmt.Fprintln(os.Stderr, "\trover \tgets data for current rover")
fmt.Println("\nOptions:") fmt.Fprintln(os.Stderr, "\nOptions:")
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -191,7 +190,9 @@ func InnerMain(command string) error {
} }
default: default:
return fmt.Errorf("Unknown command: %s", command) // Print the usage
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
Usage()
} }
// Save out the persistent file // Save out the persistent file
@ -209,7 +210,7 @@ func InnerMain(command string) error {
// Simple main // Simple main
func main() { func main() {
flag.Usage = Usage flag.Usage = Usage
flag.Parse() flag.CommandLine.Parse(os.Args[2:])
// Print the version if requested // Print the version if requested
if *ver { if *ver {
@ -217,15 +218,8 @@ func main() {
return return
} }
// Verify we have a single command line arg
args := flag.Args()
if len(args) != 1 {
Usage()
os.Exit(1)
}
// Run the inner main // Run the inner main
if err := InnerMain(args[0]); err != nil { if err := InnerMain(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err) fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1) os.Exit(1)
} }