Skip to content

Commit d7cbfd4

Browse files
committed
docs: improve examples
1 parent 612754f commit d7cbfd4

File tree

21 files changed

+135
-98
lines changed

21 files changed

+135
-98
lines changed

README.md

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ fmt.Println(out)
566566
567567
### <a id="creationflags"></a>CreationFlags
568568
569-
CreationFlags is a no-op on non-Windows platforms.
569+
CreationFlags sets Windows creation flags.
570570
571571
_Example: creation flags_
572572
@@ -584,7 +584,7 @@ fmt.Println(execx.Command("go", "env", "GOOS").CreationFlags(0) != nil)
584584
585585
### <a id="hidewindow"></a>HideWindow
586586
587-
HideWindow is a no-op on non-Windows platforms.
587+
HideWindow controls window visibility and sets CREATE_NO_WINDOW for console apps.
588588
589589
_Example: hide window_
590590
@@ -602,7 +602,7 @@ fmt.Println(execx.Command("go", "env", "GOOS").HideWindow(true) != nil)
602602
603603
### <a id="pdeathsig"></a>Pdeathsig
604604
605-
Pdeathsig is a no-op on non-Linux Unix platforms.
605+
Pdeathsig is a no-op on Windows.
606606
607607
_Example: pdeathsig_
608608
@@ -627,7 +627,7 @@ fmt.Println(execx.Command("go", "env", "GOOS").Pdeathsig(0) != nil)
627627
628628
### <a id="setpgid"></a>Setpgid
629629
630-
Setpgid sets the process group ID behavior.
630+
Setpgid is a no-op on Windows.
631631
632632
_Example: setpgid_
633633
@@ -652,7 +652,7 @@ fmt.Println(execx.Command("go", "env", "GOOS").Setpgid(true) != nil)
652652
653653
### <a id="setsid"></a>Setsid
654654
655-
Setsid sets the session ID behavior.
655+
Setsid is a no-op on Windows.
656656
657657
_Example: setsid_
658658
@@ -734,8 +734,7 @@ fmt.Println(err == nil && len(results) == 2)
734734
GracefulShutdown sends a signal and escalates to kill after the timeout.
735735
736736
```go
737-
proc := execx.Command("sleep", "2").
738-
Start()
737+
proc := execx.Command("sleep", "2").Start()
739738
_ = proc.GracefulShutdown(os.Interrupt, 100*time.Millisecond)
740739
res, err := proc.Wait()
741740
fmt.Println(err != nil || res.ExitCode != 0)
@@ -747,8 +746,7 @@ fmt.Println(err != nil || res.ExitCode != 0)
747746
Interrupt sends an interrupt signal to the process.
748747
749748
```go
750-
proc := execx.Command("sleep", "2").
751-
Start()
749+
proc := execx.Command("sleep", "2").Start()
752750
_ = proc.Interrupt()
753751
res, err := proc.Wait()
754752
fmt.Println(err != nil || res.ExitCode != 0)
@@ -760,8 +758,7 @@ fmt.Println(err != nil || res.ExitCode != 0)
760758
KillAfter terminates the process after the given duration.
761759
762760
```go
763-
proc := execx.Command("sleep", "2").
764-
Start()
761+
proc := execx.Command("sleep", "2").Start()
765762
proc.KillAfter(100 * time.Millisecond)
766763
res, err := proc.Wait()
767764
fmt.Println(err != nil || res.ExitCode != 0)
@@ -773,8 +770,7 @@ fmt.Println(err != nil || res.ExitCode != 0)
773770
Send sends a signal to the process.
774771
775772
```go
776-
proc := execx.Command("sleep", "2").
777-
Start()
773+
proc := execx.Command("sleep", "2").Start()
778774
_ = proc.Send(os.Interrupt)
779775
res, err := proc.Wait()
780776
fmt.Println(err != nil || res.ExitCode != 0)
@@ -786,8 +782,7 @@ fmt.Println(err != nil || res.ExitCode != 0)
786782
Terminate kills the process immediately.
787783
788784
```go
789-
proc := execx.Command("sleep", "2").
790-
Start()
785+
proc := execx.Command("sleep", "2").Start()
791786
_ = proc.Terminate()
792787
res, err := proc.Wait()
793788
fmt.Println(err != nil || res.ExitCode != 0)
@@ -800,8 +795,8 @@ Wait waits for the command to complete and returns the result and any error.
800795
801796
```go
802797
proc := execx.Command("go", "env", "GOOS").Start()
803-
res, err := proc.Wait()
804-
fmt.Println(err == nil && res.ExitCode == 0)
798+
res, _ := proc.Wait()
799+
fmt.Println(res.ExitCode == 0)
805800
// #bool true
806801
```
807802
@@ -812,8 +807,8 @@ fmt.Println(err == nil && res.ExitCode == 0)
812807
IsExitCode reports whether the exit code matches.
813808
814809
```go
815-
res, err := execx.Command("go", "env", "GOOS").Run()
816-
fmt.Println(err == nil && res.IsExitCode(0))
810+
res, _ := execx.Command("go", "env", "GOOS").Run()
811+
fmt.Println(res.IsExitCode(0))
817812
// #bool true
818813
```
819814
@@ -822,18 +817,18 @@ fmt.Println(err == nil && res.IsExitCode(0))
822817
IsSignal reports whether the command terminated due to a signal.
823818
824819
```go
825-
res, err := execx.Command("go", "env", "GOOS").Run()
826-
fmt.Println(err == nil && res.IsSignal(os.Interrupt))
827-
// #bool false
820+
res, _ := execx.Command("go", "env", "GOOS").Run()
821+
fmt.Println(res.IsSignal(os.Interrupt))
822+
// false
828823
```
829824
830825
### <a id="ok"></a>OK
831826
832827
OK reports whether the command exited cleanly without errors.
833828
834829
```go
835-
res, err := execx.Command("go", "env", "GOOS").Run()
836-
fmt.Println(err == nil && res.OK())
830+
res, _ := execx.Command("go", "env", "GOOS").Run()
831+
fmt.Println(res.OK())
837832
// #bool true
838833
```
839834
@@ -846,23 +841,27 @@ OnStderr registers a line callback for stderr.
846841
```go
847842
var lines []string
848843
_, err := execx.Command("go", "env", "-badflag").
849-
OnStderr(func(line string) { lines = append(lines, line) }).
844+
OnStderr(func(line string) {
845+
lines = append(lines, line)
846+
fmt.Println(line)
847+
}).
850848
Run()
851-
fmt.Println(err == nil && len(lines) == 1)
852-
// #bool true
849+
fmt.Println(err == nil)
850+
// flag provided but not defined: -badflag
851+
// usage: go env [-json] [-changed] [-u] [-w] [var ...]
852+
// Run 'go help env' for details.
853+
// false
853854
```
854855
855856
### <a id="onstdout"></a>OnStdout
856857
857858
OnStdout registers a line callback for stdout.
858859
859860
```go
860-
var lines []string
861-
_, err := execx.Command("go", "env", "GOOS").
862-
OnStdout(func(line string) { lines = append(lines, line) }).
861+
_, _ = execx.Command("go", "env", "GOOS").
862+
OnStdout(func(line string) { fmt.Println(line) }).
863863
Run()
864-
fmt.Println(err == nil && len(lines) > 0)
865-
// #bool true
864+
// darwin
866865
```
867866
868867
### <a id="stderrwriter"></a>StderrWriter
@@ -874,8 +873,12 @@ var out strings.Builder
874873
_, err := execx.Command("go", "env", "-badflag").
875874
StderrWriter(&out).
876875
Run()
877-
fmt.Println(err == nil && out.Len() > 0)
878-
// #bool true
876+
fmt.Print(out.String())
877+
fmt.Println(err == nil)
878+
// flag provided but not defined: -badflag
879+
// usage: go env [-json] [-changed] [-u] [-w] [var ...]
880+
// Run 'go help env' for details.
881+
// false
879882
```
880883
881884
### <a id="stdoutwriter"></a>StdoutWriter

examples/creationflags/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func main() {
12-
// CreationFlags sets Windows creation flags.
12+
// CreationFlags is a no-op on non-Windows platforms.
1313

1414
// Example: creation flags
1515
fmt.Println(execx.Command("go", "env", "GOOS").CreationFlags(0) != nil)

examples/gracefulshutdown/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ func main() {
1414
// GracefulShutdown sends a signal and escalates to kill after the timeout.
1515

1616
// Example: graceful shutdown
17-
proc := execx.Command("sleep", "2").
18-
Start()
17+
proc := execx.Command("sleep", "2").Start()
1918
_ = proc.GracefulShutdown(os.Interrupt, 100*time.Millisecond)
2019
res, err := proc.Wait()
2120
fmt.Println(err != nil || res.ExitCode != 0)

examples/hidewindow/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func main() {
12-
// HideWindow controls window visibility.
12+
// HideWindow is a no-op on non-Windows platforms.
1313

1414
// Example: hide window
1515
fmt.Println(execx.Command("go", "env", "GOOS").HideWindow(true) != nil)

examples/interrupt/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ func main() {
1212
// Interrupt sends an interrupt signal to the process.
1313

1414
// Example: interrupt
15-
proc := execx.Command("sleep", "2").
16-
Start()
15+
proc := execx.Command("sleep", "2").Start()
1716
_ = proc.Interrupt()
1817
res, err := proc.Wait()
1918
fmt.Println(err != nil || res.ExitCode != 0)

examples/isexitcode/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
// IsExitCode reports whether the exit code matches.
1313

1414
// Example: exit code
15-
res, err := execx.Command("go", "env", "GOOS").Run()
16-
fmt.Println(err == nil && res.IsExitCode(0))
15+
res, _ := execx.Command("go", "env", "GOOS").Run()
16+
fmt.Println(res.IsExitCode(0))
1717
// #bool true
1818
}

examples/issignal/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
// IsSignal reports whether the command terminated due to a signal.
1414

1515
// Example: signal
16-
res, err := execx.Command("go", "env", "GOOS").Run()
17-
fmt.Println(err == nil && res.IsSignal(os.Interrupt))
18-
// #bool false
16+
res, _ := execx.Command("go", "env", "GOOS").Run()
17+
fmt.Println(res.IsSignal(os.Interrupt))
18+
// false
1919
}

examples/killafter/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ func main() {
1313
// KillAfter terminates the process after the given duration.
1414

1515
// Example: kill after
16-
proc := execx.Command("sleep", "2").
17-
Start()
16+
proc := execx.Command("sleep", "2").Start()
1817
proc.KillAfter(100 * time.Millisecond)
1918
res, err := proc.Wait()
2019
fmt.Println(err != nil || res.ExitCode != 0)

examples/ok/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
// OK reports whether the command exited cleanly without errors.
1313

1414
// Example: ok
15-
res, err := execx.Command("go", "env", "GOOS").Run()
16-
fmt.Println(err == nil && res.OK())
15+
res, _ := execx.Command("go", "env", "GOOS").Run()
16+
fmt.Println(res.OK())
1717
// #bool true
1818
}

examples/onstderr/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ func main() {
1414
// Example: stderr lines
1515
var lines []string
1616
_, err := execx.Command("go", "env", "-badflag").
17-
OnStderr(func(line string) { lines = append(lines, line) }).
17+
OnStderr(func(line string) {
18+
lines = append(lines, line)
19+
fmt.Println(line)
20+
}).
1821
Run()
19-
fmt.Println(err == nil && len(lines) == 1)
20-
// #bool true
22+
fmt.Println(err == nil)
23+
// flag provided but not defined: -badflag
24+
// usage: go env [-json] [-changed] [-u] [-w] [var ...]
25+
// Run 'go help env' for details.
26+
// false
2127
}

0 commit comments

Comments
 (0)