-
-
Save holiman/5ed50fc9dcf44fd661abdb91a59137fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[user@work cmdtest]$ ./cmdtest --header aa --header bb --url foo | |
invoked via main app | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: aa | |
1: bb | |
ctx.IsSet(UrlFlag.Name): true | |
foo | |
[user@work cmdtest]$ ./cmdtest connect --header aa --header bb --url foo | |
invoked via subcommand | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: aa | |
1: bb | |
ctx.IsSet(UrlFlag.Name): true | |
foo | |
[user@work cmdtest]$ ./cmdtest --header aa --header bb --url foo connect | |
invoked via subcommand | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: aa | |
1: bb | |
ctx.IsSet(UrlFlag.Name): true | |
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[user@work cmdtest]$ ./cmdtest --header aa --header bb --url foo | |
invoked via main app | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: aa | |
1: bb | |
ctx.IsSet(UrlFlag.Name): true | |
foo | |
[user@work cmdtest]$ ./cmdtest connect --header aa --header bb --url foo | |
invoked via subcommand | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: aa | |
1: bb | |
ctx.IsSet(UrlFlag.Name): true | |
foo | |
[user@work cmdtest]$ ./cmdtest --header aa --header bb --url foo connect | |
invoked via subcommand | |
ctx.IsSet(HttpHeaderFlag.Name): true | |
0: [aa bb] | |
ctx.IsSet(UrlFlag.Name): true | |
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/ethereum/go-ethereum/internal/flags" | |
"github.com/urfave/cli/v2" | |
) | |
// NewApp creates an app with sane defaults. | |
func NewApp() *cli.App { | |
app := cli.NewApp() | |
app.Before = func(ctx *cli.Context) error { | |
flags.MigrateGlobalFlags(ctx) | |
return nil | |
} | |
return app | |
} | |
var ( | |
// The app that holds all commands and flags. | |
app = NewApp() | |
HttpHeaderFlag = &cli.StringSliceFlag{ | |
Name: "header", | |
Aliases: []string{"H"}, | |
Usage: "Pass custom header(s) to server", | |
} | |
UrlFlag = &cli.StringFlag{ | |
Name: "url", | |
Aliases: []string{"U"}, | |
Usage: "URL to connect", | |
} | |
subCmd = &cli.Command{ | |
Action: subCommand, | |
Name: "connect", | |
Usage: "", | |
Flags: []cli.Flag{ | |
HttpHeaderFlag, | |
UrlFlag, | |
}, | |
Description: "Shows metadata about the chain status.", | |
} | |
) | |
func init() { | |
// Initialize the CLI app and start Geth | |
app.Action = mainApp | |
app.Name = "Main app" | |
app.Commands = []*cli.Command{ | |
subCmd, | |
} | |
app.Flags = []cli.Flag{ | |
HttpHeaderFlag, | |
UrlFlag, | |
} | |
} | |
func mainApp(ctx *cli.Context) error { | |
fmt.Printf("invoked via main app\n") | |
printCtx(ctx) | |
return nil | |
} | |
func subCommand(ctx *cli.Context) error { | |
fmt.Printf("invoked via subcommand\n") | |
printCtx(ctx) | |
return nil | |
} | |
func printCtx(ctx *cli.Context) { | |
fmt.Printf(" ctx.IsSet(HttpHeaderFlag.Name): %v\n", ctx.IsSet(HttpHeaderFlag.Name)) | |
if ctx.IsSet(HttpHeaderFlag.Name) { | |
for i, v := range ctx.StringSlice(HttpHeaderFlag.Name) { | |
fmt.Printf(" %d: %v\n", i, v) | |
} | |
} | |
fmt.Printf(" ctx.IsSet(UrlFlag.Name): %v\n", ctx.IsSet(UrlFlag.Name)) | |
if ctx.IsSet(UrlFlag.Name) { | |
fmt.Printf(" %v\n", ctx.String(UrlFlag.Name)) | |
} | |
} | |
func main() { | |
if err := app.Run(os.Args); err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment