Using \ifthenelse statements I've defined a macro that prints it's value as long as it is different from the value of the previous time it was used.
\RequirePackage{xifthen}
\def\storedval{} % create a macro to later store a value in
\newcommand{\mycommand}[1]{%
\ifthenelse{\isempty{#1}{}}{\\}{% If empty, don't print anything but keep an empty line.
\ifthenelse{\equal{#1}{\storedval}}{\\}% If value is the same as the previous one, don't print it but keep an empty line.
{#1\\}\def\storedval{#1}}} % Else: print value and store it in \storedval
The purpose for this is that I get data generated by another program but do not want to print the data if it is repeated. In the document, this:
\mycommand{A}
\mycommand{A}
\mycommand{A}
\mycommand{}
\mycommand{B}
\mycommand{C}
\mycommand{C}
\mycommand{A}
... gives me A, three empty lines, B, C, an empty line, and A. That's what I want.
Now I would like to store this in a package and define an option that would let me choose whether or not I want to print repeated lines. I couldn't figure out how to use \DeclareOption properly. I could of course repeat the whole code, and place it inside of two \DeclareOption statements. But I assume there's a more economical way.
So I gave it a shot trying the following MWE, but this is clearly the wrong way (outcommented the lines that are not working). (Disclaimer: this is the first time I'm trying to declare an option for a package ...)
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mypackage.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}
\RequirePackage{xifthen}
\def\storedval{} % create a macro to later store a value in
\newcommand{\mycommand}[1]{%
\ifthenelse{\isempty{#1}{}}{\\}{% If empty, don't print anything but keep an empty line.
%\DeclareOption{skiprepetitions}{\ifthenelse{\equal{#1}{\storedval}}{\\}}% If value is the same as the previous one, don't print it but keep an empty line.
%\DeclareOption{keeprepetitions}{} % don't do anything if this is selected
{#1\\}\def\storedval{#1}}} % Else: print value and store it in \storedval
\DeclareOption*{\PackageWarning{mypackage}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
\end{filecontents}
\usepackage{mypackage}
%\usepackage[skiprepetitions]{mypackage}
\begin{document}
Testing:\\
\mycommand{A}
\mycommand{A}
\mycommand{A}
\mycommand{}
\mycommand{B}
\mycommand{C}
\mycommand{C}
\mycommand{A}
\mycommand{A}
\end{document}

\DeclareOptionwithin a macro (but that is very unusual) but you need to declare any option before\ProcessOptionsis used otherwise it does nothing. In your case the option declaration (even if you ignore the ifthenelse tests) is not executed until (if)\mycommandis executed and that is in the document body long after the package and its options have been processed. – David Carlisle Sep 05 '18 at 07:08