Creating package options which are mutually exclusive and choosing a default

In my opinion, the numberwithin etc. options should set a \if... variable and postpone the definition of the theorem according to the state of the, say, \if@numberwithin boolean. The argument of numberwithin must be stored, however.

As far as I know this is the way how standard LaTeX classes process mutually exclausive states for options.

\RequirePackage{filecontents}

\begin{filecontents}{mytheorems.sty}
    \RequirePackage{xkeyval}
    \RequirePackage{amsthm}

    \newif\if@numberwithin
    \def\@numberwithin{section}% Defining a default if needed        
    \DeclareOptionX{numberwithin}{%
      \@numberwithintrue
      \gdef\@numberwithin{#1}%
    }
    \DeclareOptionX{nonumberwithin}{%
      \@numberwithinfalse
    }
    \ExecuteOptionsX{nonumberwithin}
    \ProcessOptionsX

    \if@numberwithin
    \theoremstyle{plain}
    \newtheorem{theorem}{Theorem}[\@numberwithin]%
    \else
    \theoremstyle{plain}
    \newtheorem{theorem}{Theorem}%
    \fi

    \theoremstyle{plain}
    \newtheorem{lemma}[theorem]{Lemma}
\end{filecontents}

\documentclass{article}

%\usepackage[numberwithin=section]{mytheorems}%or use option [numberwithin=section] for example

\usepackage{mytheorems}%or use option [numberwithin=section] for example

\begin{document}
    \begin{lemma}
        content...
    \end{lemma}
\end{document}

Add a default option to numberwithin, so that calling

\usepackage[numberwithin]{mytheorems}

or

\usepackage[numberwithin=section]{mytheorems}

will be equivalent as well as \usepackage{mytheorems}, because of \ExecuteOptionsX{numberwithin}. Then delay the association of the counter after processing the options.

\ProvidesPackage{mytheorems}
\RequirePackage{xkeyval}
\RequirePackage{amsmath}
\RequirePackage{amsthm}

\DeclareOptionX{numberwithin}[section]{%
  \def\mthm@numberwithin{#1}%
}
\DeclareOptionX{nonumberwithin}{%
  \def\mthm@numberwithin{}%
}
\ExecuteOptionsX{numberwithin}
\ProcessOptionsX

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}

\ifx\mthm@numberwithin\@empty\else
  \numberwithin{theorem}{\mthm@numberwithin}
\fi

\newtheorem{lemma}[theorem]{Lemma}