forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-interactive.go
More file actions
128 lines (122 loc) · 3.15 KB
/
example-interactive.go
File metadata and controls
128 lines (122 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/urfave/cli/v3"
)
func main() {
cmd := &cli.Command{
Name: "interactive-demo",
Usage: "A demonstration of interactive mode in urfave/cli",
Description: `This example demonstrates how to use the interactive mode
to prompt users for missing parameters. Use --interactive or -i flag
to enable interactive prompting.`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Usage: "Enable interactive mode for missing parameters",
},
&cli.InteractiveStringFlag{
StringFlag: cli.StringFlag{
Name: "name",
Value: "Guest",
Usage: "Your name",
},
Prompt: "Enter your name",
Required: true,
},
&cli.InteractiveIntFlag{
Int64Flag: cli.Int64Flag{
Name: "age",
Value: 18,
Usage: "Your age",
},
Prompt: "Enter your age",
Required: true,
},
&cli.InteractiveStringFlag{
StringFlag: cli.StringFlag{
Name: "email",
Usage: "Your email address",
},
Prompt: "Enter your email (optional)",
Required: false,
},
&cli.InteractiveBoolFlag{
BoolFlag: cli.BoolFlag{
Name: "subscribe",
Value: false,
Usage: "Subscribe to newsletter",
},
Prompt: "Do you want to subscribe to our newsletter",
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("\n=== User Profile ===")
fmt.Printf("Name: %s\n", cmd.String("name"))
fmt.Printf("Age: %d\n", cmd.Int("age"))
if email := cmd.String("email"); email != "" {
fmt.Printf("Email: %s\n", email)
} else {
fmt.Println("Email: Not provided")
}
fmt.Printf("Subscribed: %v\n", cmd.Bool("subscribe"))
if cmd.Bool("subscribe") {
fmt.Println("\nThank you for subscribing!")
}
return nil
},
Commands: []*cli.Command{
{
Name: "create",
Usage: "Create a new project interactively",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Usage: "Enable interactive mode",
},
&cli.InteractiveStringFlag{
StringFlag: cli.StringFlag{
Name: "project-name",
Usage: "Name of the project",
},
Prompt: "Enter project name",
Required: true,
},
&cli.InteractiveStringFlag{
StringFlag: cli.StringFlag{
Name: "description",
Value: "A new project",
Usage: "Project description",
},
Prompt: "Enter project description",
Required: false,
},
&cli.InteractiveStringFlag{
StringFlag: cli.StringFlag{
Name: "language",
Value: "go",
Usage: "Programming language",
},
Prompt: "Enter programming language",
Required: false,
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("\n=== Project Created ===")
fmt.Printf("Project Name: %s\n", cmd.String("project-name"))
fmt.Printf("Description: %s\n", cmd.String("description"))
fmt.Printf("Language: %s\n", cmd.String("language"))
return nil
},
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}