jakeheis / SwiftCLI

A powerful framework for developing CLIs in Swift
MIT License
861 stars 72 forks source link

False Flag #63

Closed yonaskolb closed 5 years ago

yonaskolb commented 6 years ago

It would be great to be able to set a flag to false. eg: --flag=false

yonaskolb commented 6 years ago

For a bit of context Mint used to have a --global flag. At one point the default value of this changed, so now it's useful to turn it off. Some people have been using --global=false for that. In moving to SwiftCLI this has now broken. There is now an opposite --prevent-global flag, but that's not backwards compatible.

jakeheis commented 6 years ago

You can mostly accomplish this right now with a Key<Bool> which would permit --global=true or --global=false, but not just --global. I think it's relatively rare when one would want to accept both --flag and --flag=false, so I hesitate to build this in by default.

If you need to accept both --global and --global=false, you can accomplish this with the verbose but flexible ArgumentListManipulators

class LegacyGlobalSupport: ArgumentListManipulator {

    func manipulate(arguments: ArgumentList) {
        arguments.manipulate { (args) -> [String] in
            var updated = args
            if let index = updated.index(of: "--global=false") {
                updated[index] = "--prevent-global"
            }
            return updated
        }
    }

}

// Insert in array before OptionSplitter(); otherwise ["--global=false"] would already be split up into ["--global", "false"] 
myCli.argumentListManipulators.insert(LegacyGlobalSupport(), at: 0)