Cli: StringSliceFlag doesn't support Destination

Created on 4 Mar 2017  路  12Comments  路  Source: urfave/cli

Multiple flag types support a Destination attribute to directly bind the value to a variable, except StringSliceFlag. Can we add this flag there as well?

arev2 help wanted kinfeature statuconfirmed

Most helpful comment

Works

type Config struct {
    Foo string
}

cfg := Config{}

flags := []cli.Flag{
    &cli.StringFlag{
        Name:        "foo",
        Value:       "bar",
        Usage:       "usage",
        Destination: &cfg.Foo,
    },
}

Fails

type Config struct {
    Foo []string
}

cfg := Config{}

flags := []cli.Flag{
    &cli.StringSliceFlag{
        Name:        "foo",
        Value:       "bar",
        Usage:       "usage",
        Destination: &cfg.Foo,
    },
}

I would expect to be able to use Destination also for StringSliceFlag, which currently doesn't work, so currently I got to bind it to my config struct like this, which is not that nice as Destination would be much cleaner:

Before: func(c *cli.Context) error {
    if len(c.StringSlice("foo")) > 0 {
        // StringSliceFlag doesn't support Destination
        cfg.Foo = c.StringSlice("foo")
    }
}

I hope these examples are detailed enough :)

All 12 comments

@tboerger it isn't readily apparent from the documentation, but it actually augments the Value field since, in this case, it is a pointer. I agree that Destination would be better for consistency though.

Value is something different than Destination :)

But let's see what happens if I assign the default value to the destination variable.

@urfave/cli what is your opinion on this? We can accomodate this change but this would most likely break the public API. Should we defer this to v2 instead?

I am already using gopkg.in/urfave/cli.v2 for ages, but still missing this feature :(

I'm not totally sure I understand the ask here! Perhaps an example would help?

Works

type Config struct {
    Foo string
}

cfg := Config{}

flags := []cli.Flag{
    &cli.StringFlag{
        Name:        "foo",
        Value:       "bar",
        Usage:       "usage",
        Destination: &cfg.Foo,
    },
}

Fails

type Config struct {
    Foo []string
}

cfg := Config{}

flags := []cli.Flag{
    &cli.StringSliceFlag{
        Name:        "foo",
        Value:       "bar",
        Usage:       "usage",
        Destination: &cfg.Foo,
    },
}

I would expect to be able to use Destination also for StringSliceFlag, which currently doesn't work, so currently I got to bind it to my config struct like this, which is not that nice as Destination would be much cleaner:

Before: func(c *cli.Context) error {
    if len(c.StringSlice("foo")) > 0 {
        // StringSliceFlag doesn't support Destination
        cfg.Foo = c.StringSlice("foo")
    }
}

I hope these examples are detailed enough :)

Gotcha! We should definitely support this 馃憤馃憤馃憤

Is it more complicated to get this integrated? Can I help somehow? Getting Destination for the slices would reduce the whole boilerplate code within https://github.com/drone-plugins which are all based on urfave/cli.

馃憢 this is still a valid issue! I'm just gonna close it as a duplicate of https://github.com/urfave/cli/issues/1075

Uhm... You mean #1075 is a duplicate of this, but whatever. I have not received any further information anyway.

Did I mean that? I haven't seen any formal rules anywhere about precedents and duplicates. Apologies if I was rude 馃檹

Was this page helpful?
0 / 5 - 0 ratings