Skip to content

Introduce subcommand grouping into the command configuration to improve help #644

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

Merged
merged 6 commits into from
Jun 4, 2024
Merged
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
41 changes: 33 additions & 8 deletions Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,27 @@ public struct CommandConfiguration: Sendable {
public var shouldDisplay: Bool

/// An array of the types that define subcommands for this command.
public var subcommands: [ParsableCommand.Type]

///
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we drop this stray doc comment line?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was using the extra line to separate the "abstract" line from the "detail" line.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wow I can't this morning, I made like 2 other mistakes similar, sorry about the noise

Copy link
Member

Choose a reason for hiding this comment

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

It's a weird diff!

/// This property "flattens" the grouping structure of the subcommands.
/// Use 'ungroupedSubcommands' to access 'groupedSubcommands' to retain the grouping structure.
public var subcommands: [ParsableCommand.Type] {
get {
return ungroupedSubcommands + groupedSubcommands.flatMap { $0.subcommands }
}

set {
groupedSubcommands = []
ungroupedSubcommands = newValue
}
}

/// An array of types that define subcommands for this command and are
/// not part of any command group.
public var ungroupedSubcommands: [ParsableCommand.Type]

/// The list of subcommands and subcommand groups.
public var groupedSubcommands: [CommandGroup]
Comment on lines +64 to +69
Copy link
Member

Choose a reason for hiding this comment

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

These look great 👍🏻


/// The default command type to run if no subcommand is given.
public var defaultSubcommand: ParsableCommand.Type?

Expand Down Expand Up @@ -79,8 +98,10 @@ public struct CommandConfiguration: Sendable {
/// a `--version` flag.
/// - shouldDisplay: A Boolean value indicating whether the command
/// should be shown in the extended help display.
/// - subcommands: An array of the types that define subcommands for the
/// command.
/// - ungroupedSubcommands: An array of the types that define subcommands
/// for the command that are not part of any command group.
/// - groupedSubcommands: An array of command groups, each of which defines
/// subcommands that are part of that logical group.
/// - defaultSubcommand: The default command type to run if no subcommand
/// is given.
/// - helpNames: The flag names to use for requesting help, when combined
Expand All @@ -97,7 +118,8 @@ public struct CommandConfiguration: Sendable {
discussion: String = "",
version: String = "",
shouldDisplay: Bool = true,
subcommands: [ParsableCommand.Type] = [],
subcommands ungroupedSubcommands: [ParsableCommand.Type] = [],
groupedSubcommands: [CommandGroup] = [],
defaultSubcommand: ParsableCommand.Type? = nil,
helpNames: NameSpecification? = nil,
aliases: [String] = []
Expand All @@ -108,7 +130,8 @@ public struct CommandConfiguration: Sendable {
self.discussion = discussion
self.version = version
self.shouldDisplay = shouldDisplay
self.subcommands = subcommands
self.ungroupedSubcommands = ungroupedSubcommands
self.groupedSubcommands = groupedSubcommands
self.defaultSubcommand = defaultSubcommand
self.helpNames = helpNames
self.aliases = aliases
Expand All @@ -124,7 +147,8 @@ public struct CommandConfiguration: Sendable {
discussion: String = "",
version: String = "",
shouldDisplay: Bool = true,
subcommands: [ParsableCommand.Type] = [],
subcommands ungroupedSubcommands: [ParsableCommand.Type] = [],
groupedSubcommands: [CommandGroup] = [],
defaultSubcommand: ParsableCommand.Type? = nil,
helpNames: NameSpecification? = nil,
aliases: [String] = []
Expand All @@ -136,7 +160,8 @@ public struct CommandConfiguration: Sendable {
self.discussion = discussion
self.version = version
self.shouldDisplay = shouldDisplay
self.subcommands = subcommands
self.ungroupedSubcommands = ungroupedSubcommands
self.groupedSubcommands = groupedSubcommands
self.defaultSubcommand = defaultSubcommand
self.helpNames = helpNames
self.aliases = aliases
Expand Down
28 changes: 28 additions & 0 deletions Sources/ArgumentParser/Parsable Types/CommandGroup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

/// A set of commands grouped together under a common name.
public struct CommandGroup: Sendable {
/// The name of the command group that will be displayed in help.
public let name: String

/// The list of subcommands that are part of this group.
public let subcommands: [ParsableCommand.Type]

/// Create a command group.
public init(
name: String,
subcommands: [ParsableCommand.Type]
) {
self.name = name
self.subcommands = subcommands
}
}
80 changes: 60 additions & 20 deletions Sources/ArgumentParser/Usage/HelpGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ internal struct HelpGenerator {
case subcommands
case options
case title(String)

case groupedSubcommands(String)

var description: String {
switch self {
case .positionalArguments:
Expand All @@ -63,6 +64,8 @@ internal struct HelpGenerator {
return "Options"
case .title(let name):
return name
case .groupedSubcommands(let name):
return "\(name) Subcommands"
}
}
}
Expand Down Expand Up @@ -211,34 +214,67 @@ internal struct HelpGenerator {
}

let configuration = commandStack.last!.configuration
let subcommandElements: [Section.Element] =
configuration.subcommands.compactMap { command in
guard command.configuration.shouldDisplay else { return nil }
var label = command._commandName
for alias in command.configuration.aliases {
label += ", \(alias)"
}
if command == configuration.defaultSubcommand {
label += " (default)"
}
return Section.Element(
label: label,
abstract: command.configuration.abstract)

// Create section for a grouping of subcommands.
func subcommandSection(
header: Section.Header,
subcommands: [ParsableCommand.Type]
) -> Section {
let subcommandElements: [Section.Element] =
subcommands.compactMap { command in
guard command.configuration.shouldDisplay else { return nil }
var label = command._commandName
for alias in command.configuration.aliases {
label += ", \(alias)"
}
if command == configuration.defaultSubcommand {
label += " (default)"
}
return Section.Element(
label: label,
abstract: command.configuration.abstract)
}

return Section(header: header, elements: subcommandElements)
}


// All of the subcommand sections.
var subcommands: [Section] = []

// Add section for the ungrouped subcommands, if there are any.
if !configuration.ungroupedSubcommands.isEmpty {
subcommands.append(
subcommandSection(
header: .subcommands,
subcommands: configuration.ungroupedSubcommands
)
)
}

// Add sections for all of the grouped subcommands.
subcommands.append(
contentsOf: configuration.groupedSubcommands
.compactMap { group in
return subcommandSection(
header: .groupedSubcommands(group.name),
subcommands: group.subcommands
)
}
)

// Combine the compiled groups in this order:
// - arguments
// - named sections
// - options/flags
// - subcommands
// - ungrouped subcommands
// - grouped subcommands
return [
Section(header: .positionalArguments, elements: positionalElements),
] + sectionTitles.map { name in
Section(header: .title(name), elements: titledSections[name, default: []])
} + [
Section(header: .options, elements: optionElements),
Section(header: .subcommands, elements: subcommandElements),
]
] + subcommands
}

func usageMessage() -> String {
Expand All @@ -247,8 +283,12 @@ internal struct HelpGenerator {
}

var includesSubcommands: Bool {
guard let subcommandSection = sections.first(where: { $0.header == .subcommands })
else { return false }
guard let subcommandSection = sections.first(where: {
switch $0.header {
case .groupedSubcommands, .subcommands: return true
case .options, .positionalArguments, .title(_): return false
}
}) else { return false }
return !subcommandSection.elements.isEmpty
}

Expand Down
70 changes: 70 additions & 0 deletions Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,76 @@ extension HelpGenerationTests {

""")
}

struct WithSubgroups: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "subgroupings",
subcommands: [ M.self ],
groupedSubcommands: [
CommandGroup(
name: "Broken",
subcommands: [ Foo.self, Bar.self ]
),
CommandGroup(name: "Complicated", subcommands: [ N.self ])
]
)
}

func testHelpSubcommandGroups() throws {
AssertHelp(.default, for: WithSubgroups.self, equals: """
USAGE: subgroupings <subcommand>

OPTIONS:
-h, --help Show help information.

SUBCOMMANDS:
m

BROKEN SUBCOMMANDS:
foo Perform some foo
bar Perform bar operations

COMPLICATED SUBCOMMANDS:
n

See 'subgroupings help <subcommand>' for detailed help.
""")
}

struct OnlySubgroups: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "subgroupings",
groupedSubcommands: [
CommandGroup(
name: "Broken",
subcommands: [ Foo.self, Bar.self ]
),
CommandGroup(
name: "Complicated",
subcommands: [ M.self, N.self ]
)
]
)
}

func testHelpOnlySubcommandGroups() throws {
AssertHelp(.default, for: OnlySubgroups.self, equals: """
USAGE: subgroupings <subcommand>

OPTIONS:
-h, --help Show help information.

BROKEN SUBCOMMANDS:
foo Perform some foo
bar Perform bar operations

COMPLICATED SUBCOMMANDS:
m
n

See 'subgroupings help <subcommand>' for detailed help.
""")
}
}

extension HelpGenerationTests {
Expand Down