function [linkFlags, groups] = getLinkFlags(this, varargin)
% GETLINKFLAGS The getLinkFlags function returns link options stored
% in the model's build information. Using optional includeGroups and
% excludeGroups arguments, you can selectively include or exclude groups of
% options the function returns.
%
% Argument      description
% buildinfo     Build information handle returned by RTW.Buildinfo.
% 
% includeGroups (optional) A character array or cell array of character arrays
%               that specifies groups of link flags you want the function
%               to return. 
%
% excludeGroups (optional) A character array or cell array of character arrays
%               that specifies groups of link flags you do not want the
%               function to return.
%
% Return values
% the function returns the link options stored in the model's build
% information.
%
% example 1:   
%   myModelBuildInfo = RTW.BuildInfo;
%   addLinkFlags(myModelBuildInfo, {{'-MD -Gy' '-T'},... 
%                   {'Debug' 'MemOpt'});
%   compflags=getLinkFlags(myModelBuildInfo);
%
%   resulting values:
%   compflags == {{'-MD -Gy' '-T'}
%
% example 2:
%   myModelBuildInfo = RTW.BuildInfo;
%   addLinkFlags(myModelBuildInfo, {{'-MD -Gy' '-T'}, {'Debug' 'MemOpt'});
%   compflags=getLinkFlags(myModelBuildInfo, '', 'Debug');
%
%   resulting values:
%   compflags == {'-T'}
%
% Please refer to the documentation for more information.
%

% Copyright 2005-2021 The MathWorks, Inc.
[varargin{:}] = convertStringsToChars(varargin{:});

% Link flags for this component
[linkFlags, ~, ~, groups] = this.Options.getOptions('LinkFlags',varargin{:});

% Child buildInfos must be loaded
loadChildBuildInfos(this);
                
% Acquire link flags from each child buildInfo
childBuildInfos = [this.LinkablesInternal.BuildInfoHandle];
                
for i=1:length(childBuildInfos)
    childBuildInfo = childBuildInfos(i);
    if isempty(childBuildInfo)
        continue;
    end
    [childLinkFlags, ~, ~, childGroups] = childBuildInfo.Options.getOptions('LinkFlags', varargin{:});
    linkFlags = [linkFlags childLinkFlags]; %#ok
    groups = [groups childGroups]; %#ok
end

% Avoid side effect where unique converts a 0x0 empty cell array to a 0x1
% empty cell array
if ~isempty(linkFlags)
    [linkFlags, idx] = unique(linkFlags, 'stable');
    groups = groups(idx);
end



