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 built-in support for --version flag #102

Merged
merged 4 commits into from
Mar 30, 2020
Merged

Conversation

natecook1000
Copy link
Member

Description

Adds built-in support for responding to a --version flag. This flag is only valid when a command provides a version: parameter in its configuration. Fixes #25.

Detailed Design

If a command in the tree has specified a version, the --version flag is checked for only after evaluating any explicitly defined arguments. This allows commands to define their own --version flags or options and not have them be overridden by the auto-generated version.

Nested commands inherit their version from their parent commands, and can override their parent by providing their own version string.

version is added to the CommandConfiguration type and to the initializer:

  /// Creates the configuration for a command.
  ///
  /// - Parameters:
  ///   - commandName: The name of the command to use on the command line. If
  ///     `commandName` is `nil`, the command name is derived by converting
  ///     the name of the command type to hyphen-separated lowercase words.
  ///   - abstract: A one-line description of the command.
  ///   - discussion: A longer description of the command.
  ///   - version: The version number for this command. When you provide a
  ///     non-empty string, the arguemnt parser prints it if the user provides
  ///     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.
  ///   - defaultSubcommand: The default command type to run if no subcommand
  ///     is given.
  ///   - helpNames: The flag names to use for requesting help, simulating
  ///     a Boolean property named `help`.
  public init(
    commandName: String? = nil,
    abstract: String = "",
    discussion: String = "",
    version: String = "",
    shouldDisplay: Bool = true,
    subcommands: [ParsableCommand.Type] = [],
    defaultSubcommand: ParsableCommand.Type? = nil,
    helpNames: NameSpecification = [.short, .long]
  )

Documentation Plan

How has the new feature been documented? Have the relevant portions of the guide been updated in addition to symbol-level documentation?

Test Plan

  • Unit tests for generating the version string.
  • End-to-end tests to verify that the version is being rendered as the correct value when defined, and that commands that explicitly define --version get that behavior.
  • Example tests with versions defined in Math.

Source Impact

Because the version: parameter is defaulted, this has no source impact. In addition, since there's no change in behavior unless the command defines a version, there's no impact on existing tools' behavior.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@natecook1000
Copy link
Member Author

@griffin-stewie What do you think of this design?

@griffin-stewie
Copy link
Contributor

griffin-stewie commented Mar 29, 2020

@natecook1000 Thank you for adding built-in support for version flag. It looks great.

One thing I noticed is that the information about the version flag is not printed in the OPTIONS section of the help. I felt it would have been nice to have a help message output to make it more end-user friendly.

You don't support short names like -v, do you? It might be nice to be able to customize it like var helpNames: NameSpecification which is already provided.

@natecook1000
Copy link
Member Author

Great points, @griffin-stewie! I've added the flag to the generated help, but I'm holding off for now on customizing the flag. There might be a more general pattern for customizing these built-in options, which I'd like to look at after getting code-completion support built.

@natecook1000 natecook1000 merged commit 31799bc into master Mar 30, 2020
@natecook1000 natecook1000 deleted the nate/version_flag branch March 30, 2020 17:36
@griffin-stewie
Copy link
Contributor

I agree with you. Thank you for implementing it.

@dduan
Copy link
Contributor

dduan commented Apr 22, 2020

This is awesome. Thanks!

dduan added a commit to dduan/DrString that referenced this pull request Apr 22, 2020
Use the built-in version support from Swift ArgumentParser: apple/swift-argument-parser#102
dduan added a commit to dduan/DrString that referenced this pull request Apr 22, 2020
Use the built-in version support from Swift ArgumentParser: apple/swift-argument-parser#102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is there a way to balance the processing of the argument with the "--version" option?
3 participants