function [val, args] = parsem(varargin)
% [val, args] =  parsem( 'name', args , [input3], ['set' || 'condset'] )
% parses varargin
%
% Input:
%   'name'              the questionable argument. The function searches for string <name> in <args>. The return value depends on [input3] and on args.
%
%   args                a cell array (usually varargin)
%         'parse_one'           If contained in args, then val =1. Args is treated in the usual way. Thus the value in args can be different from the return value!!!
%         'parse_random'        If contained in args, then val = 0 or 1. Args is treated in the usual way. Thus the value in args can be different from the return value!!!
%         'help'                If contained in args, then 'name' is printed to the console
%
%   [input3]            (fct handle or a default-value) (Thus the default value never can be a function handle).
%                           If input3 is not given: If the string can be found, and the entry in args behind the string is not a string, then this value is returned.
%                                                   Otherwise, If the string can be found the function returns 1, otherwise 0.
%                           If input3 is given:     If the string can be found, the function returns the value in <args> which is one position behind the last occurence where <name> was found.
%                                                   If that string cannot be found, the function returns the value which is defined input3.
%                                                   If the string cannot be found and input3 is a functionhandle, then return value is functionhandle(input3)
%                       The last occurence of 'name' defines what happens!
% Options:
%   'set'               All occurences of 'name' in args are removed and at the end 'name' (and if given input3) is appended.
%   'condset'           Only sets the value if 'name' is not found. The ordering of the element in args will be changed.
%
% Remark:
%   If an option shall be removed, call this function with the option to be removed and use the second return argument.
%
% Output:
%   val                 The parsed value or the set value
%   args                input arguments without the parsed values (except 'set' or 'condset' is given).
%                       If the input argument is contained more then once, then all of those are removed in the return value.
%
% Eg.:  [v,arg]=parsem('tommsch',{'asd','tommsch',2},'is the best','set')
%       [v,arg]=parsem('tommsch',{'asd','tommsch',0,'tommsch'},'is the second best','condset')
%
% Written by: tommsch, 2016

name=varargin{1};
args=varargin{2};

idx=find(strcmp(varargin,'set'));        if(any(idx)); varargin(idx)=[]; set=1; else; set=0; end;      %look, if 'set' is set
idx=find(strcmp(varargin,'condset'));    if(any(idx)); varargin(idx)=[]; condset=1; else; condset=0; end;      %look, if 'condset' is set

%Set default value, determine if strings are allowed for val
if(size(varargin,2)>=3);
    input3=varargin{3};
    if(isa(input3,'function_handle')); 
        default=input3(name);
    else; 
        default=input3;
    end;
    valcanbestring=1;
else
    default=0;
    valcanbestring=0;
end;

%read value for name, remove everything which is read 
idx=find(strcmp(args, name));
szeargs=size(args,2); %number of elements in args
if(~isempty(idx));
    for i=1:size(idx,2);
        k=idx(i)+1;
        if(szeargs>=k && (~ischar(args{k}) || valcanbestring));
            idx(end+1)=k;  %#ok<AGROW> %save the index of val, so that it can get deleted afterwards
            val=args{k};
        else
            val=1;
        end
    end
    args(idx)=[];
else
    val=default;
end

%make everything for set and condset
if(set || (condset && isempty(idx))); 
    args{end+1}=name;
    val=default;
    args{end+1}=val;
elseif(condset)
    args{end+1}=name;
    args{end+1}=val;
end

if(any(find(strcmp(args,'parse_one'))));    val=1; end;
if(any(find(strcmp(args,'parse_random')))); val=randi(2)-1; end;
if(any(find(strcmp(args,'help'))));         fprintf('%c',name); fprintf('\n'); end;

end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing.   

