enhanced_help_demo/
enhanced_help_demo.rs

1//! Demonstrates enhanced help formatting
2//!
3//! This example shows the improved help output with:
4//! - Command aliases display
5//! - Usage examples
6//! - Better spacing and organization
7
8use flag_rs::{CommandBuilder, Flag, FlagType};
9
10fn main() {
11    let app = CommandBuilder::new("kubectl")
12        .short("Kubernetes command-line tool")
13        .long("kubectl controls the Kubernetes cluster manager.\n\n\
14              Find more information at: https://kubernetes.io/docs/reference/kubectl/")
15        .example("kubectl get pods")
16        .example("kubectl apply -f deployment.yaml")
17        .example("kubectl logs -f my-pod")
18        .flag(
19            Flag::new("namespace")
20                .short('n')
21                .usage("The namespace scope for this CLI request")
22                .value_type(FlagType::String)
23                .default(flag_rs::FlagValue::String("default".to_string())),
24        )
25        .flag(
26            Flag::new("kubeconfig")
27                .usage("Path to the kubeconfig file to use for CLI requests")
28                .value_type(FlagType::String),
29        )
30        .subcommand(
31            CommandBuilder::new("get")
32                .short("Display one or many resources")
33                .group_id("Basic Commands")
34                .long("Prints a table of the most important information about the specified resources.\n\n\
35                      You can filter the list using a label selector and the --selector flag. If the\n\
36                      desired resource type is namespaced you will only see results in your current\n\
37                      namespace unless you pass --all-namespaces.")
38                .example("kubectl get pods")
39                .example("kubectl get pods -n kube-system")
40                .example("kubectl get pods --selector=app=nginx")
41                .example("kubectl get pods,services")
42                .build(),
43        )
44        .subcommand(
45            CommandBuilder::new("apply")
46                .short("Apply a configuration to a resource by file name or stdin")
47                .group_id("Basic Commands")
48                .long("Apply a configuration to a resource by file name or stdin. The resource name must\n\
49                      be specified. This resource will be created if it doesn't exist yet. To use 'apply',\n\
50                      always create the resource initially with either 'apply' or 'create --save-config'.")
51                .example("kubectl apply -f ./pod.yaml")
52                .example("kubectl apply -f https://example.com/manifest.yaml")
53                .example("kubectl apply -k ./")
54                .flag(
55                    Flag::new("filename")
56                        .short('f')
57                        .usage("Filename, directory, or URL to files to use to create the resource")
58                        .value_type(FlagType::String)
59                        .required(),
60                )
61                .flag(
62                    Flag::new("recursive")
63                        .short('R')
64                        .usage("Process the directory used in -f, --filename recursively")
65                        .value_type(FlagType::Bool),
66                )
67                .build(),
68        )
69        .subcommand(
70            CommandBuilder::new("delete")
71                .short("Delete resources by file names, stdin, resources and names, or by resources and label selector")
72                .aliases(vec!["del", "remove", "rm"])
73                .group_id("Basic Commands")
74                .example("kubectl delete pod my-pod")
75                .example("kubectl delete -f ./pod.yaml")
76                .example("kubectl delete pods --all")
77                .build(),
78        )
79        .subcommand(
80            CommandBuilder::new("logs")
81                .short("Print the logs for a container in a pod")
82                .group_id("Troubleshooting and Debugging Commands")
83                .long("Print the logs for a container in a pod or specified resource. If the pod has\n\
84                      only one container, the container name is optional.")
85                .aliases(vec!["log"])
86                .example("kubectl logs my-pod")
87                .example("kubectl logs my-pod -c my-container")
88                .example("kubectl logs -f my-pod")
89                .example("kubectl logs --tail=20 my-pod")
90                .flag(
91                    Flag::new("follow")
92                        .short('f')
93                        .usage("Specify if the logs should be streamed")
94                        .value_type(FlagType::Bool),
95                )
96                .flag(
97                    Flag::new("tail")
98                        .usage("Lines of recent log file to display")
99                        .value_type(FlagType::Int)
100                        .default(flag_rs::FlagValue::Int(-1)),
101                )
102                .build(),
103        )
104        .subcommand(
105            CommandBuilder::new("describe")
106                .short("Show details of a specific resource or group of resources")
107                .group_id("Troubleshooting and Debugging Commands")
108                .example("kubectl describe pod my-pod")
109                .example("kubectl describe nodes")
110                .build(),
111        )
112        .subcommand(
113            CommandBuilder::new("exec")
114                .short("Execute a command in a container")
115                .group_id("Troubleshooting and Debugging Commands")
116                .example("kubectl exec -it my-pod -- /bin/bash")
117                .example("kubectl exec my-pod -- ls /app")
118                .build(),
119        )
120        .subcommand(
121            CommandBuilder::new("config")
122                .short("Modify kubeconfig files")
123                .group_id("Settings Commands")
124                .example("kubectl config view")
125                .example("kubectl config use-context my-context")
126                .build(),
127        )
128        .subcommand(
129            CommandBuilder::new("version")
130                .short("Print the client and server version information")
131                .example("kubectl version")
132                .example("kubectl version --short")
133                .build(),
134        )
135        .build();
136
137    let args: Vec<String> = std::env::args().skip(1).collect();
138    if let Err(e) = app.execute(args) {
139        eprintln!("{}", e);
140        std::process::exit(1);
141    }
142}