From 4716dc11af97aa7fd965071461953b5aeb0a59be Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Sat, 15 Nov 2025 19:17:38 +0800 Subject: [PATCH] cmd, internal: move func `CheckExclusive()` to package flags #31189 (#1747) --- cmd/abigen/main.go | 2 +- cmd/utils/flags.go | 51 ++++----------------------------------- internal/flags/helpers.go | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/cmd/abigen/main.go b/cmd/abigen/main.go index 8338eb8e79..d25c26f31c 100644 --- a/cmd/abigen/main.go +++ b/cmd/abigen/main.go @@ -92,7 +92,7 @@ func init() { } func generate(c *cli.Context) error { - utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. + flags.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. if c.String(pkgFlag.Name) == "" { utils.Fatalf("No destination package specified (--pkg)") diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 162e0c5231..7fb28a0cea 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -945,7 +945,7 @@ func setNodeUserIdent(ctx *cli.Context, cfg *node.Config) { } func setWhiteBlackListPeers(ctx *cli.Context, cfg *p2p.Config) { - CheckExclusive(ctx, PeersWhitelistFlag, PeersBlacklistFlag) + flags.CheckExclusive(ctx, PeersWhitelistFlag, PeersBlacklistFlag) // setup whitelist for peers if ctx.IsSet(PeersWhitelistFlag.Name) { @@ -1195,7 +1195,7 @@ func setWS(ctx *cli.Context, cfg *node.Config) { // setIPC creates an IPC path configuration from the set command line flags, // returning an empty string if IPC was explicitly disabled, or the set path. func setIPC(ctx *cli.Context, cfg *node.Config) { - CheckExclusive(ctx, IPCDisabledFlag, IPCPathFlag) + flags.CheckExclusive(ctx, IPCDisabledFlag, IPCPathFlag) switch { case ctx.Bool(IPCDisabledFlag.Name): cfg.IPCPath = "" @@ -1323,7 +1323,7 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { cfg.NoDiscovery = true } - CheckExclusive(ctx, DiscoveryV5Flag, NoDiscoverFlag) + flags.CheckExclusive(ctx, DiscoveryV5Flag, NoDiscoverFlag) cfg.DiscoveryV5 = ctx.Bool(DiscoveryV5Flag.Name) if netrestrict := ctx.String(NetrestrictFlag.Name); netrestrict != "" { @@ -1345,7 +1345,7 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { // SetNodeConfig applies node-related command line flags to the config. func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { - CheckExclusive(ctx, Enable0xPrefixFlag, EnableXDCPrefixFlag) + flags.CheckExclusive(ctx, Enable0xPrefixFlag, EnableXDCPrefixFlag) SetP2PConfig(ctx, &cfg.P2P) setIPC(ctx, cfg) setHTTP(ctx, cfg) @@ -1468,47 +1468,6 @@ func setTxPool(ctx *cli.Context, cfg *txpool.Config) { } } -// CheckExclusive verifies that only a single isntance of the provided flags was -// set by the user. Each flag might optionally be followed by a string type to -// specialize it further. -func CheckExclusive(ctx *cli.Context, args ...interface{}) { - set := make([]string, 0, 1) - for i := 0; i < len(args); i++ { - // Make sure the next argument is a flag and skip if not set - flag, ok := args[i].(cli.Flag) - if !ok { - panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i])) - } - // Check if next arg extends current and expand its name if so - name := flag.Names()[0] - - if i+1 < len(args) { - switch option := args[i+1].(type) { - case string: - // Extended flag check, make sure value set doesn't conflict with passed in option - if ctx.String(flag.Names()[0]) == option { - name += "=" + option - set = append(set, "--"+name) - } - // shift arguments and continue - i++ - continue - - case cli.Flag: - default: - panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1])) - } - } - // Mark the flag if it's set - if ctx.IsSet(flag.Names()[0]) { - set = append(set, "--"+name) - } - } - if len(set) > 1 { - Fatalf("Flags %v can't be used at the same time", strings.Join(set, ", ")) - } -} - func SetXDCXConfig(ctx *cli.Context, cfg *XDCx.Config, XDCDataDir string) { if ctx.IsSet(XDCXDataDirFlag.Name) { log.Warn("The flag XDCx-datadir or XDCx.datadir is deprecated, please remove this flag") @@ -1539,7 +1498,7 @@ func SetXDCXConfig(ctx *cli.Context, cfg *XDCx.Config, XDCDataDir string) { // SetEthConfig applies eth-related command line flags to the config. func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { // Avoid conflicting network flags - CheckExclusive(ctx, MainnetFlag, TestnetFlag, DevnetFlag, DeveloperFlag) + flags.CheckExclusive(ctx, MainnetFlag, TestnetFlag, DevnetFlag, DeveloperFlag) ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) setEtherbase(ctx, ks, cfg) diff --git a/internal/flags/helpers.go b/internal/flags/helpers.go index 486acdcc7b..0b5eac5a83 100644 --- a/internal/flags/helpers.go +++ b/internal/flags/helpers.go @@ -295,3 +295,45 @@ func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) { } } } + +// CheckExclusive verifies that only a single instance of the provided flags was +// set by the user. Each flag might optionally be followed by a string type to +// specialize it further. +func CheckExclusive(ctx *cli.Context, args ...any) { + set := make([]string, 0, 1) + for i := 0; i < len(args); i++ { + // Make sure the next argument is a flag and skip if not set + flag, ok := args[i].(cli.Flag) + if !ok { + panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i])) + } + // Check if next arg extends current and expand its name if so + name := flag.Names()[0] + + if i+1 < len(args) { + switch option := args[i+1].(type) { + case string: + // Extended flag check, make sure value set doesn't conflict with passed in option + if ctx.String(flag.Names()[0]) == option { + name += "=" + option + set = append(set, "--"+name) + } + // shift arguments and continue + i++ + continue + + case cli.Flag: + default: + panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1])) + } + } + // Mark the flag if it's set + if ctx.IsSet(flag.Names()[0]) { + set = append(set, "--"+name) + } + } + if len(set) > 1 { + fmt.Fprintf(os.Stderr, "Flags %v can't be used at the same time", strings.Join(set, ", ")) + os.Exit(1) + } +}