mirror of
https://github.com/kovetskiy/mark.git
synced 2025-09-16 09:27:38 +08:00
Make flag mutually exclusive
This commit is contained in:
parent
de0e1b622a
commit
2173fbcfcd
2
main.go
2
main.go
@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "dev"
|
version = "dev"
|
||||||
commit = "none"
|
commit = "none"
|
||||||
@ -30,6 +29,7 @@ func main() {
|
|||||||
Flags: util.Flags,
|
Flags: util.Flags,
|
||||||
EnableShellCompletion: true,
|
EnableShellCompletion: true,
|
||||||
HideHelpCommand: true,
|
HideHelpCommand: true,
|
||||||
|
Before: util.CheckMutuallyExclusiveTitleFlags,
|
||||||
Action: util.RunMark,
|
Action: util.RunMark,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
52
util/cli_test.go
Normal file
52
util/cli_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runWithArgs(args []string) error {
|
||||||
|
cmd := &cli.Command{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "title-from-h1"},
|
||||||
|
&cli.BoolFlag{Name: "title-from-filename"},
|
||||||
|
},
|
||||||
|
Before: CheckMutuallyExclusiveTitleFlags,
|
||||||
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return cmd.Run(context.Background(), args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckMutuallyExclusiveTitleFlags(t *testing.T) {
|
||||||
|
t.Run("neither flag set", func(t *testing.T) {
|
||||||
|
err := runWithArgs([]string{"cmd"})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("only title-from-h1 set", func(t *testing.T) {
|
||||||
|
err := runWithArgs([]string{"cmd", "--title-from-h1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("only title-from-filename set", func(t *testing.T) {
|
||||||
|
err := runWithArgs([]string{"cmd", "--title-from-filename"})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("both flags set", func(t *testing.T) {
|
||||||
|
err := runWithArgs([]string{"cmd", "--title-from-h1", "--title-from-filename"})
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error, got nil")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
altsrc "github.com/urfave/cli-altsrc/v3"
|
altsrc "github.com/urfave/cli-altsrc/v3"
|
||||||
altsrctoml "github.com/urfave/cli-altsrc/v3/toml"
|
altsrctoml "github.com/urfave/cli-altsrc/v3/toml"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@ -58,13 +61,13 @@ var Flags = []cli.Flag{
|
|||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "title-from-h1",
|
Name: "title-from-h1",
|
||||||
Value: false,
|
Value: false,
|
||||||
Usage: "extract page title from a leading H1 heading. If no H1 heading on a page exists, then title must be set in the page metadata.",
|
Usage: "extract page title from a leading H1 heading. If no H1 heading on a page exists, then title must be set in the page metadata. Mutually exclusive with --title-from-filename.",
|
||||||
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_TITLE_FROM_H1"), altsrctoml.TOML("title-from-h1", altsrc.NewStringPtrSourcer(&filename))),
|
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_TITLE_FROM_H1"), altsrctoml.TOML("title-from-h1", altsrc.NewStringPtrSourcer(&filename))),
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "title-from-filename",
|
Name: "title-from-filename",
|
||||||
Value: false,
|
Value: false,
|
||||||
Usage: "use the filename (without extension) as the Confluence page title if no explicit 'Title' header or H1 heading is found.",
|
Usage: "use the filename (without extension) as the Confluence page title if no explicit page title is set in the metadata. Mutually exclusive with --title-from-h1.",
|
||||||
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_TITLE_FROM_FILENAME"), altsrctoml.TOML("title-from-filename", altsrc.NewStringPtrSourcer(&filename))),
|
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_TITLE_FROM_FILENAME"), altsrctoml.TOML("title-from-filename", altsrc.NewStringPtrSourcer(&filename))),
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
@ -194,3 +197,11 @@ var Flags = []cli.Flag{
|
|||||||
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_FEATURES"), altsrctoml.TOML("features", altsrc.NewStringPtrSourcer(&filename))),
|
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_FEATURES"), altsrctoml.TOML("features", altsrc.NewStringPtrSourcer(&filename))),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckMutuallyExclusiveTitleFlags checks if both title-from-h1 and title-from-filename are set
|
||||||
|
func CheckMutuallyExclusiveTitleFlags(context context.Context, command *cli.Command) (context.Context, error) {
|
||||||
|
if command.Bool("title-from-h1") && command.Bool("title-from-filename") {
|
||||||
|
return context, errors.New("flags --title-from-h1 and --title-from-filename are mutually exclusive. Please specify only one")
|
||||||
|
}
|
||||||
|
return context, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user