Issue with ArgumentParser - Code stopped working without changes

Hello everyone,

I'm new to Swift and I'm currently facing a strange issue with ArgumentParser. Recently, my code using ArgumentParser has stopped working without any changes being made to the codebase. I'm hoping to get some help in resolving this issue.

In my Swift project i'm using ArgumentParser to handle command-line arguments, yesterday this code was working fine but now it's throwing fatal error. I don't understand what is happenning.

The error is ArgumentParser/ArgumentDecoder.swift:89: Fatal error. The stack trace:

ArgumentParser/ArgumentDecoder.swift:89: Fatal error
Current stack trace:
0    libswiftCore.so                    0x00007f2d757f4dc0 _swift_stdlib_reportFatalErrorInFile + 112
1    libswiftCore.so                    0x00007f2d754be91c <unavailable> + 1444124
2    libswiftCore.so                    0x00007f2d754be738 <unavailable> + 1443640
3    libswiftCore.so                    0x00007f2d754bd220 _assertionFailure(_:_:file:line:flags:) + 807
4    translater                         0x00005652200a2d16 <unavailable> + 531734
5    translater                         0x00005652200a43b0 <unavailable> + 537520
6    libswiftCore.so                    0x00007f2d754da93a <unavailable> + 1558842
7    libswiftCore.so                    0x00007f2d754ef9e0 Dictionary<>.init(from:) + 1830
8    libswiftCore.so                    0x00007f2d754f1a50 <unavailable> + 1653328
9    libswiftCore.so                    0x00007f2d7573d9f0 dispatch thunk of Decodable.init(from:) + 4
10   translater                         0x00005652200a3441 <unavailable> + 533569
11   translater                         0x00005652200a4983 <unavailable> + 539011
12   libswiftCore.so                    0x00007f2d754dc159 <unavailable> + 1565017
13   libswiftCore.so                    0x00007f2d754d36c0 KeyedDecodingContainer.decode<A>(_:forKey:) + 34
14   translater                         0x000056522021a120 <unavailable> + 2068768
15   translater                         0x000056522021ab00 <unavailable> + 2071296
16   libswiftCore.so                    0x00007f2d7573d9f0 dispatch thunk of Decodable.init(from:) + 4
17   translater                         0x00005652200ce0f9 <unavailable> + 708857
18   translater                         0x00005652200cea1b <unavailable> + 711195
19   translater                         0x00005652200cf75f <unavailable> + 714591
20   translater                         0x000056522009f17b <unavailable> + 516475
21   translater                         0x000056522009fbfd <unavailable> + 519165
22   translater                         0x00005652200a0777 <unavailable> + 522103
23   translater                         0x0000565220212e92 <unavailable> + 2039442
24   libc.so.6                          0x00007f2d7511ed90 <unavailable> + 171408
25   libc.so.6                          0x00007f2d7511edc0 __libc_start_main + 128
26   translater                         0x0000565220052945 <unavailable> + 203077
Illegal instruction (core dumped)

My main file:

struct CommandLine: ParsableCommand {
    @Flag(name: .customLong("dry-run"), help: "Prints the results of the operation but does not save it to the database.")
    var dryRun: Bool = false

    @Option(name: .customLong("json"), help: "JSON file to parse." )
    var jsonFile: String?

    @Option(name: .customLong("csv"), help: "CSV file to parse." )
    var csvFile: String?

    @Option(name: .shortAndLong, help: "Source language of the file.")
    var sourceLang: String?

    @Option(name: .shortAndLong, help: "Target language.")
    var targetLang: String?

    @Flag(name: .shortAndLong, help: "Show available languages.")
    var languages: Bool = false

    @Flag(name: .customLong("deleteMaster"), help: "Delete all translations from master table.")
    var deleteMaster: Bool = false

    var translations: [String: String] = [:]    

    mutating func run() throws {
        print("DEBUG PRINT") 
        DatabaseManager.shared.createMasterTable()

        if languages {
            printLanguages()
            return
        }

        // more stuff
    }
}

CommandLine.main()

The print in the first line of the run function never is executed, the fatal error is thrown before it.

Thank you in advance for your help!

Edit: i have read the argument parser documentation and the github repo but i didn't find anything to help me.

@nnnnnnnn it looks like the var translations: [String: Int] = [:] is causing us to call var allKeys: [K] { fatalError() } swift-argument-parser/ArgumentDecoder.swift at main · apple/swift-argument-parser · GitHub. If I remove that dictionary from the code it runs.

2 Likes

Thank you. That was the problem. I can't add attributes to the struct if they are not option or flag ?

You can but you will need to specify a CodingKeys for all the properties that are parsable by ArgumentParser instead of using the compiler synthesized one that includes your translations property.