@@ -31,6 +31,7 @@ import (
3131 "time"
3232
3333 "github.com/fatih/color"
34+ cc "github.com/ivanpirog/coloredcobra"
3435 "github.com/pingcap/errors"
3536 "github.com/pingcap/tiup/components/playground-ng/proc"
3637 "github.com/pingcap/tiup/pkg/environment"
@@ -121,24 +122,33 @@ func execute(state *cliState) error {
121122 state = newCLIState ()
122123 }
123124
125+ arg0 := playgroundCLIArg0 ()
126+
124127 rootCmd := & cobra.Command {
125128 Use : fmt .Sprintf ("%s [version]" , filepath .Base (os .Args [0 ])),
126- Long : `Bootstrap a TiDB cluster in your local host, the latest release version will be chosen
127- if you don't specified a version.
128-
129- Examples:
130- $ tiup playground-ng nightly # Start a TiDB nightly version local cluster
131- $ tiup playground-ng v5.0.1 --db 3 --pd 3 --kv 3 # Start a local cluster with 10 nodes
132- $ tiup playground-ng nightly --without-monitor # Start a local cluster and disable monitor system
133- $ tiup playground-ng --pd.config ~/config/pd.toml # Start a local cluster with specified configuration file
134- $ tiup playground-ng --db.binpath /xx/tidb-server # Start a local cluster with component binary path
135- $ tiup playground-ng --tag xx # Start a local cluster with data dir named 'xx' and uncleaned after exit
136- $ tiup playground-ng -d --tag xx # Start a local cluster in background (daemon mode)
137- $ tiup playground-ng stop --tag xx # Stop the cluster started with --tag xx
138- $ tiup playground-ng stop-all # Stop all running playground-ng instances
139- $ tiup playground-ng ps # List all running playground-ng instances
140- $ tiup playground-ng --mode tikv-slim # Start a local tikv only cluster (No TiDB or TiFlash Available)
141- $ tiup playground-ng --mode tikv-slim --kv 3 --pd 3 # Start a local tikv only cluster with 6 nodes` ,
129+ Long : colorstr .Sprintf (`>_ [bold]TiUP Playground[reset] [dim](ng)[reset]
130+
131+ Start and manage a TiDB cluster locally for development.
132+
133+ [bold]Examples:[reset]
134+
135+ [dim]Start a cluster using latest release version:[reset]
136+ [cyan]%[1]s[reset]
137+
138+ [dim]Start a nightly cluster:[reset]
139+ [cyan]%[1]s nightly[reset]
140+
141+ [dim]Start a TiKV-only cluster:[reset]
142+ [cyan]%[1]s --mode tikv-slim[reset]
143+
144+ [dim]Start a cluster and run in background:[reset]
145+ [cyan]%[1]s -d[reset]
146+
147+ [dim]Start a tagged cluster (data will not be cleaned after exit):[reset]
148+ [cyan]%[1]s --tag foo[reset]
149+
150+ [dim]Stop the specified cluster:[reset]
151+ [cyan]%[1]s stop --tag foo[reset]` , arg0 ),
142152 SilenceUsage : true ,
143153 SilenceErrors : true ,
144154 Version : version .NewTiUPVersion ().String (),
@@ -395,10 +405,43 @@ Examples:
395405 color .NoColor = true
396406 }
397407
398- tui .AddColorFunctionsForCobra ()
399- tui .BeautifyCobraUsageAndHelp (rootCmd )
408+ cobra .AddTemplateFunc ("pgCmdLine" , func (useLine string ) string {
409+ return rewriteCobraUseLine (arg0 , useLine )
410+ })
411+ cobra .AddTemplateFunc ("pgCmdPath" , func (commandPath string ) string {
412+ return rewriteCobraCommandPath (arg0 , commandPath )
413+ })
414+
415+ usageTpl := rootCmd .UsageTemplate ()
416+ usageTpl = strings .ReplaceAll (usageTpl , "{{.UseLine}}" , "{{pgCmdLine .UseLine}}" )
417+ usageTpl = strings .ReplaceAll (usageTpl , "{{.CommandPath}}" , "{{pgCmdPath .CommandPath}}" )
418+ rootCmd .SetUsageTemplate (usageTpl )
419+
420+ cc .Init (& cc.Config {
421+ RootCmd : rootCmd ,
422+ Headings : cc .Bold ,
423+ Commands : cc .Cyan + cc .Bold ,
424+ })
425+
426+ usageTpl = rootCmd .UsageTemplate ()
427+ usageTpl = strings .ReplaceAll (usageTpl , "(CommandStyle .CommandPath)" , "(CommandStyle (pgCmdPath .CommandPath))" )
428+ rootCmd .SetUsageTemplate (usageTpl )
429+
430+ // Cobra's default version flag uses the command name derived from `Use`,
431+ // which is the binary name (e.g. tiup-playground-ng). For standalone runs we
432+ // want argv0 (e.g. bin/tiup-playground-ng); for TiUP component mode we want
433+ // `tiup playground-ng[:<ver>]`.
434+ rootCmd .InitDefaultHelpFlag ()
435+ if f := rootCmd .Flags ().Lookup ("help" ); f != nil {
436+ f .Usage = fmt .Sprintf ("help for %s" , arg0 )
437+ }
438+
439+ rootCmd .InitDefaultVersionFlag ()
440+ if f := rootCmd .Flags ().Lookup ("version" ); f != nil {
441+ f .Usage = fmt .Sprintf ("version for %s" , arg0 )
442+ }
400443
401- rootCmd .Flags ().StringVar (& state .options .ShOpt .Mode , "mode" , "tidb" , fmt .Sprintf ("tiup playground-ng mode: '%s', '%s', '%s', '%s', '%s'" , proc .ModeNormal , proc .ModeCSE , proc .ModeNextGen , proc .ModeDisAgg , proc .ModeTiKVSlim ))
444+ rootCmd .Flags ().StringVar (& state .options .ShOpt .Mode , "mode" , "tidb" , fmt .Sprintf ("%s mode: '%s', '%s', '%s', '%s', '%s'" , arg0 , proc .ModeNormal , proc .ModeCSE , proc .ModeNextGen , proc .ModeDisAgg , proc .ModeTiKVSlim ))
402445 rootCmd .Flags ().StringVar (& state .options .ShOpt .PDMode , "pd.mode" , "pd" , "PD mode: 'pd', 'ms'" )
403446 rootCmd .Flags ().StringVar (& state .options .ShOpt .CSE .S3Endpoint , "cse.s3_endpoint" , "http://127.0.0.1:9000" ,
404447 fmt .Sprintf ("Object store URL for --mode=%s, --mode=%s, --mode=%s" , proc .ModeCSE , proc .ModeDisAgg , proc .ModeNextGen ))
@@ -930,7 +973,7 @@ var _ repository.DownloadProgress = (*repoDownloadProgress)(nil)
930973var _ repository.DownloadProgressReporter = (* repoDownloadProgress )(nil )
931974
932975func main () {
933- tui .RegisterArg0 ("tiup playground-ng" )
976+ tui .RegisterArg0 (playgroundCLIArg0 () )
934977
935978 state := newCLIState ()
936979
0 commit comments