Repository: shader-slang/slang
File: source/slang/slang-options.cpp (C++)

Code before:
    CommandLineArg arg;
    SLANG_RETURN_ON_FAIL(m_reader.expectArg(arg));

    if (SLANG_FAILED(StringUtil::parseInt(arg.value.getUnownedSlice(), outInt)))
    {
        m_sink->diagnose(arg.loc, Diagnostics::expectingAnInteger);
        return SLANG_FAIL;
    }
    return SLANG_OK;
}

SlangResult createArtifactFromReferencedModule(
    String path,
    SourceLoc loc,
    DiagnosticSink* sink,
    IArtifact** outArtifact)
{
    auto desc = ArtifactDescUtil::getDescFromPath(path.getUnownedSlice());

    if (desc.kind == ArtifactKind::Unknown)
    {
        sink->diagnose(loc, Diagnostics::unknownLibraryKind, Path::getPathExt(path));
        return SLANG_FAIL;
    }

    // If it's a GPU binary, then we'll assume it's a library
    if (ArtifactDescUtil::isGpuUsable(desc))
    {
        desc.kind = ArtifactKind::Library;
    }

    // If its a zip we'll *assume* its a zip holding compilation results
    if (desc.kind == ArtifactKind::Zip)
    {
        desc.payload = ArtifactPayload::CompileResults;
    }

    if (!ArtifactDescUtil::isLinkable(desc))
    {
        sink->diagnose(loc, Diagnostics::kindNotLinkable, Path::getPathExt(path));
        return SLANG_FAIL;
    }

    const String name = ArtifactDescUtil::getBaseNameFromPath(desc, path.getUnownedSlice());

    // Create the artifact
    auto artifact = Artifact::create(desc, name.getUnownedSlice());

    // There is a problem here if I want to reference a library that is a 'system' library or is not
    // directly a file In that case the path shouldn't be set and the name should completely define
    // the library. Seeing as on all targets the baseName doesn't have an extension, and all library

Review comment (suggestion):
I am not sure if we want to re-use `_expectInt()`.
If the given value was a string value, as an example, the error message will say "expected int value".
And the user replace the string with "-1", as an example, now the error message will change to "expected an unsigned int".

Code after:
    CommandOptions::UserValue& outValue)
{
    CommandLineArg arg;
    SLANG_RETURN_ON_FAIL(m_reader.expectArg(arg));
    SLANG_RETURN_ON_FAIL(_getValue(valueCategory, arg, outValue));
    return SLANG_OK;
}

SlangResult OptionsParser::_expectInt(const CommandLineArg& initArg, Int& outInt)
{
    SLANG_UNUSED(initArg);

    CommandLineArg arg;
    SLANG_RETURN_ON_FAIL(m_reader.expectArg(arg));

    if (SLANG_FAILED(StringUtil::parseInt(arg.value.getUnownedSlice(), outInt)))
    {
        m_sink->diagnose(arg.loc, Diagnostics::expectingAnInteger, initArg.value);
        return SLANG_FAIL;
    }
    return SLANG_OK;
}

SlangResult OptionsParser::_expectUInt(const CommandLineArg& initArg, Int& outInt)
{
    SLANG_RETURN_ON_FAIL(_expectInt(initArg, outInt));
    if (outInt < 0)
    {
        m_sink
            ->diagnose(initArg.loc, Diagnostics::expectingAUnsignedInteger, initArg.value, outInt);
        return SLANG_FAIL;
    }
    return SLANG_OK;
}


SlangResult createArtifactFromReferencedModule(
    String path,
    SourceLoc loc,
    DiagnosticSink* sink,
    IArtifact** outArtifact)
{
    auto desc = ArtifactDescUtil::getDescFromPath(path.getUnownedSlice());

    if (desc.kind == ArtifactKind::Unknown)
    {
        sink->diagnose(loc, Diagnostics::unknownLibraryKind, Path::getPathExt(path));
        return SLANG_FAIL;
    }

    // If it's a GPU binary, then we'll assume it's a library