Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a custom CLI flag separator #1878

Closed
JonasScharpf opened this issue Mar 28, 2024 · 8 comments
Closed

How to use a custom CLI flag separator #1878

JonasScharpf opened this issue Mar 28, 2024 · 8 comments
Labels
area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this

Comments

@JonasScharpf
Copy link

I'm new to Go so please bare with me.

I have an application which accepts multiple --groups flags by using StringSliceFlag which works great.

I can use it with fancy_name --groups="asdf" --groups="qwertz" and get [asdf qwertz].

As soon as the value of the args contains a , it does the right thing and splits it at the comma. fancy_name --groups="asdf,foo" --groups="qwertz" becomes [asdf foo qwertz] but I would need [asdf,foo qwertz]. This is where #1241 and #1134 becomes interesting. I already tried to set app.SliceFlagSeparator = ";" but this has no effect on the flag parsing. Changing the defaultSliceFlagSeparator in flag.go to e.g. ; solved the problem of course.

So my question in the end is how to set a slice separator for all or a specific flag

func main() {
    app := cli.NewApp()

    cli.AppHelpTemplate = flags.Template
    cli.HelpPrinterCustom = flags.HelpPrinter
    // Force the use of cli.HelpPrinterCustom.
    app.ExtraInfo = func() map[string]string { return map[string]string{} }
    app.SliceFlagSeparator = ";"
    // app.DisableSliceFlagSeparator = true
    
    app.Flags = flags.GetCliFlags()
    app.Action = run

    err := app.Run(os.Args)
    if err != nil {
        log.Fatal("terminated:", err)
    }
}

func GetCliFlags() []cli.Flag {
    return []cli.Flag{
        &cli.StringSliceFlag{
            Name:    "groups",
            Value:   cli.NewStringSlice("this,that,other", "foo"),
            Usage:   "Groups, can be given multiple times",
            // EnvVars: []string{"GROUPS"},
    }
}
@JonasScharpf JonasScharpf added area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this labels Mar 28, 2024
@JonasScharpf
Copy link
Author

not setting a Value for StringSliceFlag solves the issue somehow. fancy_name --groups="asdf,foo" --groups="qwertz" then returns the expected content which is [asdf,foo qwertz]

@Pipello
Copy link

Pipello commented Mar 28, 2024

I already tried to set app.SliceFlagSeparator = ";" but this has no effect
I guess that is a bug

or a specific flag
I don't see a real usage fof setting separator per flag but having a Separator string on the StringSliceFlag could solve it

@dearchap
Copy link
Contributor

Can you try WithSeparatorSpec on the string slice flag ?

@JonasScharpf
Copy link
Author

could you give me a code hint for WithSeparatorSpec on a StringSliceFlag? May I do something wrong as Go newbie, because of unknown field 'WithSeparatorSpec' in struct literal of type cli.StringSliceFlag

@dearchap
Copy link
Contributor

dearchap commented Apr 2, 2024

https://github.com/urfave/cli/blob/v2-maint/flag_string_slice.go#L181

Are you using the latest v2 release ?

@JonasScharpf
Copy link
Author

yes I do, v2.27.0

I still believe this is a syntax thing on my side

func GetCliFlags() []cli.Flag {
    return []cli.Flag{
        &cli.StringSliceFlag{
            Name:    "groups",
            Value:   cli.NewStringSlice("this,that,other", "foo"),
            Usage:   "Groups, can be given multiple times",
            WithSeparatorSpec: cli.separatorSpec{sep: ";"},
    }
}

@dearchap
Copy link
Contributor

dearchap commented Apr 3, 2024

Ah no you need to do

func GetCliFlags() []cli.Flag {
    ssflag := &cli.StringSliceFlag{
            Name:    "groups",
            Value:   cli.NewStringSlice("this,that,other", "foo"),
            Usage:   "Groups, can be given multiple times",
    }
    ssflag.WithSeparatorSpec(cli.separatorSpec{sep: ";"})

    return []cli.Flag{
        ssflag,
    }
}

@JonasScharpf
Copy link
Author

Thanks for the hint, but it does not work as separatorSpec is not exported (function starting with lower case). In the yet unreleased v3 this is even no longer available

./first.go:90:34: separatorSpec not exported by package cli
./first.go:90:48: unknown field 'sep' in struct literal of type cli.separatorSpec

I'll stay with v2, keep the setting globally and use no default value

@JonasScharpf JonasScharpf closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

3 participants