or
JouleV
Currently, I want to have a pair of command, say `\commentfoo` and `\uncommentfoo` which work like this

```tex
\documentclass{article}
\usepackage{enumitem}
\newlist{foo}{enumerate}{1}
% Do something with \commentfoo and \uncommentfoo
\begin{document}
% This should be shown
\begin{foo}
  \item a
  \item b
\end{foo}
\commentfoo
% This should be hidden, i.e. comment out
\begin{foo}
  \item a
  \item b
\end{foo}
\uncommentfoo
% This should be shown again
\begin{foo}
  \item a
  \item b
\end{foo}
\end{document}
```

So far I have used `comment`'s commands, and it works quite well for "normal" environments:

```tex
\documentclass{article}
\usepackage{comment}
\newenvironment{foo}{}{}
\def\commentfoo{\excludecomment{foo}}
\def\uncommentfoo{\includecomment{foo}}
\begin{document}
\begin{foo}
  This should be shown 1
\end{foo}
\commentfoo
\begin{foo}
  This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
  This should be shown 2
\end{foo}
\end{document}
```

But for list environments, it fails

```tex
\documentclass{article}
\usepackage{enumitem}
\usepackage{comment}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}
\def\commentfoo{\excludecomment{foo}}
\def\uncommentfoo{\includecomment{foo}}
\begin{document}
\begin{foo}
  \item This should be shown 1
\end{foo}
% works until this point
\commentfoo
\begin{foo}
  \item This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
  \item This should be shown 2
\end{foo}
\end{document}
```

How to implement `\commentfoo` and `\uncommentfoo` that works for list environments?
Top Answer
Skillmon
The problem is that the `comment` package doesn't seem to alter the `\endfoo` macro. The following does `\let` the `\endfoo` to `\empty` so that it doesn't call the `enumitem` internals which lead to the error. Also we have to restore `\foo` and `\endfoo` in `\uncommentfoo`, for this we again just use `\let` and don't let the `comment` package do this job (it will not restore the list behaviour, and then we'd get some `Lonely \item` errors).

```tex
\documentclass{article}

\usepackage{enumitem}
\usepackage{comment}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}

% save the definitions of \foo as a list
\let\fooList\foo
\let\endfooList\endfoo

% clear \endfoo and set up \foo to start the comment behaviour
\def\commentfoo{\let\endfoo\empty\excludecomment{foo}}
% restore both \foo and \endfoo
\def\uncommentfoo{\let\foo\fooList\let\endfoo\endfooList}

\begin{document}
\begin{foo}
  \item This should be shown 1
\end{foo}
% works until this point and even works beyond
\commentfoo
\begin{foo}
  \item This should be hidden
\end{foo}
\uncommentfoo
\begin{foo}
  \item This should be shown 2
\end{foo}
\end{document}
```
Answer #2
Skillmon
Since the original answer has the problem that `comment` requires the environment's `\end{foo}` to be completely on its own in a line without anything preceding it and anything following it (including spaces), the following is a new answer, that should work (almost) arbitrarily.

It utilizes the `parser` module of `pgf` to gobble everything regardless of braces. It even tries to locally change the category code of `%` to ignore comments, but this will only work if it's not in an argument of another macro (so that the category change does apply). As long as you don't use `%\end{foo}` to end the environment this limitation doesn't really matter, though.

```
\documentclass{article}

\usepackage{enumitem}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}

\usepackage{pgf}
\usepgfmodule{parser}

% save the definitions of \foo as a list
\let\fooList\foo
\let\endfooList\endfoo

% clear \endfoo and set up \foo to start the comment behaviour
\def\commentfoo
  {%
    \let\endfoo\empty
    \def\foo
      {%
        % set up a parser rule for \end, which grabs an argument and compares
        % that argument to the string 'foo', if they match, end the parser
        \pgfparserdef{commentfoo}{initial}\end[m]
          {%
            \def\tmpA{####1}\def\tmpB{foo}%
            \ifx\tmpA\tmpB
              \pgfparserswitch{final}%
            \fi
          }%
        % need to formally \end the environment, so do this after the parser is
        % done
        \pgfparserdeffinal{commentfoo}{\end{foo}}%
        % ignore input for which no parser rule is defined
        \pgfparserset{commentfoo/silent=true}%
        \catcode`\%=12
        \pgfparserparse{commentfoo}%
      }%
  }
% restore both \foo and \endfoo
\def\uncommentfoo{\let\foo\fooList\let\endfoo\endfooList}

\begin{document}
\begin{foo}
  \item This should be shown 1
\end{foo}
% works until this point and even works beyond.
\commentfoo
\begin{foo}\item This should be hidden\end{foo}This should be shown because it's
not inside of \texttt{foo}.
\uncommentfoo
\begin{foo}
  \item This should be shown 2
\end{foo}
\commentfoo
\begin{foo}THis}is an{ unbalanced token list that's completely #gobbled\end{foo}
\end{document}
```
Answer #3
CrazyHorse
Use `\iffalse`  ... `\fi`:

```
\documentclass{article}
\usepackage{enumitem}
\newlist{foo}{enumerate}{2}
\setlist[foo]{label=\arabic*.}
\let\commentfoo\iffalse 
%\let\commentfoo\iftrue % for printing
\let\uncommentfoo\fi
\begin{document}
\begin{foo}
  \item a
  \item b
\end{foo}
\commentfoo
% This should be hidden, i.e. comment out
\begin{foo}
  \item a
  \item b
\end{foo}
\uncommentfoo
\begin{foo}
  \item a
  \item b
\end{foo}
\end{document}
```

However, instaed of defining the two macros you can simply use

```
\iffalse
% This is hidden, i.e. comment out
% With \iftrue it is active
\begin{foo}
  \item a
  \item b
\end{foo}
\fi
```
How to implement a "switch" that "comment out" a list environment?
Skillmon replying to CrazyHorse
Please note that others are not notified if you don't ping them. You can mark your chat message as a direct reply by clicking on the small red arrow which appears if you hover over a chat message.
CrazyHorse
ah, ok. That's right!
Skillmon replying to CrazyHorse
An example of things which are otherwise totally fine TeX code, but will break inside of `\iffales...\fi`:  
  
```tex  
\documentclass[]{article}

\newcommand\ifempty[1]{\if\relax\detokenize{#1}\relax}

\begin{document}

\iffalse
\ifempty{}
  Do if empty
\else
  Do if not empty
\fi
\fi

\end{document}
```  
  
That's what I meant with limitations.
CrazyHorse
That's obvious, that environments and all `\if...\fi` constructs must be commented  out completely. 
Skillmon replying to CrazyHorse
You can comment out parts that have matching `\if...\fi` blocks and if that's a given, but code which doesn't have matching `\if...\fi` blocks can't be commented out, which means it's not arbitrary. It's better than the limitations of `comment` though, imho, you got some of your stars from me in this case :)
CrazyHorse
Why can't I comment out arbitrary parts of the code? Assuming that all `\if... \fi` parts are covered.
Skillmon
@CrazyHorse Welcome to TopTeX! Please note that you can't comment out arbitrary contents with that (well, the only limitation is that it needs balanced `\if....\fi` statements), which is the only reason why `comment` and my second answer go to such lengths. But for the case in the MWE this is a perfectly fine strategy. 
Skillmon replying to JouleV
oh, and there would be no `silent` option, so it'd most likely throw errors.
Skillmon replying to JouleV
great. Finally someone profits from my `parser` rewrite :) Without it this would've been a lot more complicated (we'd have to define 5 more parser rules).
JouleV replying to Skillmon
Thanks you very much! This time it is perfect!
Skillmon replying to JouleV
I've added a second answer (since the first one does answer the formally asked question how to do this with `comment`, whereas the second doesn't use `comment` at all).
Skillmon replying to JouleV
Is the limitation that the contents of the environment have to be balanced ok for you, or should the code work arbitrarily (only possible if the environment isn't inside of the argument of any macro)? 
Skillmon replying to JouleV
I'll work something out, but have to take care of my kids before that. An easy approach, which works only if the contents are a balanced token list (every opening brace is matched by a closing brace), would be `\usepackage{environ}\newcommand*\commentfoo{\let\foo\undefined\let\endfoo\undefined\NewEnviron{foo}{}}`.
JouleV
@Skillmon Thanks, your answer is very good! However, I found out that using `\excludecomment` means I *must not* indent `\begin{foo}` and `\end{foo}`, which does not quite satisfy my needs. Do you know of any way to remove the `foo` environment no matters how `\begin{foo}` and `\end{foo}` are indented?