Skip to content

Errors thrown from validate() prevent help text from being displayed #149

@sharplet

Description

@sharplet

My command accepts a list of paths as arguments, using @Argument() var paths: [String]. I couldn't find a built in way to validate that the array is non-empty, so I implemented validate() and throw a ValidationError in that case. Because CommandParser validates commands before checking if help was requested, only a short usage string is ever displayed, instead of the full help text.

ArgumentParser version: 0.0.6

Swift version:

Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Target: x86_64-apple-darwin19.4.0

Checklist

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

Steps to Reproduce

Paste this into a new Swift package:

import ArgumentParser

struct Test: ParsableCommand {
  @Argument(help: "A list of paths.")
  var paths: [String]

  func validate() throws {
    if paths.isEmpty {
      throw ValidationError("At least one path must be specified.")
    }
  }

  func run() throws {
    for path in paths {
      print(path)
    }
  }
}

Test.main()

Then run swift run test --help.

Expected behavior

Expected the command to output the help text.

Actual behavior

Received this output instead:

Error: At least one path must be specified.
Usage: test [<paths>  . . .]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions