Debian Codegraph @

shell/example_test.go

raw
1 // Copyright (c) 2018, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package shell_test
5
6 import (
7 "fmt"
8
9 "mvdan.cc/sh/v3/shell"
10 )
11
12 func ExampleExpand() {
13 env := func(name string) string {
14 switch name {
15 case "HOME":
16 return "/home/user"
17 }
18 return "" // leave the rest unset
19 }
20 out, _ := shell.Expand("No place like $HOME", env)
21 fmt.Println(out)
22
23 out, _ = shell.Expand("Some vars are ${missing:-awesome}", env)
24 fmt.Println(out)
25
26 out, _ = shell.Expand("Math is fun! $((12 * 34))", nil)
27 fmt.Println(out)
28 // Output:
29 // No place like /home/user
30 // Some vars are awesome
31 // Math is fun! 408
32 }
33
34 func ExampleFields() {
35 env := func(name string) string {
36 switch name {
37 case "foo":
38 return "bar baz"
39 }
40 return "" // leave the rest unset
41 }
42 out, _ := shell.Fields(`"many quoted" ' strings '`, env)
43 fmt.Printf("%#v\n", out)
44
45 out, _ = shell.Fields("unquoted $foo", env)
46 fmt.Printf("%#v\n", out)
47
48 out, _ = shell.Fields(`quoted "$foo"`, env)
49 fmt.Printf("%#v\n", out)
50 // Output:
51 // []string{"many quoted", " strings "}
52 // []string{"unquoted", "bar", "baz"}
53 // []string{"quoted", "bar baz"}
54 }