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

Add support for Void? flags #191

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/ArgumentParser/Parsable Properties/Flag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ extension Flag where Value == Optional<Bool> {
}
}

extension Flag where Value == Optional<Void> {
public init(
name: NameSpecification = .long,
help: ArgumentHelp? = nil
) {
self.init(_parsedValue: .init { key in
.flag(key: key, name: name, help: help, value: ())
})
}
}

extension Flag where Value == Bool {
/// Creates a Boolean property that reads its value from the presence of a
/// flag.
Expand Down
8 changes: 8 additions & 0 deletions Sources/ArgumentParser/Parsing/ArgumentSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ extension ArgumentSet {
return ArgumentSet(arg)
}

static func flag(key: InputKey, name: NameSpecification, help: ArgumentHelp?, value: Void) -> ArgumentSet {
Copy link
Contributor Author

@MPLew-is MPLew-is Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also like to note that the value: Void argument is temporary, as the conflicting Bool-related flag method changes signature in #170, and this function would be flag(key:name:help:) after that.

let help = ArgumentDefinition.Help(options: .isOptional, help: help, key: key)
let arg = ArgumentDefinition(kind: .name(key: key, specification: name), help: help, update: .nullary({ (origin, name, values) in
values.set(value, forKey: key, inputOrigin: origin)
}))
return ArgumentSet(arg)
}

static func updateFlag<Value: Equatable>(key: InputKey, value: Value, origin: InputOrigin, values: inout ParsedValues, hasUpdated: Bool, exclusivity: FlagExclusivity) throws -> Bool {
switch (hasUpdated, exclusivity) {
case (true, .exclusive):
Expand Down
20 changes: 20 additions & 0 deletions Tests/ArgumentParserEndToEndTests/FlagsEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,23 @@ extension FlagsEndToEndTests {
}
}
}


fileprivate struct VoidFlag: ParsableArguments {
@Flag()
var test: Void?
}

extension FlagsEndToEndTests {
func testParsingVoid_DefaultNil() throws {
AssertParse(VoidFlag.self, []) { options in
XCTAssert(options.test == nil)
}
}

func testParsingVoid_OverrideDefault() throws {
AssertParse(VoidFlag.self, ["--test"]) { options in
XCTAssert(options.test != nil)
}
}
}