diff --git a/cmd/oras/internal/errors/errors.go b/cmd/oras/internal/errors/errors.go index 1d32f019c..baa32ca94 100644 --- a/cmd/oras/internal/errors/errors.go +++ b/cmd/oras/internal/errors/errors.go @@ -181,3 +181,19 @@ func CheckMutuallyExclusiveFlags(fs *pflag.FlagSet, exclusiveFlagSets ...[]strin } return nil } + +// CheckRequiredTogetherFlags checks if any flags required together are all set, +// returns an error when detecting any of the required flags not set. +func CheckRequiredTogetherFlags(fs *pflag.FlagSet, requiredTogetherFlags ...string) error { + var unchangedFlags []string + for _, flagName := range requiredTogetherFlags { + if !fs.Changed(flagName) { + unchangedFlags = append(unchangedFlags, fmt.Sprintf("--%s", flagName)) + } + } + if len(unchangedFlags) > 0 { + flags := strings.Join(unchangedFlags, ", ") + return fmt.Errorf("%s required but not provided", flags) + } + return nil +} diff --git a/cmd/oras/internal/option/remote.go b/cmd/oras/internal/option/remote.go index 156059f9e..eca9b9878 100644 --- a/cmd/oras/internal/option/remote.go +++ b/cmd/oras/internal/option/remote.go @@ -163,9 +163,9 @@ func (opts *Remote) Parse(cmd *cobra.Command) error { if err := opts.parseCustomHeaders(); err != nil { return err } - - cmd.MarkFlagsRequiredTogether(certFileAndKeyFileFlags...) - + if err := oerrors.CheckRequiredTogetherFlags(cmd.Flags(), certFileAndKeyFileFlags...); err != nil { + return err + } return opts.readSecret(cmd) }