Created
March 2, 2015 17:02
-
-
Save ericchiang/076c62bfd221c718ed83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
) | |
func cmd1(args []string) { | |
fs := flag.NewFlagSet("cmd1", flag.ExitOnError) | |
foo := fs.String("foo", "defaultfoo", "description of foo") | |
fs.Parse(args) | |
fmt.Println("value of foo", *foo) | |
} | |
func cmd2(args []string) { | |
fs := flag.NewFlagSet("cmd2", flag.ExitOnError) | |
bar := fs.String("bar", "defaultbar", "description of bar") | |
fs.Parse(args) | |
fmt.Println("value of bar", *bar) | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println(`this is a help string | |
subcommands | |
cmd1 | |
cmd2 | |
`) | |
os.Exit(2) | |
} | |
switch os.Args[1] { | |
case "cmd1": | |
cmd1(os.Args[2:]) | |
case "cmd2": | |
cmd2(os.Args[2:]) | |
default: | |
fmt.Println("unknown sub command:", os.Args[1]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment