Skip to content

c.IsSet returns true for empty env vars #517

Closed

Description

@vikstrous

I want to be able to differentiate between empty env vars and unset env vars, but they are treated the same right now:

package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    app := cli.NewApp()
    app.Name = "boom"
    app.Usage = "make an explosive entrance"
    app.Flags = []cli.Flag{
        cli.StringFlag{
            Name:   "thing",
            EnvVar: "THING",
        },
    }
    app.Action = func(c *cli.Context) error {
        fmt.Printf("isset: %t\n", c.IsSet("thing"))
        return nil
    }
    os.Setenv("THING", "")
    //os.Setenv("THING", "set")

    app.Run(os.Args)
}

This prints

isset: false

If you uncomment the commented out line it prints:

isset: true

I need both cases to be treated as set. Right now I have some ugly workarounds to do this.

Activity

  1. changed the title [-]isset returns true for empty env vars[/-] [+]c.IsSet returns true for empty env vars[/+] on Sep 1, 2016
  2. jszwedko commented on Sep 3, 2016

    @jszwedko
    Contributor

    Hi @vikstrous,

    Thank you for bringing this up. I've gone back and forth on how to treat empty env vars, but I think I agree with you that they should be treated as set and override any defaults defined on the flag. I could see applications depending on this behavior, but given that IsSet only actually worked with env vars (#502) as of this past month (and hasn't been officially released), I'm OK with modifying it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugdescribes or fixes a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions