Skip to content

Add support for customizing default value string displayed in Help #87

@griffin-stewie

Description

@griffin-stewie

Sample code

Here is my sample code:

extension URL: ExpressibleByArgument {
    public init?(argument: String) {
        guard let url = URL(string: argument) else {
            return nil
        }
        self = url
    }
}
struct MySample: ParsableCommand {
    @Option(default: URL(string: FileManager.default.currentDirectoryPath)!, help: "Directory")
    var directory: URL
}

Produces output:

USAGE: sample [--directory <directory>]

OPTIONS:
  -v, --version           Print version 
  --directory <directory> Directory (default:
                          /Users/stewie/Library/Developer/Xcode/DerivedData/sample-blghzaivmncuwahbbafbmsweltpy/Build/Products/Debug)
  -h, --help              Show help information.

Expected behavior

I would expect the following

  --directory <directory> Directory (default: current directory)

Actual behavior

I get:

  --directory <directory> Directory (default:
                          /Users/stewie/Library/Developer/Xcode/DerivedData/sample-blghza

Current directory is common default value for file path like ls command. but 1294160 prints actual full path on help. Since defaultValueDescription is implemented 1294160 and it is not a protocol requirement.

Plan

Simply we can add defaultValueDescription in ExpressibleByArgument protocol

/// A type that can be expressed as a command-line argument.
public protocol ExpressibleByArgument {
  /// Creates a new instance of this type from a command-line-specified
  /// argument.
  init?(argument: String)

  /// Add this ↓
  var defaultValueDescription: String { get }
}

Source Impact

Default behavior is given by default implementation declared in ExpressibleByArgument as Protocol Extensions. Only users who want to customize need to override defaultValueDescription.

Environment

ArgumentParser: 1294160

Related Forum Thread

How can I change the default value string displayed in Help? - Related Projects / ArgumentParser - Swift Forums

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