ArgumentParser/Option.swift:82: Fatal error: Can't read a value from a parsable argument definition

I am new to Swift. I am using this library like this:

@main
final class Program: Options, ParsableCommand {
...
}

where

class Options {
@Option(name: [.customShort("w"), .long], help: "Image width (inches)")
var width: UInt = 24

@Option(name: [.customShort("h"), .long], help: "Image height (inches)")
var height: UInt = 24   

}

when I run my program I get this error:

ArgumentParser/Option.swift:82: Fatal error:


Can't read a value from a parsable argument definition.

This error indicates that a property declared with an @Argument,

@Option, @Flag, or @OptionGroup property wrapper was neither

initialized to a value nor decoded from command-line arguments.

To get a valid value, either call one of the static parsing methods

(parse, parseAsRoot, or main) or define an initializer that

initializes every property of your parsable type.


could anyone tell me what is the problem and how cn I fix it? thanks.

1 Like

I recently ran into this same problem as well, and for anyone looking for help, here's what worked for me:

The main command struct:

@main struct Cosmic: AsyncParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "A package manager for macOS.",
        subcommands: [Setup.self, Add.self])
    
    struct Options: ParsableArguments {
        @Flag(name: [.long, .customShort("v")]) var verbose: Bool = false
    }
}

The subcommand structure:

extension Cosmic {
    struct Setup: AsyncParsableCommand {
        static var configuration = CommandConfiguration(abstract: "Setup the Cosmic package manager.")
        @OptionGroup var options: Cosmic.Options
     ...
     }
}

Constructing a command manually somewhere:

@Test("Set up Cosmic")
func setUpCosmic() async throws {
    var setupCommand = Cosmic.Setup()
    setupCommand.options = try .parse(["--verbose"])
    ...
}

The root cause is as @natecook1000 mentioned in this thread on GitHub, - automatically-generated empty initializers for subcommands do not provide an initial value for these OptionGroups. Therefore, we must set them, and I think the most straightforward way to do that is the parse function, not an initializer.