function export(targetObject, options)
%TARGET.EXPORT    Export.
%   TARGET.EXPORT(targetObject) exports the specified target object to a
%   MATLAB function, registerTargets.m. When you execute the generated
%   function, it adds the target object to an internal database. Use the
%   generated file to share definitions over sessions and across machines.
%
%   TARGET.EXPORT(..., Name, Value) exports the specified target object to
%   a MATLAB function using the name-value pairs.
%   Supported argument names include:
%
%   FileName    Name of MATLAB function file that contains exported target
%               object data. Default is 'registerTargets.m'.
%
%   Overwrite   true            Overwrite MATLAB function file if it
%                               exists.
%               false (default) Generate error if MATLAB function file
%                               exists. File is not overwritten.
%
%   See also TARGET.ADD

%   Copyright 2019-2022 The MathWorks, Inc.

    arguments
        targetObject (1, :) target.Object
        options.FileName string {mustBeScalarOrEmpty} = string.empty()
        options.Overwrite (1, 1) logical = false
    end

    internalObject = targetframework.internal.user.commandline.matlab.Helpers.unwrapValues(targetObject);
    namedargs = namedargs2cell(options);

    % Catch any error thrown by the internal APIs and rethrow it by hiding
    % the stack.
    try
        target.internal.export(internalObject, namedargs{:});
    catch exception
        throwAsCaller(exception);
    end
end
