Skip to content

Commit b775798

Browse files
committed
Improve example
1 parent bdae545 commit b775798

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

examples/Open/main.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import (
1717
"fmt"
1818
"log"
1919
"os"
20+
"path/filepath"
21+
"strings"
2022

2123
"github.com/cli/browser"
2224
)
2325

2426
func usage() {
25-
fmt.Fprintf(os.Stderr, "Usage:\n %s [file]\n", os.Args[0])
27+
fmt.Fprintf(os.Stderr, "Usage:\n %s {<url> | <file> | -}\n", filepath.Base(os.Args[0]))
2628
flag.PrintDefaults()
2729
}
2830

@@ -39,12 +41,21 @@ func check(err error) {
3941

4042
func main() {
4143
args := flag.Args()
42-
switch len(args) {
43-
case 0:
44-
check(browser.OpenReader(os.Stdin))
45-
case 1:
46-
check(browser.OpenFile(args[0]))
47-
default:
44+
45+
if len(args) != 1 {
4846
usage()
47+
os.Exit(1)
48+
}
49+
50+
if args[0] == "-" {
51+
check(browser.OpenReader(os.Stdin))
52+
return
53+
}
54+
55+
if strings.HasPrefix(args[0], "http:") || strings.HasPrefix(args[0], "https:") {
56+
check(browser.OpenURL(args[0]))
57+
return
4958
}
59+
60+
check(browser.OpenFile(args[0]))
5061
}

0 commit comments

Comments
 (0)