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

Hide optional flags when a command has too many options #416

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Hide optional flags when too many options
The usage string feature that only shows positional args and required
options/flags was incorrectly allowing through flags with type
`Bool?`.
  • Loading branch information
natecook1000 committed Mar 3, 2022
commit 194f0b8d2a4376449e888d03b1d7cfbd426c88b1
18 changes: 16 additions & 2 deletions Sources/ArgumentParser/Parsable Properties/Flag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ extension Flag where Value == Optional<Bool> {
help: ArgumentHelp? = nil
) {
self.init(_parsedValue: .init { key in
.flag(key: key, name: name, default: nil, inversion: inversion, exclusivity: exclusivity, help: help)
.flag(
key: key,
name: name,
default: nil,
required: false,
inversion: inversion,
exclusivity: exclusivity,
help: help)
})
}
}
Expand Down Expand Up @@ -257,7 +264,14 @@ extension Flag where Value == Bool {
help: ArgumentHelp?
) {
self.init(_parsedValue: .init { key in
.flag(key: key, name: name, default: initial, inversion: inversion, exclusivity: exclusivity, help: help)
.flag(
key: key,
name: name,
default: initial,
required: initial == nil,
inversion: inversion,
exclusivity: exclusivity,
help: help)
})
}

Expand Down
12 changes: 10 additions & 2 deletions Sources/ArgumentParser/Parsing/ArgumentSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,17 @@ extension ArgumentSet {
}

/// Creates an argument set for a pair of inverted Boolean flags.
static func flag(key: InputKey, name: NameSpecification, default initialValue: Bool?, inversion: FlagInversion, exclusivity: FlagExclusivity, help: ArgumentHelp?) -> ArgumentSet {
static func flag(
key: InputKey,
name: NameSpecification,
default initialValue: Bool?,
required: Bool,
inversion: FlagInversion,
exclusivity: FlagExclusivity,
help: ArgumentHelp?) -> ArgumentSet
{
// The flag is required if initialValue is `nil`, otherwise it's optional
let helpOptions: ArgumentDefinition.Help.Options = initialValue != nil ? .isOptional : []
Copy link
Member

Choose a reason for hiding this comment

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

update comment above?

let helpOptions: ArgumentDefinition.Help.Options = required ? [] : .isOptional

let enableHelp = ArgumentDefinition.Help(options: helpOptions, help: help, defaultValue: initialValue.map(String.init), key: key, isComposite: true)
let disableHelp = ArgumentDefinition.Help(options: [.isOptional], help: help, key: key)
Expand Down
4 changes: 4 additions & 0 deletions Tests/ArgumentParserUnitTests/UsageGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ extension UsageGenerationTests {
@Flag var j: Bool = false
@Flag var k: Bool = false
@Flag var l: Bool = false

@Flag(inversion: .prefixedEnableDisable)
var m: Bool?

@Option var option: Bool
@Argument var input: String
@Argument var output: String?
Expand Down