forked from apple/swift-argument-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce subcommand grouping into the command configuration to impro…
…ve help (apple#644) * Introduce subcommand grouping into the command configuration to improve help Add optional support for grouping subcommands into named groups, to help bring order to commands with many subcommands without requiring additional structure. For example, here's the help for a "subgroupings" command that has an ungrouped subcommand (m), and two groups of subcommands ("broken" and "complicated"). 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. To be able freely mix subcommands and subcommand groups, CommandConfiguration has a new initializer that takes a result builder. The help output above is created like this: struct WithSubgroups: ParsableCommand { static let configuration = CommandConfiguration( commandName: "subgroupings" ) { CommandGroup(name: "Broken") { Foo.self Bar.self } M.self CommandGroup(name: "Complicated") { N.self } } } Each `CommandGroup` names a new group and is given commands (there are no groups within groups). The other entries are arbitrary ParsableCommands. This structure is only cosmetic, and only affects help generation by providing more structure for the reader. It doesn't impact existing clients, who can still reason about the flattened list of subcommands if they prefer. * Add an optional abstract to command groups * Expand subcommand group result builders to handle all result-builder syntax Adds support for if, if-else, if #available, and for..in loops. * Revert "Add an optional abstract to command groups" This reverts commit ab563a2. * Eliminate result builders in favor of a second "groupedSubcommands" array Introduce subcommand groups with a more modest extension to the API that adds another array of subcommand groups alongside the (ungrouped) subcommands array. We can consider introducing result builders as a separate step later, if there's more to be gained from it. * Drop the (ungrouped) "subcommands" heading when there are none.
- Loading branch information
1 parent
0fbc884
commit 516c2f8
Showing
4 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters