swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

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

Open YourMJK opened this issue 2 years ago • 0 comments

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

  • [x] If possible, I've reproduced the issue using the main branch of this package
  • [x] 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

YourMJK avatar Aug 12 '22 14:08 YourMJK