Skip to content

Including transform: parameter in @Argument declaration generates wrong help message for optional arguments #466

@YourMJK

Description

@YourMJK

Declaring an argument with @Argument(..., transform:) makes it non-optional in the "USAGE" and doesn't include it's default value in the argument's abstract in the "ARGUMENTS" section, even if the value has a default value and/or is of an optional type.
(The actual parsing of the optional arguments seems to be correct)

ArgumentParser version: 1.1.3
Swift version: swift-driver version: 1.26.9 Apple Swift version 5.5.1 (swiftlang-1300.0.31.4 clang-1300.0.29.6)

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

Declare arguments like this in a ParsableCommand struct:

struct Cmd1: ParsableCommand {
  @Argument(help: "Test argument 1", transform: { Int($0)! })
  var test1: Int = 0
  
  @Argument(help: "Test argument 2", transform: URL.init(fileURLWithPath:))
  var test2: URL?
}

and print the help message through Cmd.parseOrExit([]) or print(Cmd.helpMessage())

Expected behavior

The printed help string (namely USAGE and ARGUMENTS) should be the same as if the arguments were declared like this (without the transform: parameter):

struct Cmd2: ParsableCommand {
  @Argument(help: "Test argument 1")
  var test1: Int = 0
  
  @Argument(help: "Test argument 2")
  var test2: String?
}

Actual behavior

Help message for arguments with transform: parameter looks like this:

USAGE: cmd1 <test1> <test2>

ARGUMENTS:
  <test1>                 Test argument 1
  <test2>                 Test argument 2

While help message without transform: is correctly generated:

USAGE: cmd2 [<test1>] [<test2>]

ARGUMENTS:
  <test1>                 Test argument 1 (default: 0)
  <test2>                 Test argument 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions