function S=parseUsage(str)
% parseUsage <Description here.>

% revision history:
% 04/21/16 Mark D. Shattuck <mds> parseUsage.m
% 01/30/20 mds change optional output designator to ~ [c~,d,e~]
%              added optional input arg (?in/out same syntax?) nb: maybe no
%              use case

%% Main

str=strtrim(str);

% process outputs
eq=find(str=='=',1);

if(isempty(eq))  % if no = then no out str
  S.out=[];
  S.in=str;
  S.oarg={};
else
  S.out=strtrim(str(1:eq-1));
  S.in=str(eq+1:end);
  if(S.out(1)=='[') % should check for closure
    tmp=textscan(S.out(2:end-1),'%s','delimiter',',');
    S.oarg=strtrim(tmp{:})';
  else
    S.oarg={S.out};
  end
end
% optional oargs [c~,d,e~]
loc=strfind(S.oarg,'~');
S.oopt=~cellfun('isempty',loc);
S.oarg(S.oopt)=...
  cellfun(@(x) S.oarg{x}(1:end-1),...
  num2cell(find(S.oopt)),'uniformoutput',false);

% process inputs
S.in=strtrim(S.in);
if(S.in(end)==';');
  S.in=strtrim(S.in(1:end-1));
end
par=find(S.in=='(',1); % should check for closure
if(isempty(par))
  S.fn=S.in;
  S.iargstr='';
else
  S.fn=strtrim(S.in(1:par-1));
  S.iargstr=strtrim(S.in(par+1:end-1));
end

% parse iagrstr
if(isempty(S.iargstr));
  S.iarg={};
  S.iopt={};
else
  delim=[0 regexp(S.iargstr,',(?![^\[]*])') length(S.iargstr)+1];
  Nc=length(delim)-1;
  S.iarg=cell(1,Nc);
  S.iopt=cell(1,Nc);
  for nc=1:Nc
    tmp=S.iargstr(delim(nc)+1:delim(nc+1)-1);
    if(tmp(1)=='[')  % should check for ]
      tmp=tmp(2:end-1);
      S.iarg{nc}=tmp;
      S.iopt{nc}='~';
    else
      bra=find(tmp=='[',1);
      if(~isempty(bra))
        S.iarg{nc}=tmp(1:bra-1);
        ket=find(tmp==']',1,'last');
        S.iopt{nc}=tmp(bra+1:ket-1);
      else
        S.iarg{nc}=tmp;
        S.iopt{nc}='';
      end
    end  
  end
end
S.iopt=strtrim(S.iopt);
S.iarg=strtrim(S.iarg);





